【发布时间】:2017-08-12 10:28:58
【问题描述】:
我正在使用本地护照将用户登录到我的应用程序。我们通过下面的函数登录,该函数在我的 angularJS 控制器中调用。
$http.post('/login', $scope.user).then(function (response){
if(response.data.success){
//If successful I console.log the user data and redirect them to the main page.
console.log(response.data.user);
$location.path(response.data.redirectTo);
} else {
// Handle the error messages passed back by my express routing.
}
});
这按预期工作,如果我使用正确的详细信息登录,那么它将 console.log 用户,然后将我重定向到新页面。
我的问题是我现在如何做到这一点,以便我的所有其他页面都可以确保用户已登录?我应该对收到的用户数据使用什么? 我正在谈论的页面使用以下 $routeProvider 路由:
app.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/login', {
templateUrl: 'login.html',
controller: 'loginCtrl'
});
$routeProvider.when('/home', {
templateUrl: 'home.html',
controller: 'homeCtrl',
// Something to make sure the user is logged in
});
}])
我读过的所有教程都要求我通过快速路由来处理这个问题,但这只会导致重定向循环,因为/login 没有通过isAuthenticated 路由中间件。
app.get('*', isAuthenticated, function(req,res){
res.sendFile("index.html");
});
function isAuthenticated(req,res,next){
if(req.isAuthenticated()){
next();
} else {
res.redirect('/login');
}
}
任何关于如何从这里向前推进的帮助将不胜感激。
【问题讨论】:
标签: javascript angularjs express passport.js mean-stack