You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next-auth中Credential Provider的authorize函数日志不显示求助

自定义登录页调用signIn后,Credential Provider的authorize函数日志无输出排查

我在自定义登录页调用signIn方法执行登录操作,但Credential Provider的authorize函数中的console.log语句在浏览器控制台和终端都找不到输出,请求协助排查问题。

相关代码片段

登录页提交函数

const onSubmit = async (e) => {
    e.preventDefault();
    try {
      const res = await signIn("credentials", {
        redirect: false,
        email: "hello@gmail.com",
        password: "1234werwer",
        callbackUrl:`${window.location.origin}`       
      });
      console.log("await", res);
      setAlert({ status: "success", message: "Login successfully" });
      setLoginData({ email: "", password: "" });
    } catch (error) {
      console.log({ error });
      setAlert({ status: "error", message: "Something went wrong" });
    }
  };

路由文件片段

const authOptions = {
  secret: "hehe,
  pages: {
    signIn: "/login",
  },
  providers: [
    CredentialsProvider({
      // The name to display on the sign in form (e.g. 'Sign in with...')
      name: "my-project",
      async authorize(credentials, req) {
        console.log("authorize>>>>>>>")
        const user = await fetch("/api/auth/callback", {})
        return {
          id: "2",
          email: credentials?.email,
        };
      },
      credentials: {
        email: {
          label: "email",type: "email",
          placeholder: "jsmith@example.com",
        },
        password: {
          label: "Password",
          type: "password"
        },
      }
    }),
  ],

  callbacks: {
//....
}

next.config文件内容

/** @type {import('next').NextConfig} */
const nextConfig = {};

module.exports = nextConfig;

我已多方搜索仍未找到问题原因。

排查要点

  • 修复secret语法错误:你的authOptions.secret值写为"hehe,,缺少闭合双引号,会直接导致NextAuth初始化失败,authorize函数根本不会执行。先将secret修正为合法字符串,比如"hehe"。
  • 确认NextAuth路由配置:确保在pages/api/auth/[...nextauth].js(App Router环境下为app/api/auth/[...nextauth]/route.js)中正确导出authOptions,并通过NextAuth(authOptions)创建路由处理函数。路由配置错误会导致前端signIn请求无法触发后端authorize逻辑。
  • 聚焦服务器端日志位置:authorize函数运行在服务器端,日志只会输出在启动Next.js的终端窗口中,不会出现在浏览器控制台。检查终端是否正常运行,是否过滤了日志内容。
  • 移除冗余fetch请求:在authorize内部调用fetch("/api/auth/callback")属于无意义操作,甚至可能引发循环请求,干扰正常登录逻辑,建议删除该行代码。
  • 验证signIn参数匹配性:signIn("credentials")中的参数是provider的id,CredentialsProvider默认id为"credentials",与你的调用一致;若手动指定过id,需确保与调用参数完全匹配。
  • 检查版本兼容性:确认next-auth版本与Next.js版本兼容(如NextAuth v4要求Next.js 12及以上版本),版本不兼容可能导致异常逻辑。

内容的提问来源于stack exchange,提问作者Saif Haq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 16:01:36