如何解决在自定义登录页面上抛出错误时,下一个身份验证“凭据”重定向
我有一个自定义登录页面,它会在提交表单时调用 signIn()
函数。
我只使用“凭据” 提供程序。
服务器端,我只是想抛出一个错误,以便我可以在前端处理它。看起来很容易。
我继续收到一条错误消息:Error: HTTP GET is not supported for /api/auth/login?callbackUrl=http://localhost:4000/login
我被重定向到的网址是:http://localhost:4000/api/auth/login?callbackUrl=http://localhost:4000/login
这是我的代码: pages/login.js(仅相关代码。其余只是布局。)
<form
method="post"
onSubmit={() =>
signIn("credentials",{
email: "test",password: "test",})
}
>
<label>
Username
<input type="email" />
</label>
<label>
Password
<input name="password" type="password" />
</label>
<button type="submit">Sign In</button>
</form>
pages/api/auth/[...nextauth.js]
import NextAuth from "next-auth";
import Providers from "next-auth/providers";
const options = {
site: process.env.NEXTAUTH_URL,providers: [
Providers.Credentials({
id: "chatter",name: "Credentials",type: "credentials",credentials: {
email: { label: "Email",type: "email",placeholder: "email@domain.com" },password: { label: "Password",type: "password" },},authorize: async credentials => {
console.log("credentials:",credentials);
throw new Error("error message"); // Redirect to error page
},}),],pages: {
signIn: "login",newUser: null,};
export default (req,res) => NextAuth(req,res,options);
解决方法
您是否检查 pages/api/auth 中的 [...nextauth].js?
我遇到了同样的问题,但我可以修复 [...nextauth].js 将“pages/api”移动到“pages/api/auth”。
,你可以这样使用:
signIn("credentials",{
redirect: false,email: "test",password: "test",})
.then((error) => console.log(error))
.catch((error) => console.log(error));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。