简体   繁体   English

如果用户使用AngularJS登录,如何重定向到仪表板?

[英]How to redirect to dashboard if user is logged in with AngularJS?

I am new in AngularJS. 我是AngularJS的新手。 I want to redirect my page to dashboard if user is logged in. After login, i am getting access token which I am saving in cookies. 如果用户已登录,我想将页面重定向到仪表板。登录后,我将获取访问令牌,该令牌将保存在Cookie中。 I checked the solutions of Stack Overflow, then also my problem is not solved. 我检查了堆栈溢出的解决方案,然后我的问题也没有解决。

Here is my code: 这是我的代码:

app.js app.js

(function(window){

var app= angular.module('customersApp',['ngRoute','ngCookies','ui.bootstrap']);

app.config(['$routeProvider',
  function ($routeProvider) {
        $routeProvider.
        when('/login', {
            title: 'Login',
            controller: 'loginController',
               templateUrl: 'app/views/loginuser.html'
        })
            .when('/logout', {
                title: 'Logout',
                templateUrl: 'partials/login.html',
                controller: 'loginController'
            })

            .when('/dashboard', {
                title: 'Dashboard',
                templateUrl: 'app/views/dynamic_table.html',
                controller: 'dashboard'
            })
            .when('/verified_artists', {
                title: 'Verified Artists',
                templateUrl: 'app/views/verified_artists.html',
                controller: 'artistController'
            })
            .when('/new_artists', {
                title: 'New Request Artists',
                templateUrl: 'app/views/new_artists.html',
                controller: 'artistController'
            })

            .otherwise({
                redirectTo: '/login'
            });
  }]);
   window.app = app;

}(window));

loginController.js loginController.js

app.controller('loginController', function ($scope,$http,$cookies,$cookieStore) {


    //initially set those objects to null to avoid undefined error
    $scope.login = {};
    $scope.signup = {};
    $scope.doLogin = function (customer) {


  $.post("websiteurl.com/admin_login",
  {

     user_email : $scope.login.email,
      password : $scope.login.password

  },


  function(data,status){

      data = JSON.parse(data);
      console.log(data);

    var someSessionObj = { 'accesstoken' : data.access_token};

    $cookies.dotobject = someSessionObj;
    $scope.usingCookies = { 'cookies.dotobject' : $cookies.dotobject, "cookieStore.get" : $cookieStore.get('dotobject') };

    $cookieStore.put('obj', someSessionObj);
    $scope.usingCookieStore = { "cookieStore.get" : $cookieStore.get('obj'), 'cookies.dotobject' : $cookies.obj, };

    console.log($cookieStore.get('obj').accesstoken);

     if(data.flag==10)
      {
          alert(data.error);
      }
      else
      {
         window.location.href = "#/dashboard";

      }


  })


    };


}); 

try to look for route change event and check for cookie using cookie store... 尝试查找路线更改事件并使用Cookie存储区检查Cookie ...

 $rootScope.$on( "$routeChangeStart", function(event, next, current) { // check for the cookie // if present // check next route // if its login/signup page // event.preventDefault(); (don't make the route change, redirect to dashboard) // $location.path("/dashboard"); // if it is some other route // allow it. // if cookie not present // $location.path("/login"); }); 

暂无
暂无

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

相关问题 使用php将登录用户从登录页面重定向到用户仪表板 - Redirect a logged in user from login page to user dashboard with php 如果用户已登录并且在所有其他情况下显示公共路由,如何重定向到 /dashboard 公共路由 /login 和 /register? - How to redirect to /dashboard public routes /login and /register if user is logged in and in all other cases show public routes? 如何检查用户是否登录然后重定向? - How to check if user logged and then redirect? 用户登录后如何重定向? - How to redirect once user is logged in? 如果用户未登录 Firebase,如何重定向 - How to redirect if User Is NOT logged-in in Firebase 如果用户在 express js 中尝试访问登录路由时已经登录,我想重定向到仪表板 - I want to redirect to dashboard if user is already logged in when he try to access login route in express js angularjs + ui-router:当用户未登录每个状态更改时,重定向到登录页面 - angularjs + ui-router: redirect to login page when user is not logged in on each state change 如何检查用户是否使用$ routeChangeStart登录了angularjs - How do i check if user is logged in angularjs with $routeChangeStart 如何检测用户是否已登录以重定向到特定页面javascript - How to detect if the user is logged in to redirect to a specific page javascript 您如何重定向未使用护照身份验证登录的用户 - how do you redirect a user who is not logged in using passport authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM