egg
egg copied to clipboard
在路由中间件中使用passport.authenticate('local'),如何才能登陆后直接执行后面的代码,不跳转?
在路由中间件中使用passport.authenticate('local'),如何才能登陆后直接执行后面的代码,不跳转?
Translation of this issue:
# In the routing middleware use the passport.Authenticate ('local'), how can we directly execute the following code after login, do not jump?
Using the passport.authenticate('local') in the routing middleware, how can I execute the subsequent code directly after login and not jump?
@atian25 如果不使用passport.authenticate('local'),怎样才能直接将鉴权后的user写入session?让session跟用用户名密码登陆一样?
验证用户权限可以使用中间件
// app/middleware/admin_required.js
module.exports = () => {
/*
* 需要管理员权限
*/
return async function(ctx, next) {
if (!ctx.user) {
await ctx.render('notify/notify', { error: '你还没有登录。' });
return;
}
if (!ctx.user.is_admin) {
await ctx.render('notify/notify', { error: '需要管理员权限。' });
return;
}
await next();
};
};
// app/router.js
module.exports = app => {
const { router, controller, config, middleware } = app;
const adminRequired = middleware.adminRequired();
router.get('/setting', adminRequired, controller.setting);
}
Session 问题参考文档
- https://eggjs.app/zh-cn/core/cookie-and-session.html
问题看不懂,时间比较长了,先关闭,有问题再开。