mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-02 16:16:39 +08:00
optimize auth
This commit is contained in:
parent
35e7f12d88
commit
b5e41896bf
15
lib/auth.ts
15
lib/auth.ts
@ -9,6 +9,7 @@ import {ADMIN_LIST, isEmail, isName} from "@/lib/auth_list";
|
||||
import {createTransport} from "nodemailer";
|
||||
import { comparePassword, hashPassword } from "@/lib/utils";
|
||||
const SECURE_COOKIES:boolean = !!process.env.SECURE_COOKIES;
|
||||
type PartialUser = Partial<User>;
|
||||
|
||||
|
||||
export const authOptions: NextAuthOptions = {
|
||||
@ -87,7 +88,7 @@ export const authOptions: NextAuthOptions = {
|
||||
// 判断姓名格式是否符合要求,不符合则拒绝
|
||||
if (username && isName(username)) {
|
||||
// Any object returned will be saved in `user` property of the JWT
|
||||
let user:{[key: string]: string} = {}
|
||||
let user: PartialUser = {}
|
||||
if (isEmail(username)) {
|
||||
user['email'] = username;
|
||||
} else {
|
||||
@ -96,13 +97,8 @@ export const authOptions: NextAuthOptions = {
|
||||
// 目前用户不存在,则会创建新用户。
|
||||
let existingUser = await existUser(user); // await insertUser(user)
|
||||
if (!existingUser) {
|
||||
if (await getSetting("allowNewUser")) {
|
||||
// 如果不存在,则创建
|
||||
user['allowToLogin'] = !!await getSetting("allowNewUser");
|
||||
existingUser = await insertUser(user);
|
||||
} else {
|
||||
// 如果不存在,则报错
|
||||
throw new Error("未知用户")
|
||||
}
|
||||
}
|
||||
// 有密码就校验密码,没有就直接返回用户
|
||||
(password || existingUser.password) && validatePassword(password, existingUser.password);
|
||||
@ -235,7 +231,7 @@ async function getSetting(key: string) {
|
||||
}
|
||||
}
|
||||
|
||||
async function existUser(user: {[key: string]: string} | User ) {
|
||||
async function existUser(user: PartialUser ) {
|
||||
const conditions = [];
|
||||
if (user?.name) {
|
||||
conditions.push({ name: user.name });
|
||||
@ -250,7 +246,8 @@ async function existUser(user: {[key: string]: string} | User ) {
|
||||
}) : null
|
||||
}
|
||||
|
||||
export async function insertUser(user: {[key: string]: string}) {
|
||||
export async function insertUser(user: PartialUser ) {
|
||||
console.log('------------', user)
|
||||
try {
|
||||
return await prisma.user.create({
|
||||
data: user
|
||||
|
Loading…
Reference in New Issue
Block a user