这是我使用Express.js的方法:
1) 检查用户是否通过身份验证 :我有一个名为CheckAuth的中间件函数,该函数在需要验证用户身份的每条路由上使用:
function checkAuth(req, res, next) { if (!req.session.user_id) { res.send('You are not authorized to view this page'); } else { next(); }}我在这样的路线中使用此功能:
app.get('/my_secret_page', checkAuth, function (req, res) { res.send('if you are viewing this page it means you are logged in');});2) 登录路径:
app.post('/login', function (req, res) { var post = req.body; if (post.user === 'john' && post.password === 'johnspassword') { req.session.user_id = johns_user_id_here; res.redirect('/my_secret_page'); } else { res.send('Bad user/pass'); }});3) 注销途径:
app.get('/logout', function (req, res) { delete req.session.user_id; res.redirect('/login');});如果您想了解有关Express.js的更多信息,请在此处查看其网站:expressjs.com/en/guide/routing.html
如果需要更复杂的内容,请查看everyauth(对于facebook,twitter,它有很多auth方法可用)等;关于此的好教程)。



