【发布时间】:2023-02-11 05:21:03
【问题描述】:
我已经阅读了session callbacks 上的文档,并尝试通过保留 JWT 令牌来实现它,但仍然无法正常工作。文档还说,当将 NextAuth 与数据库(在我的例子中是 Firebase,请参阅适配器)一起使用时,它应该:
将 NextAuth.js 与数据库一起使用时,用户对象将是数据库中的用户对象(包括用户 ID)(如果用户之前已登录)或用户的更简单的原型用户对象(即姓名、电子邮件、图像)以前没有登录过的人。
在没有数据库的情况下使用 NextAuth.js 时,用户对象将始终是原型用户对象,并从配置文件中提取信息。
我在这里错过了什么?
[...nextauth].tsimport { FirestoreAdapter } from "@next-auth/firebase-adapter"; import NextAuth from "next-auth"; import GithubProvider from "next-auth/providers/github"; import GoogleProvider from "next-auth/providers/google"; export const authOptions = { // Configure one or more authentication providers providers: [ GithubProvider({ clientId: process.env.GITHUB_ID as string, clientSecret: process.env.GITHUB_SECRET as string, }), GoogleProvider({ clientId: process.env.GOOGLE_ID as string, clientSecret: process.env.GOOGLE_SECRET as string, }), ], adapter: FirestoreAdapter({ apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY, appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID, authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN, databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL, projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID, storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET, messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID, }), callbacks: { async jwt({ token, account, profile }: any) { // Persist the OAuth access_token and or the user id to the token right after signin if (account) { console.log("token", token); token.accessToken = account.access_token; token.id = profile.id; } return token; }, async session({ session, token, user }: any) { // Send properties to the client, like an access_token and user id from a provider. if (token) { session.accessToken = token.accessToken; session.user = {}; session.user.id = token.id; } return session; }, }, }; export default NextAuth(authOptions);
【问题讨论】: