【问题标题】:Capture error 401 adonis.js error E_INVALID_SESSION: Invalid session捕获错误 401 adonis.js 错误 E_INVALID_SESSION: Invalid session
【发布时间】:2019-05-08 20:12:25
【问题描述】:

我有一个在 adonis 开发的登录,并且路由已经有 .middleware ('auth') 以保证它们仅在用户输入时使用,但当他没有输入时,错误 401 E_INVALID_SESSION:会话无效,我想要登录地址而不是那个错误

【问题讨论】:

    标签: node.js adonis.js


    【解决方案1】:

    我建议你创建自己的middleware:

    像这样:

    'use strict'
    /** @typedef {import('@adonisjs/framework/src/Request')} Request */
    /** @typedef {import('@adonisjs/framework/src/Response')} Response */
    /** @typedef {import('@adonisjs/framework/src/View')} View */
    
    class AuthVerif {
      /**
       * @param {object} ctx
       * @param {Request} ctx.request
       * @param {Function} next
       */
      async handle({ response, auth }, next) {
        // call next to advance the request
        try {
          await auth.check()
        } catch (error) {
          // custom error response
          return response.status(401).send({ message: error.message })
        }
        await next()
      }
    }
    
    module.exports = AuthVerif
    
    

    【讨论】:

      猜你喜欢
      • 2018-10-01
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多