[英]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.