JWT 会话的 Next Auth / NextJS 错误

问题描述 投票:0回答:1

嗨嗨,我希望你们中的一个人可以帮助我。目前我怀疑通过 next-auth 的 getServerSession 方法对用户进行身份验证。

我通过带有棱镜适配器的魔术链接进行登录。在前端,会话有效,在数据库中,会话也被干净地存储。

只有该方法不起作用并返回此错误:

[next-auth][error][JWT_SESSION_ERROR] 
https://next-auth.js.org/errors#jwt_session_error Invalid Compact JWE {
  message: 'Invalid Compact JWE',
  stack: 'JWEInvalid: Invalid Compact JWE\n' +
    '    at compactDecrypt (webpack-internal:///(rsc)/./node_modules/jose/dist/node/cjs/jwe/compact/decrypt.js:18:15)\n' +
    '    at jwtDecrypt (webpack-internal:///(rsc)/./node_modules/jose/dist/node/cjs/jwt/decrypt.js:10:61)\n' +
    '    at Object.decode (webpack-internal:///(rsc)/./node_modules/next-auth/jwt/index.js:44:52)\n' +
    '    at async Object.session (webpack-internal:///(rsc)/./node_modules/next-auth/core/routes/session.js:25:34)\n' +
    '    at async AuthHandler (webpack-internal:///(rsc)/./node_modules/next-auth/core/index.js:161:37)\n' +
    '    at async getServerSession (webpack-internal:///(rsc)/./node_modules/next-auth/next/index.js:125:21)\n' +
    '    at async GET (webpack-internal:///(rsc)/./src/app/api/users/route.ts:13:21)\n' +
    '    at async eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:254:37)',
  name: 'JWEInvalid'
}
Session null

我的控制台日志每次都显示为空...

auth/[...nextauth]/route.ts 的代码:

import { PrismaClient } from "@prisma/client";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import NextAuth from "next-auth";
import type { NextAuthOptions } from "next-auth";
import GithubProvider from "next-auth/providers/github";
import EmailProvider from "next-auth/providers/email";

const prisma = new PrismaClient();

export const authOptions: NextAuthOptions = {
  adapter: PrismaAdapter(prisma),
  providers: [
    // GithubProvider({
    //   clientId: process.env.GITHUB_ID!,
    //   clientSecret: process.env.GITHUB_SECRET!,
    // }),
    EmailProvider({
      server: {
        host: process.env.EMAIL_SERVER_HOST,
        port: process.env.EMAIL_SERVER_PORT,
        auth: {
          user: process.env.EMAIL_SERVER_USER,
          pass: process.env.EMAIL_SERVER_PASSWORD,
        },
      },
      from: process.env.EMAIL_FROM!,
      maxAge: 24 * 60 * 60,
    }),
  ],
};

const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };

我非常希望这里有人知道我来的解决方案,因为真的没有进一步的:(

reactjs authentication next.js jwt next-auth
1个回答
0
投票

我遇到了同样的问题,我解决了添加 会议: { 策略:“jwt” }, 在 authOptions 中提供者之后

© www.soinside.com 2019 - 2024. All rights reserved.