我正在努力为我正在创建的应用设置登录系统。
我可以在用户登录或注销时设置Cookie。我不认为如果用户登录就测试每个视图是一个非常优雅的解决方案,我担心这里和那里的页面可能会出现裂缝(这是一个相当大的应用程序)。
我认为最好的方法是以某种方式拦截路由更改并检查用户是否已登录,否则将其发送到登录/创建用户页面。我找到了一些方法,但似乎没有任何正式记录。有没有人在现实案例中使用过这种方法,是否有效?
我的路线文件如下:
'use strict';
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
// LOGIN
.when('/User/LoginUser', {templateUrl: 'views/user/login.html',controller: 'loginCtrl'})
....... more routes here.......
// DEFAULT
.otherwise({redirectTo: '/'});
}]);
任何帮助或建议,或者指出我将如何做这样的事情的现实世界的例子将非常感谢!
答案 0 :(得分:28)
您可以按照建议拦截路线更改并采取相应措施,使用以下示例作为基础:
$rootScope.$on('$routeChangeStart', function (event, next) {
var userAuthenticated = ...; /* Check if the user is logged in */
if (!userAuthenticated && !next.isLogin) {
/* You can save the user's location to take him back to the same page after he has logged-in */
$rootScope.savedLocation = $location.url();
$location.path('/User/LoginUser');
}
});
另外,将isLogin: true
添加到登录页面的路由定义中,如下所示:
$routeProvider
// LOGIN
.when('/User/LoginUser', {templateUrl: 'views/user/login.html',controller: 'loginCtrl', isLogin: true})
祝你的项目好运!
答案 1 :(得分:-17)
我的意见:
<?php if (!$_SESSION['user_id'] { forward(/user/access); }) ?>
这是你的角度应用......