问题描述
我想在所有页面上保持会话状态.对于这个项目,我使用expressJs,nodeJS作为服务器端.前端是AngularJS.
I would like to keep session across all the page. For this project, I am using expressJs, nodeJS as server side. AngularJS in front end.
我不确定在视图更改或URL更改时如何处理会话.因为我需要同时照顾 expressJS路由器或angularJs路由器.
I am not sure, how to handle session when view changes or url changes. Because I need to take care of both expressJS router or angularJs router.
我应该采用哪种方法?
angularJS路由器
myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/welcome', {templateUrl: 'partials/welcome.html', controller: 'MyCtrl2'});
$routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'MyCtrl2'});
$routeProvider.when('/signup', {templateUrl: 'partials/signup.html', controller: 'singupController'});
$routeProvider.otherwise({redirectTo: '/'});
}]);
注册控制器
myApp.controller('singupController',function($scope,$rootScope,$http){
$scope.doSingnup = function() {
var formData = {
'username' : this.username,
'password' : this.password,
'email' : null
};
var jdata = JSON.stringify(formData);
$http({method:'POST',url:'/signup',data:jdata})
.success(function(data,status,headers,config){
console.log(data);
}).
error(function(data,status,headers,config){
console.log(data)
});
}
})
ExpressJS路由器
module.exports = exports = function(app, db) {
var sessionHandler = new SessionHandler(db);
var contentHandler = new ContentHandler(db);
// Middleware to see if a user is logged in
app.use(sessionHandler.isLoggedInMiddleware);
app.get('/', contentHandler.displayMainPage);
app.post('/login', sessionHandler.handleLoginRequest);
app.get('/logout', sessionHandler.displayLogoutPage);
app.get("/welcome", sessionHandler.displayWelcomePage);
app.post('/signup', sessionHandler.handleSignup);
app.get('*', contentHandler.displayMainPage);
// Error handling middleware
app.use(ErrorHandler);
}
注册后,我想重定向到登录页面.我该如何在上述路由器中做到这一点.我应使用以下哪一项来更改应用的视图
1)angularJS的$ location
2)ExpressJS重定向
After signup, I would like to redirect to the login page. How can I do that in the above router. which one of the following should I use to change the view of app
1) $location of angularJS
2) redirect of ExpressJS
推荐答案
所以我遇到了同样的问题,老实说,我可能已经在我不记得的地方读过这种方法了.
So i had the same problem and to be fair i might have read the approach somewhere i don't remember anymore.
问题:Angular构建单页应用程序.刷新后,您将失去作用域以及已认证的用户.
Problem: Angular builds single page apps. After refresh, you loose scope and with it the authenticated user.
方法
AngularJS模块提供了名为 run 的启动功能页面加载时始终调用.非常适合刷新/重新加载.
AngularJS modules offer a startup function called run which is called always when the page is loaded. Perfect for refresh/reload.
myApp.run(function ($rootScope, $location, myFactory) {
$http.get('/confirm-login')
.success(function (user) {
if (user && user.userId) {
$rootScope.user = user;
}
});
}
express-session 为您保存会话,并使用浏览器发送的sessionId对您进行身份验证.因此,它始终知道您是否通过了身份验证.
express-session saves the sessions for you and authenticates you with the sessionId your browser sends. So it always knows if you are authenticated or not.
router.get('/confirm-login', function (req, res) {
res.send(req.user)
}
);
我要做的就是刷新并加载所有依赖项之后,询问我是否已通过身份验证并设置$ rootScope.user = authenticatedUserFromExpress;
All i had to do is, after refreshing and all dependencies were loaded, ask if i am authenticated and set $rootScope.user = authenticatedUserFromExpress;
这篇关于AngularJS和ExpressJS会话管理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!