简体   繁体   English

如果用户已登录离子框架,如何跳过登录页面

[英]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. 我正在开发一个IONIC应用程序,我已经检查过用户是否已经登录,如果用户已经登录,那么应用程序应该重定向到仪表板上。 This functionality is working well, but the application first showing login page for couple of seconds and then redirect to the dashboard. 此功能运行良好,但应用程序首先显示登录页面几秒钟,然后重定向到仪表板。

app.js 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 编辑:我能够进行身份验证并重定向到仪表板,但我的问题是显示少数(最多2秒)的登录页面,然后重定向到仪表板,我正在处理IONIC应用程序

Second Edit I found the problem but don't know the solution. 第二次编辑我发现了问题,但不知道解决方案。 Preference work greatly in $ionicPlatform.ready but do not work in $locationChangeStart. 首选项在$ ionicPlatform.ready中工作很大,但在$ locationChangeStart中不起作用。 And I need preference in $locationChangeStart because it runs before $ionicPlatformReady. 我需要$ locationChangeStart中的首选项,因为它在$ ionicPlatformReady之前运行。 I desperately need the solution. 我迫切需要解决方案。

I do the following: 我做以下事情:

in the app.js 在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. Auth是一个工厂,它将auth会话存储在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;
      }
   }
});

I think you should listen to the event $stateChangeStart on the $rootScope , you can listen to this event during runtime, like this: 我认为您应该在$rootScope上监听事件$stateChangeStart ,您可以在运行时监听此事件,如下所示:

angular.module("myapp.permission").run($rootScope){
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        var loggedIn = true;
        if(loggedIn) {
            $state.go('app.dashboard');
        }
    }
}

Guys try this elegant solution: 伙计们试试这个优雅的解决方案

  1. if the user is not logged in and login is required it redirect to login. 如果用户未登录并且需要登录,则会重定向到登录。

  2. if the user try to get to login and he already logged in, prevent him from doing it. 如果用户尝试登录并且他已经登录,则阻止他这样做。

  3. mark every path as "requireAuth" and it will work. 将每个路径标记为“requireAuth”,它将起作用。

  4. place your code in app.js 将您的代码放在app.js中

     .run(function ($rootScope, $location, authService) { $rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) { if (to.requireAuth && !authService.isAuthed()) { $location.path("/login"); } else if (to.name == 'login' && authService.isAuthed()) { ev.preventDefault(); $location.path("/dashboard"); } }); }) 

Remove 去掉

$urlRouterProvider.otherwise('/loginpage')

at the bottom of routes.js file. 在routes.js文件的底部。

Inside your " run " block and outside of $ionicPlatform.ready you can use this: 在你的“ run ”块内和$ ionicPlatform.ready之外你可以使用这个:

    //Check if user loggedin and redirect to login page if not

$rootScope.$on('$stateChangeStart', function (event, toState) { 

    //Check only if you are not in login page              
    if(toState.name.indexOf('app') !== -1 ) {

      // If user is not logged, will redirect to login page, eg:
      var prefs = plugins.appPreferences;
      prefs.fetch('iuserid').then(function (value) {
         if (value != '') {
            $state.go('login');
         }
      });
    }                         

});  

After you must change your: 你必须改变之后:

$urlRouterProvider.otherwise('/login');

for this: 为了这:

$urlRouterProvider.otherwise('/dashboard');

The idea is directly route to dashboard and if is not logged in, will redirect to login page. 这个想法是直接路由到仪表板,如果没有登录,将重定向到登录页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户已登录离子,如何跳过登录页面 - how to skip login page if user already logged in in ionic Ionic 1 - 如果用户未登录则显示登录页面,如果用户已登录则跳过 - Ionic 1 - Show login page if user not logged and skip if user is already logged AngularJs-如果已经登录,则跳过登录页面 - AngularJs - Skip login page if already logged in 如果用户已经登录Ionic Facebook集成应用程序,如何避免Facebook登录屏幕 - How to avoid Facebook LogIn screen if user already logged in in Ionic facebook integration app 当用户使用ionic登录时,停止用户进入登录页面 - stop user to go login page when user logged in using ionic 如果已经登录 ionic,则重定向用户 - redirect user if the already logged in ionic 如果已经使用angularjs登录,如何避免显示登录页面? - How to avoid showing login page if already logged on with angularjs? 限制用户登录后重定向到登录页面,即使他按角度按浏览器的后退按钮? - restrict the redirection to login page once the user has already logged in even if he press the back button of browser in angular? 如果用户输入 /login 时已经登录,我该如何重定向? - How can I do to redirect if already logged in when user enter in /login? Backand / Ionic-如何获取已登录用户的数据? - Backand/Ionic - How to retrieve data of logged in user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM