Middleware Error on Production (No Secret) #4414
Replies: 14 comments 20 replies
-
I have noticed this issue as well. |
Beta Was this translation helpful? Give feedback.
-
I also believe this issue to be related to #3878 |
Beta Was this translation helpful? Give feedback.
-
And, this issue is caused by an error in next.js, which is fixed as of 12.1.4 |
Beta Was this translation helpful? Give feedback.
-
Thanks, @promet99 for looking into it 🙏. Could you try upgrading next.js to the latest version and see if the error persists @batuhanbilginn? |
Beta Was this translation helpful? Give feedback.
-
I was bumping into this issue myself and can tell that upgrading both next & next-auth makes error go away indeed. But by some trial and error I've figured that I can't use the middleware way of securing api routes with db strategy(prisma in my case). Is there any other sensible option to still use middleware and prevent unauthenticated user from accessing the routes? Thinking of Thanks in advance! |
Beta Was this translation helpful? Give feedback.
-
@d-ivashchuk Currently, only JWT strategy is supported for middleware. See: https://next-auth.js.org/configuration/nextjs#caveats
It won't work, unfortunately 😢 |
Beta Was this translation helpful? Give feedback.
-
that was what I was the most afraid of :/ any workarounds that you might think of? Is there an issue or PR to follow/contribute to make the middleware working with DB strategy? |
Beta Was this translation helpful? Give feedback.
-
This issue is still happening to me. I am using |
Beta Was this translation helpful? Give feedback.
-
The issue seems to be in the default export function from 'next-auth/middleware because it doesn't read the same secret defined in [...nextauth].js. The docs states that the secret is the same used in the [...nextauth].js file and that the default value is the NEXTAUTH_SECRET env var, that's confusing. The workaround I've found so far is to read the secret in both places using an environment variable. [...nextauth].jsexport const authOptions = {
# ... config
secret: process.env.SECRET
}
export default NextAuth(authOptions); middleware.jsimport { withAuth } from "next-auth/middleware";
export default withAuth({
secret: process.env.SECRET,
});
export const config = {
matcher: ["/"],
}; hope it helps |
Beta Was this translation helpful? Give feedback.
-
I am still running into the same issue or at least I think it related to middleware not running properly. For me the solution of @rcmarc didn't work, even though I thought this would be the fix. For me the problem is every route gets redirected back to login with 307 status, even though the session and the token are set. Everything is running fine locally. My configs are as follows: middleware.ts
[...nextauth].ts
I can't see the log of the authorized callback either which is why I think the problem has to do with the middleware. The log is shown locally as well. |
Beta Was this translation helpful? Give feedback.
-
Similar issue. .env
[...nextauth].ts
middleware.ts
Getting no secret error when deployed on vercel platform (locally - no issue in production mode). As a workaround solution - manually adding NEXTAUTH_SECRET env variable on vercel and redeploying... |
Beta Was this translation helpful? Give feedback.
-
Adding NEXTAUTH_SECRET on vercel form (and not on .env) solved the NO_SECRET I was facing on every deploy to production. Cannot tell who is wrong here: vercel not parsing .env files or NextAuth stating on docs that vercel does this. |
Beta Was this translation helpful? Give feedback.
-
For deploy nextjs app with nextauth on vercel followed with the following steps:
I'm using middleware
It works for me |
Beta Was this translation helpful? Give feedback.
-
On my side I found that the problem was because |
Beta Was this translation helpful? Give feedback.
-
Environment
System:
OS: macOS 12.3
CPU: (10) x64 Apple M1 Pro
Memory: 24.96 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.14.0 - /usr/local/bin/node
npm: 8.3.1 - /usr/local/bin/npm
Browsers:
Chrome: 100.0.4896.88
Firefox: 97.0.1
Safari: 15.4
npmPackages:
next: latest => 12.0.8
next-auth: ^4.3.2 => 4.3.2
react: ^17.0.2 => 17.0.2
Reproduction URL
Describe the issue
I use this code block to detect if the user is authenticated or not. It works perfectly on local but it doesn't work on the production.
The error is "No Secret" but I already provided the NEXTAUTH_SECRET environment variable to the Vercel and I can verify that with successful login and cookie creation. Somehow, Middleware can't access the variable or there is something else.
How to reproduce
You can push your app with a middleware setup and try to use the code block that I provided or from the documentation.
Expected behavior
The code block should detect if the user is logged in or not.
Beta Was this translation helpful? Give feedback.
All reactions