30 lines
853 B
TypeScript
30 lines
853 B
TypeScript
import bcrypt from "bcryptjs";
|
|
import UserRepository from "../repositories/user";
|
|
|
|
class UserService {
|
|
async login(username: string, password: string): Promise<boolean | any> {
|
|
const user = await UserRepository.getByUsername(username);
|
|
if (!user) {
|
|
return false;
|
|
}
|
|
|
|
const passwordMatch = await bcrypt.compare(password, user.password);
|
|
if (!passwordMatch) {
|
|
return false;
|
|
}
|
|
|
|
return user;
|
|
}
|
|
|
|
async register(username: string, password: string): Promise<boolean | any> {
|
|
const user = await UserRepository.getByUsername(username);
|
|
if (user) {
|
|
return false;
|
|
}
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
return await UserRepository.create(username, hashedPassword);
|
|
}
|
|
}
|
|
|
|
export default UserService; |