Complete refractor - we do sexy code only
This commit is contained in:
30
src/app/services/user.ts
Normal file
30
src/app/services/user.ts
Normal file
@ -0,0 +1,30 @@
|
||||
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;
|
Reference in New Issue
Block a user