Need help in resolving "Invalid Compact JWE" error #4255
Replies: 8 comments 20 replies
-
Possibly related: #4075 (comment) |
Beta Was this translation helpful? Give feedback.
-
Hello , I'm also having same issue |
Beta Was this translation helpful? Give feedback.
-
You need export the authOptions as well as Nextauth{authOptions} then on index.js receive the authOptions on ServerSideSession() I has the same issue because I did not export authOptions it self I was only exporting NextAuth(authOptions).. hope this help! |
Beta Was this translation helpful? Give feedback.
-
I cant seem to get rid of this error with the app directory. Even if I strip auth down to nothing, I still see this error.
// app/api/auth/[...nextauth]/route.ts
import NextAuth, {type NextAuthOptions} from "next-auth"
export const authOptions: NextAuthOptions = {
debug: true,
pages: {
signIn: "/login"
},
providers: [
]
}
const handler = NextAuth(authOptions)
export { handler as GET, handler as POST } // app/page.tsx
export default async function Home() {
const session = await getServerSession(authOptions)
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<pre>{JSON.stringify(session, null, 2)}</pre>
</main>
)
} [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 Home (webpack-internal:///(rsc)/./app/page.tsx:13:21)',
name: 'JWEInvalid'
} |
Beta Was this translation helpful? Give feedback.
-
Mostly, this problem has to do with how you are configuring your NextAuth and how you call getServerSession(). If you find yourself struggling with this issue here is an example NextAuth config to help you fix your problem. Note: I'm using next 13 First, ensure you are configuring your providers and authOptions correctly. import { db } from '@/src/server/db.server';
import { PrismaAdapter } from '@auth/prisma-adapter';
import { NextAuthOptions } from 'next-auth';
import NextAuth from 'next-auth/next';
import GithubProvider from 'next-auth/providers/github';
export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(db),
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID!,
clientSecret: process.env.GITHUB_SECRET!,
}),
],
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST }; Second, let's say you are trying to fetch users from the database and you are using getServerSession() to ensure only logged in users can access the route import { db } from '@/src/server/db.server';
import { getServerSession } from 'next-auth';
import { NextResponse } from 'next/server';
import { authOptions } from '../auth/[...nextauth]/route';
export async function GET(request: Request) {
const session = getServerSession(authOptions);
if (!session) {
return NextResponse.json(
{ message: 'Please login' },
{
status: 401,
statusText: 'Unauthorized',
}
);
}
const users = await db.user.findMany();
return NextResponse.json(users);
} The key is in passing authOptions to the getServerSession. Also, like it has been noted earlier, clear your cookies. In addition, upgrade your adapters to latest i.e. @auth/your-adapter not @next-auth/your-adapter I hope it helps |
Beta Was this translation helpful? Give feedback.
-
folks let me summarize the solution big thanks to @Marknjo for providing solution -
getServerSession(authOptions) Hope this helps you and again big thanks to @Marknjo |
Beta Was this translation helpful? Give feedback.
-
FYI when I was hitting this error the fix for me was to tell NextAuth to use JWT manually. export const config = {
debug: true,
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID || '',
clientSecret: process.env.GITHUB_SECRET || '',
}),
],
session: {
strategy: "jwt",
maxAge: 30 * 24 * 60 * 60, // 30 days
},
adapter: PrismaAdapter(db),
} satisfies NextAuthOptions; Without that it wouldn't work and I tried tracing through everything. (Clearing cookies, using incognito, etc) Not sure what the issue was but hopefully this helps somebody. |
Beta Was this translation helpful? Give feedback.
-
Have same problem. It turns out the nextauth url is localhost:3000 while i run the nextjs at 3001. turn off app running on 3000 and run nextjs at 3000 solve the problem. |
Beta Was this translation helpful? Give feedback.
-
Question 💬
I recently upgraded my application from NextAuth v3 to v4, earlier only jwt secret was specified, now removed the jwt secret and only specified the secret under options directly.
Everything seems functional but getting following error logs on production:
message: 'Invalid Compact JWE',
'JWEInvalid: Invalid Compact JWE\n' +
at compactDecrypt (/app/node_modules/jose/dist/node/cjs/jwe/compact/decrypt.js:16:15)\n' +
at jwtDecrypt (/app/node_modules/jose/dist/node/cjs/jwt/decrypt.js:8:61)\n' +
at Object.decode (/app/node_modules/next-auth/jwt/index.js:64:34)\n' +
at async Object.session (/app/node_modules/next-auth/core/routes/session.js:41:28)\n' +
at async NextAuthHandler (/app/node_modules/next-auth/core/index.js:96:27)\n' +
at async NextAuthNextHandler (/app/node_modules/next-auth/next/index.js:21:19)\n' +
at async Object.apiResolver (/app/node_modules/next/dist/server/api-utils/node.js:182:9)\n' +
at async NextNodeServer.runApi (/app/node_modules/next/dist/server/next-server.js:386:9)\n' +
at async Object.fn (/app/node_modules/next/dist/server/base-server.js:488:37)\n' +
at async Router.execute (/app/node_modules/next/dist/server/router.js:228:32)',
name: 'JWEInvalid'
Any suggestions from anyone, what else I need to specify in terms of security keys.
How to reproduce ☕️
Contributing 🙌🏽
Yes, I am willing to help answer this question in a PR
Beta Was this translation helpful? Give feedback.
All reactions