简体   繁体   中英

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

I am new in 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. I checked the solutions of Stack Overflow, then also my problem is not solved.

Here is my code:

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

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...

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

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