2025-03-31 19:43:56 +08:00
|
|
|
import type { User } from '@prisma/client';
|
|
|
|
|
2025-02-09 10:39:10 +08:00
|
|
|
import db from '@/libs/db/prismaClient';
|
|
|
|
import { hashPassword, verifyPassword } from '@/libs/password';
|
|
|
|
import { isNil, omit } from 'lodash';
|
2025-01-24 14:26:30 +08:00
|
|
|
|
|
|
|
import type { createUserType } from './type';
|
|
|
|
|
2025-03-31 19:43:56 +08:00
|
|
|
export async function getUserByEmail(
|
|
|
|
email: string,
|
|
|
|
): Promise<Pick<User, 'id' | 'email' | 'username' | 'password'> | null> {
|
2025-01-24 14:26:30 +08:00
|
|
|
const user = await db.user.findFirst({
|
|
|
|
where: { email },
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
email: true,
|
|
|
|
username: true,
|
|
|
|
password: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (isNil(user)) return null;
|
|
|
|
return user;
|
2025-03-31 19:43:56 +08:00
|
|
|
}
|
|
|
|
export async function getUserById(id: string): Promise<User | null> {
|
2025-01-24 14:26:30 +08:00
|
|
|
const user = await db.user.findUnique({ where: { id } });
|
|
|
|
|
|
|
|
return user;
|
2025-03-31 19:43:56 +08:00
|
|
|
}
|
|
|
|
export async function getUserByusername(username: string): Promise<User | null> {
|
|
|
|
return await db.user.findFirst({ where: { username } });
|
|
|
|
}
|
2025-01-24 14:26:30 +08:00
|
|
|
|
2025-03-31 19:43:56 +08:00
|
|
|
export async function createUser(data: createUserType): Promise<Omit<User, 'password'>> {
|
2025-01-24 14:26:30 +08:00
|
|
|
const { username, email, password } = data;
|
|
|
|
|
|
|
|
const user = await db.user.create({
|
|
|
|
data: {
|
|
|
|
email,
|
2025-02-09 10:39:10 +08:00
|
|
|
password: hashPassword(password),
|
2025-01-24 14:26:30 +08:00
|
|
|
username,
|
|
|
|
},
|
|
|
|
});
|
2025-02-09 10:39:10 +08:00
|
|
|
return omit(user, ['password']);
|
2025-03-31 19:43:56 +08:00
|
|
|
}
|
2025-01-24 14:26:30 +08:00
|
|
|
|
2025-03-31 19:43:56 +08:00
|
|
|
export async function ValidateUser(
|
2025-02-09 10:39:10 +08:00
|
|
|
credential: string,
|
|
|
|
password: string,
|
|
|
|
): Promise<
|
|
|
|
| { success: false; message: string; user: null }
|
|
|
|
| {
|
|
|
|
success: true;
|
|
|
|
message: string;
|
|
|
|
user: {
|
|
|
|
id: string;
|
|
|
|
email: string;
|
|
|
|
username: string;
|
|
|
|
role: 'ADMIN' | 'USER';
|
|
|
|
createdAt: Date;
|
|
|
|
updatedAt: Date;
|
|
|
|
};
|
|
|
|
}
|
2025-03-31 19:43:56 +08:00
|
|
|
> {
|
2025-01-24 14:26:30 +08:00
|
|
|
const user = await db.user.findFirst({
|
|
|
|
where: {
|
|
|
|
OR: [{ email: credential }, { username: credential }],
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
email: true,
|
|
|
|
username: true,
|
2025-02-09 10:39:10 +08:00
|
|
|
role: true,
|
2025-01-24 14:26:30 +08:00
|
|
|
password: true,
|
2025-02-09 10:39:10 +08:00
|
|
|
createdAt: true,
|
|
|
|
updatedAt: true,
|
2025-01-24 14:26:30 +08:00
|
|
|
},
|
|
|
|
});
|
2025-03-31 19:43:56 +08:00
|
|
|
if (isNil(user)) {
|
2025-01-24 14:26:30 +08:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: '用戶不存在',
|
|
|
|
user: null,
|
|
|
|
};
|
2025-03-31 19:43:56 +08:00
|
|
|
}
|
2025-01-24 14:26:30 +08:00
|
|
|
|
|
|
|
const ispasswordValid = verifyPassword(password, user.password);
|
2025-03-31 19:43:56 +08:00
|
|
|
if (!ispasswordValid) {
|
2025-01-24 14:26:30 +08:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: '密碼錯誤',
|
2025-02-09 10:39:10 +08:00
|
|
|
user: null,
|
2025-01-24 14:26:30 +08:00
|
|
|
};
|
2025-03-31 19:43:56 +08:00
|
|
|
}
|
2025-01-24 14:26:30 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
message: '登入成功',
|
|
|
|
user: omit(user, ['password']),
|
|
|
|
};
|
2025-03-31 19:43:56 +08:00
|
|
|
}
|
|
|
|
export async function uniqueEmailValidator(email: string): Promise<boolean> {
|
2025-01-24 14:26:30 +08:00
|
|
|
if (isNil(email)) return true;
|
|
|
|
const user = await getUserByEmail(email);
|
|
|
|
if (isNil(user)) return true;
|
|
|
|
return false;
|
2025-03-31 19:43:56 +08:00
|
|
|
}
|
|
|
|
export async function uniqueUsernameValidator(username: string): Promise<boolean> {
|
2025-01-24 14:26:30 +08:00
|
|
|
if (isNil(username)) return true;
|
|
|
|
const user = await getUserByusername(username);
|
|
|
|
if (isNil(user)) return true;
|
|
|
|
return false;
|
2025-03-31 19:43:56 +08:00
|
|
|
}
|