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 just going back to the index.js
index.js
<main >
{!session && <>
<h1>You are not signed in</h1> <br/>
<button onClick={signIn}>Sign in</button>
</>}
{session && <>
<h1>Signed in as {session.user.name} </h1> <br/>
<button onClick={signOut}>Sign out</button>
</>}
</main>
[...nextauth].js
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
const options = {
providers: [
Providers.Credentials({
// The name to display on the sign in form (e.g. 'Sign in with...')
name: 'UserName',
// The credentials property is used to generate a suitable form on the sign in page.
credentials: {
username: { label: "Username", type: "text", },
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
// Authentication Logic: local function, external API call, etc
const user = { name: credentials.username, password: credentials.password }
//checking the credititials
if( user.name!="admin" || user.password!="admin"){
return null;
}else{
return user;
}
}
})
],
session: {
jwt: true,
}
/*jwt: {
secret: 'INp8IvdIyeMcoGAgFGoA61DdBglwwSqnXJZkgz8PSnw',
} */
}
export default (req, res) => NextAuth(req, res, options);
When calling signIn
, you can pass an object as the second parameter which includes a callbackUrl
which defines where the user will be redirected to after login.
<button onClick={() => signIn('yourProviderHere', { callbackUrl: '/dashboard' })}>Sign in</button>
Documentation for callbackUrl here: https://next-auth.js.org/getting-started/client#specifying-a-callbackurl