Next-auth.js: Redirect to custom URL after successful login.

Hey all, I'm trying to figure out how to redirect after a successful login to a custom url. Specifically, I want to point to localhost/home instead of just the default localhost. Thank you!
4 Replies
Codex
Codex11mo ago
You could probably force them out of it with a router.push("/home") not sure if its the best practice though
Wiznet
WiznetOP11mo ago
yeah i could maybe do that on my base layout page..but was hoping i could control the callback flow a bit more as im just using their sign in page
Alky
Alky11mo ago
Can you show the code for what you are doing right and then explain what's the behavior you want? Because i didn't get it. Isn't just get the signIn response, check if it's succesful and then send the user to whatever URL you want? Something like this:
const loginAttempt = await signIn('credentials', { redirect: false, ...data });
if (loginAttempt?.error) // Handle Error
// customLogicAndPushToCustomURL();
const loginAttempt = await signIn('credentials', { redirect: false, ...data });
if (loginAttempt?.error) // Handle Error
// customLogicAndPushToCustomURL();
Or inside pages/api/auth/[...nextauth].js Change the redirect callback
Louis
Louis11mo ago
This response on stack overflow might be what you're looking for? https://stackoverflow.com/questions/66780188/how-to-return-a-page-when-login-is-success-in-nextauth
Stack Overflow
How to return a Page when login is success in NextAuth
I want to return to a specific page when the login is success for an example when a user provides the correct credentials they should be redirected to a specific page lets say dashboard.js instead of
Want results from more Discord servers?
Add your server