-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When using auth wrapper in middleware.ts
, the behavior is not as expected
#11810
Comments
If you are simply trying to make the middleware work then you will have to do some custom work. I created middleware in my application in two ways you can check which one works for you.
Make sure to set
Routes:
To be honest I am not sure why the second approach fails sometimes but these above are the methods I used to handle the middleware. |
It just took me hours to find out that's really a bug. I'm also really expecting the behavior described above. |
I have been stumped by this issue as well. were you able to find workaround by any chance ? |
If you are returning https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/lib/index.ts#L264-L269 |
Environment
Reproduction URL
https://github.com/ks4na/nextjs-dashboard/tree/f-auth-wrapper
Describe the issue
Hello, I’m experiencing some confusion with using auth wrapper in
middleware.ts
.If I don’t use the auth wrapper in middleware.ts and simply
export default auth;
, visiting/dashboard
without logging in correctly redirects me to/login
, as expected based on the logic inauth.config.ts -> authConfig.callbacks.authorized
. However, when using the auth wrapper, I can access/dashboard
without logging in, and thecallbacks.authorized
function does not seem to work as expected.It seems that the issue is related to the implementation in the
handleAuth
function.https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/lib/index.ts#L230-L286
The order of the two
else if
statements might need to be swapped.I'm uncertain whether this behavior is intentional or if it might be a bug.
Additionally, in the first branch, the
authorized
variable could be set totrue
, but it appears that this variable is not used later in thehandleAuth
function, I’m wondering if the logic to "prevent an infinite loop" will still work as intended. I haven’t tested it, just read the code, so please forgive me if I’m wrong.How to reproduce
Reproduction URL
pnpm install && pnpm run dev
/login
page, which is not expected since you should be redirected to/login
.middleware.ts
: comment outexport default auth(async function middleware(req: NextRequest) {
function, and uncommentexport default auth;
line, then save the changes./login
since you haven’t logged in. It’s the expected behavior.Expected behavior
when using
auth wrapper
,callbacks.authorized
works as expected.The text was updated successfully, but these errors were encountered: