Closed rustyspottedcatt closed 2 weeks ago
Hey @rustyspottedcatt!
Thanks for reporting. Looks like the same issue as https://github.com/awinogrodzki/next-firebase-auth-edge/issues/242
It seems the TextEncoder
did not help to solve the issue.
Also, it seems to be connected with JOSE implementation in Windows.
Let me look for some answers and I'll get back to you
@rustyspottedcatt can I ask you to debug the authentication cookie in https://jwt.io/ and tell me the value of headers
?
It should look like this:
{
"alg": "HS256",
"typ": "JWT"
}
jwtVerify
method should be called with a token signed with HS256
algorithm. For some reason jose interprets the token as RS256
.RS256
algorithm requires asymmetric key in the form of KeyObject
, hence the error.
Knowing the header would help me identify if there's issue in jwtVerify
method or wrong algorithm is used before setting authentication cookies
Also, please let me know if the authentication cookie contains :
character when you paste it to https://jwt.io
What is the value of enableMultipleCookies
that you pass in middleware? Have you changed this option from true
to false
?
There are two specific scenarios where user can fall into this error:
Scenario 1:
enableMultipleCookies: true
enableMultipleCookies: false
${cookieName}.sig
and ${cookieName}.custom
headers, but keeps ${cookieName}
headerScenario 2:
enableMultipleCookies: true
${cookieName}.sig
and ${cookieName}.custom
headers, but leaves only ${cookieName}
headerI am not sure if that's the case you did hit. Have you tried to remove the cookies and log in again? Do you keep getting the same result?
I have prepared a fix for the cases I mentioned: https://github.com/awinogrodzki/next-firebase-auth-edge/pull/256/files
It will redirect to handleInvalidToken
and log a descriptive debug message when user with multiple cookies tries to enter single cookie flow
Also seeing the same issue. When I debugged the token I see
{
"alg": "RS256",
"kid": "_aLBDQ"
}
To create the session cookie, I just auth.createSessionCookie
(auth
being firebase-admin
auth()`
I'm on the latest version 1.7.1
Hey @giulioco,
The library is not compatible with auth.createSessionCookie
. The library creates, manages and validates session cookie in format that differs from the one returned by auth.createSessionCookie
. I might add createSessionCookie
support in future versions
Hello @awinogrodzki I'm currently having this issue, any solutions yet?
Hey @bySegunMoses,
Could you run npx next info
and share the output here?
What version of the library are you using?
Could you share your middleware.ts
file?
Are you using enableMultipleCookies
option?
Could you share the stack trace of the error that you're having?
I will close the issue now due to lack of responsiveness.
If anyone else encounters this issue, please create a new one and provide all the details mentioned in previous comment to speed up the resolution
Description:
I’m encountering a
TypeError
when attempting to verify cookies using theRS256
algorithm in a Next.js middleware. The error occurs when I passcookieSignatureKeys
to theauthMiddleware
.Error Stack:
What I have tried:
import { authMiddleware, redirectToLogin } from "next-firebase-auth-edge"; import type { NextRequest } from "next/server";
export async function middleware(request: NextRequest): Promise {
try {
return authMiddleware(request, {
loginPath: "/api/login",
logoutPath: "/api/logout",
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY as string,
cookieName: "AuthToken",
enableMultipleCookies: false,
debug: true,
cookieSignatureKeys: [
process.env.COOKIE_SECRET_CURRENT!,
process.env.COOKIE_SECRET_PREVIOUS!,
],
cookieSerializeOptions: {
path: "/",
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
maxAge: 24 60 60 * 1000,
},
serviceAccount: {
...JSON.parse(
JSON.stringify(process.env.NEXT_PRIVATE_FIREBASE_ACCOUNT) as string
),
},
handleError: async (error) => {
console.error("Middleware error:", error);
return redirectToLogin(request, {
path: '/login',
publicPaths: ['/login', '/signup'],
})
},
} catch (error) { console.error("Middleware error:", error); return new Response("Internal Server Error", { status: 500 }); } }
export const config = { matcher: ["/dashboard"], };