嗨,我的项目仍在开发中,我正在测试用户是否可以登录。我的代码 -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";
我检查了所有代码,没有发现任何错误。