更新时间:2023-12-03 15:30:04
下面是我如何与防爆preSS做.js文件:
1)检查,如果用户进行身份验证:我有一个名为CheckAuth中间件功能,我每天都需要用户进行身份验证航线上使用:
1) Check if the user is authenticated: I have a middleware function named CheckAuth which I use on every route that needs the user to be authenticated:
function checkAuth(req, res, next) {
if (!req.session.user_id) {
res.send('You are not authorized to view this page');
} else {
next();
}
}
我用这个功能在我的路线是这样的:
I use this function in my routes like this:
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');
});
如果您想了解更多关于防爆press.js这里查看他们的网站:前pressjs。 COM / guide.html
如果需要更复杂的东西,结账 everyauth (它有很多可用的身份验证方法,Facebook,微博等的;好的教程它这里)。
If you want to learn more about Express.js check their site here: expressjs.com/guide.html If there's need for more complex stuff, checkout everyauth (it has a lot of auth methods available, for facebook, twitter etc; good tutorial on it here).