Angularjs 如果已经在angular js中登录,如何避免登录
在我的应用程序中,我维护URL来分隔客户,如 myapp.localhost.com/#/customer1 myapp.localhost.com/#/customer2 现在说customer1已登录。然后,在同一个浏览器中,但在其他选项卡中,customer2正在输入其url。在这一点上,我想避免customer2登录,当他点击url时,我想在登录时再次给customer1 url 我使用了下面的代码Angularjs 如果已经在angular js中登录,如何避免登录,angularjs,Angularjs,在我的应用程序中,我维护URL来分隔客户,如 myapp.localhost.com/#/customer1 myapp.localhost.com/#/customer2 现在说customer1已登录。然后,在同一个浏览器中,但在其他选项卡中,customer2正在输入其url。在这一点上,我想避免customer2登录,当他点击url时,我想在登录时再次给customer1 url 我使用了下面的代码 .factory('authService', function ($rootS
.factory('authService', function ($rootScope, $state, $auth, $location, $stateParams) {
return {
isLoggedin: function () {
console.log($auth.isAuthenticated());
if (!$auth.isAuthenticated()) {
if($rootScope.currentUser.accessLevel == '1')
{
$rootScope.currentUser = null;
$location.path('/admin');
}
else
{
$rootScope.currentUser = null;
//$state.go('customer',{'customerTag' : localStorage.getItem('customerTag')});
$location.path("/"+localStorage.getItem('customerTag'));
}
return true;
}
}
};
})
.run(function($rootScope, $state, $stateParams, $location, $auth) {
$rootScope.$state = $state;
$rootScope.$on('$stateChangeStart', function(event, toState) {
});
});
请建议解决方案您可以在路由器配置中设置“解析”键,确保已登录的用户将重定向到管理面板或任何其他位置。像这样:
$stateProvider.state('login', {
url: '/login',
templateUrl: '/templates/login.html',
controller: 'LoginController',
resolve: {
canLogin : ['$q', 'authService', function ($q, authService) {
var deferred = $q.defer();
if (authService.isLoggedin()) {
// redirect user to admin panel or any other place
$location.path('/admin');
// location.path("/"+localStorage.getItem('customerTag'))
deferred.reject();
}
else {
deferred.resolve();
}
return deferred.promise;
}]
}
})
提示:避免使用类似isLoggedin的方法更改存储。Probaly最好根据此方法的返回更新存储。为了澄清-您希望user1登录,user2使用一个新选项卡,您希望他们获得登录屏幕。当user1返回到他们的选项卡时,他们仍然登录,而user2仍然有登录屏幕?如果是这样的话,请考虑将AuthTog保存在内存中,或者保存在本地存储中,但使用唯一的密钥来保持它们的分离。如果用户1在一个选项卡中点击url,则为customer1登录。然后,同一用户在另一个选项卡中点击customer2的url现在在这种情况下,我不想让他显示customer2的登录屏幕,我想让他显示customer1已经登录的屏幕