【问题标题】:Add team with auth user添加具有身份验证用户的团队
【发布时间】:2022-01-04 11:07:06
【问题描述】:

我正在尝试使用身份验证用户添加团队我有中间件路由('auth') 但我无法获得身份验证用户。我是 node.js 的新手,所以我认为这是一个非常简单的问题,但真的看不出我该如何解决这个问题......

public async store({ request, auth, response }: HttpContextContract) {
        const user = auth.user
        const validations = await schema.create({
            name: schema.string({}),
            size: schema.string({}),
        })
        const data = await request.validate({ schema: validations })
        const team = await Team.create(data)
        console.log(Object.keys(team));
        return response.created(team)
    }

【问题讨论】:

    标签: node.js adonis.js


    【解决方案1】:

    您需要确保用户已经过身份验证才能使用 auth.user 对象。
    添加此

    等待 auth.use('web').authenticate()

    public async store({ request, auth, response }: HttpContextContract) {
                await auth.use('web').authenticate()
                const user = auth.user
                const validations = await schema.create({
                    name: schema.string({}),
                    size: schema.string({}),
                })
                const data = await request.validate({ schema: validations })
                const team = await Team.create(data)
                console.log(Object.keys(team));
                return response.created(team)
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      相关资源
      最近更新 更多