egg icon indicating copy to clipboard operation
egg copied to clipboard

在路由中间件中使用passport.authenticate('local'),如何才能登陆后直接执行后面的代码,不跳转?

Open jjackychen opened this issue 8 years ago • 3 comments

在路由中间件中使用passport.authenticate('local'),如何才能登陆后直接执行后面的代码,不跳转?

jjackychen avatar Jun 04 '18 02:06 jjackychen

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?

egg-bot avatar Jun 04 '18 02:06 egg-bot

@atian25 如果不使用passport.authenticate('local'),怎样才能直接将鉴权后的user写入session?让session跟用用户名密码登陆一样?

jjackychen avatar Jun 04 '18 06:06 jjackychen

验证用户权限可以使用中间件

// 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

问题看不懂,时间比较长了,先关闭,有问题再开。

thonatos avatar Jun 29 '18 01:06 thonatos