8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

尽管输入的电子邮件和密码有效,但 NextJS 显示 CredentialsSignIn 错误

CharlesB 1月前

9 0

嗨,我的项目仍在开发中,我正在测试用户是否可以登录。我的代码 -routes.ts/** * 可供公众访问的路线数组。这些路线不需要

您好,我的项目仍在开发中,我正在测试用户是否可以登录。

我的代码 -

routes.ts
/**
 * An array of route that are assessible for public. These routes do not require authentication.
 */
export const publicRoutes = ["/"];

/**
 * An array of route that are used for authentication. These routes will redirect loggin users to /settings.
 */
export const authRoutes = ["/auth/login", "/auth/register"];

/**
 * The prefix for API authentication routes. Routes that start with this prefix are used for API authentication purposes.
 */

export const apiAuthPrefix = "/api/auth";

/**
 * The default redirect path after logging in.
 */
export const DEFAULT_LOGIN_REDIRECT = "/settings";
middleware.ts
import authConfig from "../auth.config";
import NextAuth from "next-auth";
import {
  DEFAULT_LOGIN_REDIRECT,
  publicRoutes,
  authRoutes,
  apiAuthPrefix,
} from "../routes";

const { auth } = NextAuth(authConfig);

export default auth((req) => {
  const { nextUrl } = req;
  const isLoggedIn = !!req.auth;

  const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix);
  const isPublicRoute = publicRoutes.includes(nextUrl.pathname);
  const isAuthRoutes = authRoutes.includes(nextUrl.pathname);

  if (isApiAuthRoute) {
    return undefined;
  }

  if (isAuthRoutes) {
    if (isLoggedIn) {
      return Response.redirect(new URL(DEFAULT_LOGIN_REDIRECT, nextUrl));
    }
    return undefined;
  }

  if (!isLoggedIn && !isPublicRoute) {
    return Response.redirect(new URL("/auth/login", nextUrl));
  }
  return undefined;
});

export const config = {
  matcher: [
    "/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",

    "/(api|trpc)(.*)",
  ],
};
auth.ts
`import NextAuth from "next-auth";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { db } from "@/lib/db";
import authConfig from "./auth.config";

export const { auth, handlers, signIn, signOut } = NextAuth({
  adapter: PrismaAdapter(db),
  session: { strategy: "jwt" },
  ...authConfig,
});`

login.ts
"use server";

import * as z from "zod";
import { LoginSchema } from "../schemas";
import { signIn } from "../auth";
import { DEFAULT_LOGIN_REDIRECT } from "../routes";
import { AuthError } from "next-auth";

export const login = async (values: z.infer<typeof LoginSchema>) => {
  const validatedFields = LoginSchema.safeParse(values);

  if (!validatedFields.success) {
    return {
      error: "Invalid fields",
    };
  }

  const { email, password } = validatedFields.data;

  try {
    await signIn("credentials", {
      email,
      password,
      redirectTo: DEFAULT_LOGIN_REDIRECT,
    });
  } catch (error) {
    if (error instanceof AuthError) {
      switch (error.type) {
        case "CredentialsSignin":
          return { error: "Invalide Credentials!" };
        default:
          return { error: "Something went wrong!" };
      }
    }
    throw error;
  }
};

尽管我使用有效的电子邮件和密码进行测试,但仍出现错误:

凭证无效!(在 UI 组件)

[auth][error] CredentialsSignin:更多信息请阅读 https://errors.authjs.dev#credentialssigninat Module.callback (webpack-internal:///(action-browser)/./node_modules/@auth/core/lib/actions/callback/index.js:230:23)
在异步 AuthInternal(webpack-internal:///(action-browser)/./node_modules/@auth/core/lib/index.js:66:24)在异步 Auth(webpack-internal:///(action-browser)/./node_modules/@auth/core/index.js:126:34)在异步 signIn(webpack-internal:///(action-browser)/./node_modules/next-auth/lib/actions.js:51:17)在异步 $$ACTION_0(webpack-internal:///(action-browser)/./actions/login.ts:30:9)在异步 D:\Projects\for-her\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:39:418在异步 rw (D:\Projects\for-her\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:38:7978)在异步 r6 处(D:\Projects\for-her\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:41:1256)在异步 doRender 处(D:\Projects\for-her\node_modules\next\dist\server\base-server.js:1438:30)在异步 cacheEntry.responseCache.get.routeKind 处(D:\Projects\for-her\node_modules\next\dist\server\base-server.js:1599:28)在异步 DevServer.renderToResponseWithComponentsImpl 处(D:\Projects\for-her\node_modules\next\dist\server\base-server.js:1507:28)异步 DevServer.renderPageComponent(D:\Projects\for-her\node_modules\next\dist\server\base-server.js:1931:24)异步 DevServer.renderToResponseImpl(D:\Projects\for-her\node_modules\next\dist\server\base-server.js:1969:32)异步 DevServer.pipeImpl(D:\Projects\for-her\node_modules\next\dist\server\base-server.js:920:25)异步 NextNodeServer.handleCatchallRenderRequest(D:\Projects\for-her\node_modules\next\dist\server\next-server.js:272:17)异步DevServer.handleRequestImpl(D:\Projects\for-her\node_modules\next\dist\server\base-server.js:816:17)异步D:\Projects\for-her\node_modules\next\dist\server\dev\next-dev-server.js:339:20异步Span.traceAsyncFn(D:\Projects\for-her\node_modules\next\dist\trace\trace.js:154:20)异步DevServer.handleRequest(D:\Projects\for-her\node_modules\next\dist\server\dev\next-dev-server.js:336:24)异步invokeRender(D:\Projects\for-her\node_modules\next\dist\server\lib\router-server.js:174:21)异步handleRequest (D:\Projects\for-her\node_modules\next\dist\server\lib\router-server.js:353:24)在异步 requestHandlerImpl 处(D:\Projects\for-her\node_modules\next\dist\server\lib\router-server.js:377:13)在异步 Server.requestListener 处(D:\Projects\for-her\node_modules\next\dist\server\lib\start-server.js:141:13)

我尝试从导入 signIn

import { signIn } from "next-auth/react";

我尝试从导入 AuthErrors import { AuthError } from "@auth/core/errors";

我检查了所有代码,没有发现任何错误。

帖子版权声明 1、本帖标题:尽管输入的电子邮件和密码有效,但 NextJS 显示 CredentialsSignIn 错误
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由CharlesB在本站《authentication》版块原创发布, 转载请注明出处!
最新回复 (0)
返回
作者最近主题: