【问题标题】:NextAuth: JWT callback returning objectNextAuth:JWT 回调返回对象
【发布时间】:2022-01-17 06:00:39
【问题描述】:

我正在使用 Next.js (11.1.2) + NextAuth (^4.0.5) + Strapi(3.6.8) 进行项目。

我正在使用 Next Auth 凭据提供程序,它工作正常。但是我需要使用session 访问一些用户信息,所以我尝试使用jwtsession 回调来做到这一点。

当我在authorize() 中记录来自strapi 的响应时,我收到{ jwt:{}, user:{} },所以没关系。

//[...nextauth.js]

async authorize(credentials, req) {
        try {
          const { data } = await axios.post(process.env.CREDENTIALS_AUTH_URL, credentials)
          if (data) {
            //console.log('data: ', data) //this is ok
            return data;
          }
          else {
            return null;
          }
        } catch (e) {
          return null;
        }
},

但是,在jwt 回调中,当我登录token 时,我得到一个带有{token:{token:{token:{...}}} 的奇怪obj:

// [...nextauth.js] callback:{ jwt: async (token) => { console.log(token) }}

token: {
  token: {
    token: {},
    user: {
      jwt: ...,
      user: [Object]
    },
    account: { type: 'credentials', provider: 'credentials' },
    isNewUser: false,
    iat: ...,
    exp: ...,
    jti: ...
  }
}

并且accountuser 在回调中始终未定义。

最后,当我在一个页面中从useSession 得到session 时,我得到了这个:

// console.log(session) in any page

{ 
  session: {
    expires: "2022-01-12T19:27:53.429Z"
    user: {} // empty
  },
  token:{
    token:{
      account: {type: 'credentials', provider: 'credentials'}
      exp: ...
      iat: ...
      isNewUser: false
      jti: "..."
      token: {} // empty
      user: { //exactly strapi response
         jwt:{...}
         user:{...}
      }
    }
  }
}

我发现的所有示例都没有使用这种令人困惑的结构处理这些对象,我不知道我是否遗漏了什么。你能帮帮我吗?

这是我的[...nextauth].js

import NextAuth from "next-auth"
import CredentialsProvider from 'next-auth/providers/credentials'
import axios from 'axios';

export default NextAuth({
  providers: [
    CredentialsProvider({
      name: '...',
      credentials: {
        email: {label: "Email", type: "text", placeholder: "email@provider.com"},
        password: {  label: "Password", type: "password" },
      },
      async authorize(credentials, req) {

        try {
          const { data } = await axios.post(process.env.CREDENTIALS_AUTH_URL, credentials)
          if (data) {
            //console.log('data: ', data)
            return data;
          }
          else {
            return null;
          }
        } catch (e) {
          return null;
        }
      },
    })
  ],
  secret: process.env.SECRET,
  session: {
    strategy: 'jwt',
    maxAge: 30 * 24 * 60 * 60 // 30 days
  },
  jwt: {
    secret: process.env.JWT_SECRET,
    encryption: true,
  },
  callbacks: {
    jwt: async (token, account) => {

      console.log('### JWT CALLBACK ###')
      console.log('token: ', token)
      console.log('account: ', account)

      return token;
    },
  
    session: async (session, token, user) => {
      console.log('### SESSION CALLBACK ###')
      console.log('session: ', session)
      console.log('user: ', token)
      console.log('user: ', user)

      return session;
    }
  },
  pages: {
    signIn: '/signin',
    signOut: '/signin',
    error: '/signin'
  }
})

【问题讨论】:

    标签: javascript jwt next.js strapi next-auth


    【解决方案1】:

    请尝试:

    - session(session, tokenOrUser)
    + session({ session, token, user })
    
    - jwt(token, user, account, OAuthProfile, isNewUser)
    + jwt({ token, user, account, profile, isNewUser })
    

    https://next-auth.js.org/getting-started/upgrade-v4#callbacks

    【讨论】:

      猜你喜欢
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 2015-11-11
      • 2016-03-17
      • 2015-09-12
      • 2016-04-15
      • 1970-01-01
      相关资源
      最近更新 更多