如何解决使用自定义登录和后端实现 NextAuth 凭据时出错
我正在使用 NextJS(GraphQL) 应用程序实现 NextAuth。我有这个错误:
Unhandled Runtime Error
TypeError: Cannot use 'in' operator to search for 'credentials' in null
网络错误还显示:
错误:/api/auth/session 不支持 HTTP GET
错误:/api/auth/providers 不支持 HTTP GET
我不会在我知道的任何地方提出 GET
请求。我的代码如下:
pages/api/[...nextauth.js]
const providers = [
Providers.Credentials({
name: 'Credentials',authorize: async (credentials) => {
try {
const user = await <GraphQL Logic to fetch user>
if (user) {
return {status: 'success',data: user.data.user}
}
} catch (e) {
const errorMessage = e.response.data.message
// Redirecting to the login page with error messsage in the URL
throw new Error(errorMessage + '&email=' + credentials.email)
}
}
})
]
const callbacks = {
async jwt(token,user) {
if (user) {
token.accessToken = user.data.token
}
return token
},async session(session,token) {
session.accessToken = token.accessToken
return session
}
}
const options = {
providers,callbacks,pages: {
error: '/login' // Changing the error redirect page to our custom login page
}
}
export default (req,res) => NextAuth(req,res,options)
pages/login.js
<form onSubmit={(e) => handleLogin(e)}>
<input value={email} onChange={(e) => setEmail(e.target.value)} />
<input type='password' value={password} onChange={(e) => setPassword(e.target.value)} />
<button type='submit' disabled={isLoginStarted}>Log In</button>
</form>
handleLogin method
const handleLogin = (e) => {
e.preventDefault()
setIsLoginStarted(true)
signIn('credentials',{
email,password,callbackUrl: `${window.location.origin}/welcome`
}
)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。