From bf6c86862ff56ad76f44f0f241198535f3615769 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Thu, 18 Apr 2024 17:30:27 +0800 Subject: [PATCH] update login --- app/app/(auth)/login/user-login-button.tsx | 18 +++++++++++------- lib/auth.ts | 10 +++++----- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/app/(auth)/login/user-login-button.tsx b/app/app/(auth)/login/user-login-button.tsx index 7b8b258bc..a9fd715ad 100644 --- a/app/app/(auth)/login/user-login-button.tsx +++ b/app/app/(auth)/login/user-login-button.tsx @@ -47,22 +47,26 @@ export default function UserLoginButton() { window.location.href = result?.url && result.url.includes("verify") ? result.url : "/"; } else { - const errorParts = result.error.split(","); - if (errorParts.length > 1) { + if (loginProvider === "credentials") { loginForm.setFields([ { - name: errorParts[0], - errors: [errorParts[1]], + name: "username", + errors: [result.error], }, - ]); - } else { - loginForm.setFields([ { name: "password", errors: [result.error], }, ]); } + if (loginProvider === "email") { + loginForm.setFields([ + { + name: "email", + errors: [result.error], + }, + ]); + } } console.log("response,", result); }); diff --git a/lib/auth.ts b/lib/auth.ts index 828498e3a..275790032 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -99,12 +99,12 @@ export const authOptions: NextAuthOptions = { existingUser = await insertUser(user); } // 有密码就校验密码,没有就直接返回用户 - password && validatePassword(password, existingUser.password); + (password || existingUser.password) && validatePassword(password, existingUser.password); return existingUser; } else { // If you return null then an error will be displayed advising the user to check their details. // return null - throw new Error("username,用户名校验失败") + throw new Error("用户名校验失败") // You can also Reject this callback with an Error thus the user will be sent to the error page with the error message as a query parameter } } @@ -196,11 +196,11 @@ export async function VerifiedAdminUser() { export function validatePassword(password: string, hashPassword: string | null | undefined ): boolean | void { if (!hashPassword) { - throw new Error("password,未设置密码"); + throw new Error("未设置密码"); } if (!comparePassword(password, hashPassword)) { - throw new Error("password,用户名或密码不正确") + throw new Error("用户名或密码不正确") } else { return true; } @@ -235,7 +235,7 @@ export async function insertUser(user: {[key: string]: string}) { // return existingUser; // } } catch (e) { - throw new Error("username,用户创建失败"); + throw new Error("用户创建失败"); // return false; } }