Is there any examples and/or way to redirect to private dashboard on successful sign in or sign up for credentials type in next-auth
?
I couldn't find any clear docs around this.
I was looking at adding redirect below but wasn't sure if it was the right approach:
callbacks.signIn = async (data, account, profile) => {
if ((account.provider === 'google' && profile.verified_email === true) || (account.type === 'credentials' && data.status === 200)) {
return Promise.resolve(true)
} else {
return Promise.resolve(false)
}
}
With the new versions of Next.js you can do the redirect on 'getStaticProps' method like the following,
Resource : https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation
export async function getStaticProps(context) {
// some imperative work..
//
if (!user) {
return {
redirect: {
destination: '/', // some destination '/dashboard' Ex,
permanent: false,
},
}
}
return {
props: {},
}
}
This can actually happen when initiating the signin. From the docs., you can pass a callback URL to the sign in. you code will look like this.
signIn(provider.id, {
callbackUrl: `${window.location.origin}/protected`,
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With