server/src/app/services/user.service.ts

30 lines
864 B
TypeScript

import bcrypt from "bcryptjs";
import UserRepository from "../repositories/user.repository";
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;