【发布时间】:2022-11-18 06:06:35
【问题描述】:
我正在制作一个使用 t3 堆栈的应用程序,并希望将来自 nextauth 的凭据提供程序置于其他提供程序之上,但是当我尝试让用户登录时,我只会在控制台中收到此错误。
cookie next-auth.session-token has been rejected because it is already expired
当我检查本地存储时,没有 nextauth.session-token,但是当我使用 github 登录时,我可以看到 cookie,并且一切正常。我的想法是,在注册用户时我需要做一些事情,但我可能错了,我只需要适当地配置 nextauth。我的 nextauth 选项是
export const authOptions: NextAuthOptions = {
// Include user.id on session
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id
}
return session
},
},
// Configure one or more authentication providers
adapter: PrismaAdapter(prisma),
providers: [
CredentialsProvider({
id: 'app',
credentials: {
email: { label: 'Email', type: 'email' },
password: { label: 'Password', type: 'password' },
},
authorize: async (credentials: { email: string; password: string }) => {
const user = await prisma.user.findUnique({
where: { email: credentials.email },
})
const valid = await bcrypt.compare(credentials.password, user.password)
if (!user || !valid) {
return null
}
if (user) {
return { ...user, email: user.email }
}
return user
},
}),
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
GitHubProvider({
clientId: env.ID_GITHUB,
clientSecret: env.SECRET_GITHUB,
}),
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
authorization: {
params: {
prompt: 'consent',
access_type: 'offline',
response_type: 'code',
},
},
}),
],
}
我正在使用 prisma 来管理我的数据库,并且可以在 prisma studio 中看到我在我的应用程序中注册的用户与来自其他提供商的用户之间存在差异。我想使用凭据的那些没有任何帐户链接到他们,也没有会话,同时使用 github 登录的其他用户有那个。
如果你想查看完整的源代码,你可以在这里找到 repo https://github.com/Retrokiller543/Blog-app-t3。如果您想自己查看问题,还可以在 https://blog-app-woad-one.vercel.app/ 上找到该应用程序的 Vercel 部署。
我试图找到有关 nextauth 如何制作令牌以及它需要什么的任何信息,但还没有找到任何明确的信息。
【问题讨论】:
标签: typescript postgresql next.js prisma next-auth