Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect after successful sign in or sign up for credentials type in next auth

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)
  }
}
like image 731
Passionate Engineer Avatar asked Nov 27 '20 08:11

Passionate Engineer


2 Answers

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: {},
  }
}
like image 81
Idir Hamouch Avatar answered Oct 03 '22 08:10

Idir Hamouch


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`,
    })
like image 28
Obed Amoasi Avatar answered Oct 03 '22 08:10

Obed Amoasi