如何解决NextAuth登录成功后如何返回页面
我想在登录成功时返回特定页面,例如当用户提供正确的凭据时,他们应该被重定向到特定页面让我们说dashboard.js 而不是仅仅返回 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);
解决方法
在调用 signIn
时,您可以传递一个对象作为第二个参数,其中包含一个 callbackUrl
,它定义了用户登录后将被重定向到的位置。
<button onClick={() => signIn('yourProviderHere',{ callbackUrl: '/dashboard' })}>Sign in</button>
此处为 callbackUrl 的文档:https://next-auth.js.org/getting-started/client#specifying-a-callbackurl
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。