相关疑难解决方法(0)

如何使用 JWT 在 NextAuth 中存储数据

确认凭据后,我返回以下 JSON:

{username: 'foo', name: 'bar', type: 123}
Run Code Online (Sandbox Code Playgroud)

但是,由于模型限制,NextAuth 不允许我存储所有字段,因此它在 JWT 中返回给客户端的是:

{name: 'bar', email: null, image: null}
Run Code Online (Sandbox Code Playgroud)

我的[...nextauth].js设置非常基本:

providers: [
    Providers.Credentials({
        async authorize(credentials) {
            const res = await fetch('http://localhost:3000/api/user', {
                method: 'GET',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    'credentials': JSON.stringify(credentials)
                }
            })
            const user = await res.json()
            
            if (user && !user.message) {
                return user
            } else {
                return null
            }
        }
    })
],
Run Code Online (Sandbox Code Playgroud)

我想出的唯一解决方案是使用 JSON 字符串伪造电子邮件字段,我可以在其中存储我需要的所有内容:

{name: 'bar', email: "{username: 'foo', type: 123}", image: null}
Run Code Online (Sandbox Code Playgroud)

我怎样才能正确地做到这一点?我尝试研究自定义模型(https://next-auth.js.org/tutorials/typeorm-custom-models …

json jwt next.js next-auth

2
推荐指数
1
解决办法
846
查看次数

标签 统计

json ×1

jwt ×1

next-auth ×1

next.js ×1