首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何处理NextAuth.js中的登录失败错误?

如何处理NextAuth.js中的登录失败错误?
EN

Stack Overflow用户
提问于 2021-11-30 07:57:18
回答 1查看 5.5K关注 0票数 7

我使用NextAuth.js进行Next.js身份验证。登录很好,但是页面仍然在错误的凭据上重新加载。它没有显示出任何错误。我需要处理错误来显示某种祝酒词。

代码语言:javascript
运行
复制
signIn("credentials", {
      ...values,
      redirect: false,
    })
      .then(async () => {
        await router.push("/dashboard");
      })
      .catch((e) => {
        toast("Credentials do not match!", { type: "error" });
      });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-18 18:52:04

当将redirect: false传递给其选项时,signIn将返回一个始终解析为具有以下格式的对象的Promise

代码语言:javascript
运行
复制
{
    error: string | undefined // Error code based on the type of error
    status: number // HTTP status code
    ok: boolean // `true` if the signin was successful
    url: string | null // `null` if there was an error, otherwise URL to redirected to
}

您必须处理then块中的任何错误,因为它不会抛出错误。

代码语言:javascript
运行
复制
signIn("credentials", { ...values, redirect: false })
    .then(({ ok, error }) => {
        if (ok) {
            router.push("/dashboard");
        } else {
            console.log(error)
            toast("Credentials do not match!", { type: "error" });
        }
    })
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70165993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档