You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Issue a redirect in the authorized callback does not work. I've noted that it will works by adjusting the code in one of two ways. Either by adjusting the middleware or by not overriding the authorized callback, but if you need to apply middleware and override the authorized callback I think you're stuck.
export{authasmiddleware}from'@/auth'// Optionally, don't invoke Middleware on some pathsexportconstconfig={/* * Match all routes except for the following: * - api/* (API routes) * - _next/static/* (static files) * - _next/image* (image optimization files) * - favicon.ico * - robots.txt * - home page (root route) */matcher: ['/((?!api|_next/static|_next/image|images|fonts|favicon.ico|robots.txt|$).*)',],}
importNextAuthfrom'next-auth'importCredentialsfrom'next-auth/providers/credentials'import{typeProvider}from'next-auth/providers'constproviders: Provider[]=[Credentials({name: 'credentials',credentials: {email: {label: 'Email',type: 'email',placeholder: 'smith'},password: {label: 'Password',type: 'password'},},asyncauthorize(credentials){constusers=[{id: '1',email: '[email protected]',name: 'test',password: 'pass',},]constuser=users.find((user)=>user.email===credentials?.email&&user.password===credentials?.password)returnuser ? {id: user.id,email: user.email,name: user.name} : null},}),]exportconst{ auth, handlers, signIn, signOut }=NextAuth({session: {strategy: 'jwt',maxAge: 5,// 5 seconds for testing},
providers,callbacks: {authorized: async({ auth })=>{// Logged in users are authenticated, otherwise redirect to login page based on the middleware matcherreturn!!auth},},})
Working with middleware matcher adjustment
This is less ideal because it require the page to not be protected by middleware, but I'm showing it for completeness. The change is to modify the matcher to include the previously protected page dashboard. This results in the server action being invoked which contains the necessary protection and redirect.
export{authasmiddleware}from'@/auth'// Optionally, don't invoke Middleware on some pathsexportconstconfig={/* * Match all routes except for the following: * - api/* (API routes) * - _next/static/* (static files) * - _next/image* (image optimization files) * - favicon.ico * - robots.txt * - home page (root route) */matcher: ['/((?!api|_next/static|_next/image|images|fonts|favicon.ico|robots.txt|dashboard$).*)',],}
Working without the authorized callback override
This is more ideal since it allows the page to be protected by middleware. I've removed the authorized callback in the auth.ts file. The down side is that this will not work if you need to override the authorized callback.
The text was updated successfully, but these errors were encountered:
ralphsmith80
added
bug
Something isn't working
triage
Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
labels
Jan 22, 2025
Environment
Reproduction URL
https://github.com/ralphsmith80/authjs-authorized-callback/blob/main/src/auth.ts#L39
Describe the issue
Issue a
redirect
in theauthorized
callback does not work. I've noted that it will works by adjusting the code in one of two ways. Either by adjusting the middleware or by not overriding theauthorized
callback, but if you need to apply middleware and override theauthorized
callback I think you're stuck.Also located in the repo here
Breaking test case
Working with middleware matcher adjustment
This is less ideal because it require the page to not be protected by middleware, but I'm showing it for completeness. The change is to modify the matcher to include the previously protected page
dashboard
. This results in the server action being invoked which contains the necessary protection and redirect.Working without the
authorized
callback overrideThis is more ideal since it allows the page to be protected by middleware. I've removed the
authorized
callback in theauth.ts
file. The down side is that this will not work if you need to override theauthorized
callback.Working with middleware matcher and
authorized
callbackThis is the solution if you want your matcher protect that page and you want to override the
authorized
callback.This does not work. How can we make this work?
How to reproduce
[email protected]
,pass
authorized
callback5
,6
, and7
Expected behavior
The user should be redirected to the sign in page.
Actual behavior
A missing CSRF error is logged in the server console.
The text was updated successfully, but these errors were encountered: