【问题标题】:Nextjs plus nextauth cannot configure jwtnextjs加nextauth无法配置jwt
【发布时间】:2021-12-07 05:53:17
【问题描述】:

我正在尝试使用 NextAuth 设置 JWT,这只是一种痛苦,阅读 GitHub 上的帖子,并且研究没有任何帮助。我有一种感觉,当 NextAuth 尝试解码 JWT 有效负载时发生了一些错误,我只是想返回一串在登录时从 Twitch 返回的数字,我需要在对 Twitch 的 API 调用中使用它。

我是 JWT 和 NextAuth 的新手,所以我不是专业人士!

整个项目的github repo https://github.com/MatWebsites/next-auth-example-main

任何帮助都会很大!

// console errors
unhandledRejection: TypeError: Cannot read properties of undefined (reading 'next-auth.session-token')
-
error - Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
-
TypeError: getCurves is not a function
-

nextjs 错误页面

//[...nextauth].js
import NextAuth from "next-auth"
import Providers from "next-auth/providers"

export default NextAuth({
  providers: [
    Providers.Twitch({
      clientId: process.env.TWITCH_CLIENT_ID,
      clientSecret: process.env.TWITCH_CLIENT_SECRET,
    }),
    Providers.Spotify({
      clientId: process.env.SPOTIFY_CLIENT_ID,
      clientSecret: process.env.SPOTIFY_CLIENT_SECRET
    }),
  ],
  secret: process.env.SECRET,

  session: {
    jwt: true,
  },

  // https://next-auth.js.org/configuration/options#jwt
  jwt: {
    // A secret to use for key generation (you should set this explicitly)
     secret: process.env.SECRET,
    // Set to true to use encryption (default: false)
    // encryption: true,
    // You can define your own encode/decode functions for signing and encryption
    // if you want to override the default behaviour.
    // encode: async ({ secret, token, maxAge }) => {},
    // decode: async ({ secret, token, maxAge }) => {},
  },
  callbacks: {
     async signIn(user, account, profile) { return true },
    // async redirect(url, baseUrl) { return baseUrl },
     async session(session, user) { return session },
     async jwt(token, user, account, profile, isNewUser) { 
       token.accessToken = account.access_token;
       return token 
      }
  },
  theme: 'light',

  debug: true,
})
//test.js - trying to decode and use the data inside the web token
import { getToken } from 'next-auth/jwt'
const secret = process.env.SECRET

const test =  (req, res) => {
    const token = getToken({req, secret});
    return (
        <div>
            <a>{token.sub}</a> // Should show the users id returned from Twitch
        </div>
    )
}

export default test

【问题讨论】:

    标签: javascript next.js next-auth twitch-api


    【解决方案1】:

    getToken 返回一个承诺。在对它进行任何操作之前,您必须为它await

    const test = async  (req, res) => {
        const token = await getToken({req, secret});
        return (
            <div>
                <a>{token.sub}</a> // Should show the users id returned from Twitch
            </div>
        )
    }
    

    【讨论】:

    • 添加这个仍然会导致这个错误错误 - Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
    • jwt回调中,尝试有条件地添加accessToken,lif (account){ token.accessToken = account.access_token
    • account 参数仅在首次登录时可用,因此请尝试添加条件。
    猜你喜欢
    • 2022-01-07
    • 2021-05-10
    • 2022-06-13
    • 2021-11-03
    • 2022-10-23
    • 2021-07-09
    • 2019-05-29
    • 2021-07-13
    • 1970-01-01
    相关资源
    最近更新 更多