Redirect callback example in docs - reversed logic in ternary #931
-
I was struggling with getting a signIn / signOut redirect callback working. After checking the docs, specifically the Redirect Callback example at https://next-auth.js.org/configuration/callbacks I was not seeing the behaviour I expected (which was to redirect the user to /dashboard on login, and to / on logout). It appears that the example in the docs is back to front:
Should be
In the redirect method in the [...nextauth] file, the function signature takes url, baseUrl. In the signIn method we can pass options including callbackUrl, which will appear as the first argument (url) in the redirect method. So when I pass the following to signIn and log url, baseUrl:
/dashboard will be the first argument, and that is where I would like the user to be redirected after signIn. By swapping the order in the ternary statement the signIn / signOut redirects work as expected. When using the ternary example in the docs the signIn / signOut redirects do not work as expected (or at least not intuitively). It's very late and I'm tired so I could be wrong, so I want to add this as a bug first, and if I'm not mistaken then I can make a pull request to update the docs. If I am mistaken, I may make a pull request to add clarity to the docs regarding signIn / signOut redirects, since imo it isn't very clear (and I have noticed a fair few questions regarding auth redirects in the issues). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi there! The docs says:
Meaning that by default, only absolute URLs are handled, that also match (starts with) the base url , all others will go to the To support relative urls, this is what I did in our app: async redirect(url, baseUrl) {
if(url.startsWith(baseUrl) {
return url
}
return new URL(url, baseUrl).toString()
} |
Beta Was this translation helpful? Give feedback.
Hi there! The docs says:
Meaning that by default, only absolute URLs are handled, that also match (starts with) the base url , all others will go to the
baeUrl
or the homepage.To support relative urls, this is what I did in our app: