How to skip login page if user is already registered in ion

Hi, I am new to ionic structure. I use the session manager in ion mode. But I want to skip the login page if the user is already logged in.

app.js

angular.module('grocery', ['ionic', 'grocery.controller', 'ngCordova', 'ngCordovaOauth']) .run(function($ionicPlatform, $cordovaSQLite, $cordovaToast, $rootScope, mainItemsList, $state) { $ionicPlatform.ready(function() { if (window.cordova && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); stops the viewport // from snapping when text inputs are focused. Ionic handles this internally for // a much nicer keyboard experience. cordova.plugins.Keyboard.disableScroll(true); } if (window.StatusBar) { StatusBar.styleDefault(); } }); }); $rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) { if (mainItemsList.isLoggedIn() != true) { $state.go('app.login'); } }) .config(function($stateProvider, $urlRouterProvider) { $stateProvider .state('app', { url: "/app", abstract: true, templateUrl: "templates/navigationDrawer.html", controller: 'AppCtrl' }) .state('app.masterList', { url: "/masterList", views: { 'menuContent': { templateUrl: "templates/masterList.html", controller: 'indexCtrl' } } }) .state('app.login', { url: "/login", views: { 'menuContent': { templateUrl: "templates/login.html", controller: 'loginCtrl' } } }) .state('app.register', { url: "/register", views: { 'menuContent': { templateUrl: "templates/register.html", controller: 'registerCtrl' } } }) $urlRouterProvider.otherwise("/app/masterList"); }); angular.module('grocery.services', []) .factory('mainItemsList', function($cordovaSQLite, $cordovaToast, $cordovaPreferences) { return { isLoggedIn: function(sessionEmail) { $cordovaPreferences.store('email', sessionEmail).success(function(value) { //$cordovaToast.showShortTop('stored'); }) .error(function(error) { $cordovaToast.showShortTop("Error " + error); }) return true; } } }) 

I tried existing stackoverflow answers. But does not work. please help me where am i wrong.

+5
source share
2 answers

Create a new controller and a new state called autologin . Make it the default. In the autologin controller, check if the user has already been registered. If it is, redirect to a page. If this is not the case, redirect the login.

  .state('app.autologin', { url: "/autologin", controller: 'autologinCtrl' }) $urlRouterProvider.otherwise("/app/autologin"); 

controller:

 angular.module('grocery').controller('autologinCtrl, function($scope, $state){ //check if user is logged in if (userLoggedIn){ state.go('app.masterList'); } else { state.go('app.login'); } }); 
0
source

If you add a new controller for this logic, there will be a chance to flicker between pages. To handle this, use $urlRouterProvider

 .config(function($stateProvider, $urlRouterProvider, mainItemsList) { $stateProvider .state('app', { url: "/app", abstract: true, templateUrl: "templates/navigationDrawer.html", controller: 'AppCtrl' }) .state('app.masterList', { url: "/masterList", views: { 'menuContent': { templateUrl: "templates/masterList.html", controller: 'indexCtrl' } } }) .state('app.login', { url: "/login", views: { 'menuContent': { templateUrl: "templates/login.html", controller: 'loginCtrl' } } }) .state('app.register', { url: "/register", views: { 'menuContent': { templateUrl: "templates/register.html", controller: 'registerCtrl' } } }); // if none of the above states are matched, use this as the fallback $urlRouterProvider.otherwise(function() { var logged = mainItemsList.isLoggedIn(); // Check User logined or not if (logged != true) { return 'app.login'; } else { return 'app.masterList'; } }); }); 
0
source

Source: https://habr.com/ru/post/1247206/


All Articles