简体   繁体   中英

Ionic 1 - Show login page if user not logged and skip if user is already logged

Working on an Ionic version 1.3.3 application where need following functionalities for user login. I had go through all stackoverflow answer but nothing found a workable solution for me.

  1. App will check on start if user already logged in (check through Ionic $localstorage) then redirect to Home page
  2. If the user is not logged redirect to login page on app start
  3. On login page after login success redirect to home page and clear login page history.

     angular.module('starter', ['ionic', 'starter.controllers', 'starter.directives', 'starter.services', 'ngStorage','ab-base64',]) .config(function ($stateProvider, $urlRouterProvider) { $stateProvider .state('app', { url: '/app', abstract: true, cache: false, templateUrl: 'templates/menu.html', controller: 'AppCtrl', onEnter: function ($state) { console.log($state); } }) .state('app.home', { cache: false, url: '/home', views: { 'menuContent': { templateUrl: 'templates/home.html' } } }) .state('app.login', { cache: false, url: '/login/:username/:password', views: { 'menuContent': { templateUrl: 'templates/login.html', controller: 'LoginController' } } }) .state('app.profile', { cache: false, url: '/profile', views: { 'menuContent': { templateUrl: 'templates/profile.html', controller: 'ProfileController' } } }) $urlRouterProvider.otherwise('/app/home'); }) 

This is how I accomplished this in Ionic v1: For the redirect if user is logged in:

.state("app.dash", {
    url: "/dashboard",
    abstract: true,
    views: {
      mainContent: {
        templateUrl: "templates/dashboard.html",
        controller: "DashboardCtrl",
        controllerAs: "vm",
        resolve: {
          auth: [
            "authService",
            function(authService) {
              return authService.isAuthenticated();
            }
          ],
          permissions: [
            "authService",
            function(authService) {
              return authService.getPermissions();
            }
          ]
        }
      }
    }
  })

For the redirect when user logs in or is already logged in.

.state("app.login", {
    url: "/login?accountCreated",
    views: {
      mainContent: {
        templateUrl: "templates/login.html",
        controller: "LoginCtrl",
        controllerAs: "vm",
        resolve: {
          isLoggedIn: [
            "$q",
            "$state",
            "authService",
            function($q, $state, authService) {
              authService.isAuthenticated().then(function(res) {
                $state.go("app.dash.home");
              });
              return $q.defer().resolve();
            }
          ]
        }
      }
    }
  })

Auth service isAuthenticated()

function isAuthenticated() {
  var deferred = $q.defer();

  getToken().then(function(token) {
    isExpired().then(function(isExpired) {
      if (!token || isExpired) {
        deferred.reject("Not Authenticated");
      } else {
        decodeToken().then(function(decodedToken) {
          deferred.resolve(decodedToken);
        });
      }
    });
  });

  return deferred.promise;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM