Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip login page if user is already logged in ionic framework

I am working on an IONIC application where I have checked if user is already logged in and if user is already logged then application should redirect on dashboard. This functionality is working well, but the application first showing login page for couple of seconds and then redirect to the dashboard.

app.js

$rootScope.$on("$locationChangeStart", function (event, next, current) {
    var prefs = plugins.appPreferences;
    prefs.fetch('iuserid').then(function (value) {
        if (value != '') {
            $state.go('app.dashboard');
        }
    });
.config(function ($stateProvider, $urlRouterProvider, $httpProvider) {
    $stateProvider
        .state('app', {
            url: "/app",
            abstract: true,
            templateUrl: "templates/menu.html",
            controller: 'AppCtrl'
        })
        .state('login', {
            url: "/login",
            templateUrl: "templates/login.html",
            controller: 'LoginCtrl'
        })
        .state('app.dashboard', {
            url: "/dashboard",
            views: {
                'menuContent': {
                    templateUrl: "templates/dashboard.html",
                    controller: 'DashboardCtrl'
                }
            }
        })
        ;
    // if none of the above states are matched, use this as the fallback
    $urlRouterProvider.otherwise('/login');
    });
});

I don't know where I am making mistake.

Edit: I am able to authenticate and redirect to the dashboard but my problem is the login page displayed for few (up to 2) seconds and then redirect to the dashboard and I am working on IONIC application

Second Edit I found the problem but don't know the solution. Preference work greatly in $ionicPlatform.ready but do not work in $locationChangeStart. And I need preference in $locationChangeStart because it runs before $ionicPlatformReady. I desperately need the solution.

like image 678
Alpesh Trivedi Avatar asked Jun 18 '15 13:06

Alpesh Trivedi


1 Answers

I do the following:

in the app.js

.state('login', {
    url: "/login",
    templateUrl     : "templates/session/login.html",
    controller      : 'SessionCtrl'
  })

  .state('register', {
    url: "/register",
    templateUrl     : "templates/session/register.html",
    controller      : 'SessionCtrl'
  })
  .state('app', {
    url: "/app",
    abstract: true,
    templateUrl     : "templates/menu.html",
    controller      : 'AppCtrl',
    onEnter: function($state, Auth){
        if(!Auth.isLoggedIn()){
           $state.go('login');
        }
    }
  })
  .state('app.main', {
    url: "/main",
    views: {
      'menuContent': {
        templateUrl   : "templates/main_menu.html",
        controller    : "MainMenuCtrl"
      }
    }
  })

  $urlRouterProvider.otherwise('/app/main');

Auth is a factory, it stores the auth session in localstorage.

angular.module('auth.services', [])
.factory('Auth', function () {
   if (window.localStorage['session']) {
      var _user = JSON.parse(window.localStorage['session']);
   }
   var setUser = function (session) {
      _user = session;
      window.localStorage['session'] = JSON.stringify(_user);
   }

   return {
      setUser: setUser,
      isLoggedIn: function () {
         return _user ? true : false;
      },
      getUser: function () {
         return _user;
      },
      logout: function () {
         window.localStorage.removeItem("session");
         window.localStorage.removeItem("list_dependents");
         _user = null;
      }
   }
});
like image 91
Mikel Sanchez Avatar answered Nov 16 '22 09:11

Mikel Sanchez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!