如果用户已经登录 ionic 框架,如何跳过登录页面 [英] How to skip login page if user is already logged in ionic framework

查看:35
本文介绍了如果用户已经登录 ionic 框架,如何跳过登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 IONIC 应用程序,我已检查用户是否已登录,如果用户已登录,则应用程序应在仪表板上重定向.此功能运行良好,但应用程序首先显示登录页面几秒钟,然后重定向到仪表板.

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.

我能够进行身份验证并重定向到仪表板,但我的问题是登录页面显示了几秒钟(最多 2 秒),然后重定向到仪表板,我正在处理 <强>IONIC应用

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

第二次编辑我发现了问题,但不知道解决方案.首选项在 $ionicPlatform.ready 中非常有效,但在 $locationChangeStart 中不起作用.我需要在 $locationChangeStart 中使用偏好,因为它在 $ionicPlatformReady 之前运行.我迫切需要解决方案.

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.

推荐答案

我执行以下操作:

在 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 是一个工厂,它将 auth 会话存储在 localstorage 中.

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;
      }
   }
});

这篇关于如果用户已经登录 ionic 框架,如何跳过登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆