forked from noxious/server
use camelcase file names from now on...
This commit is contained in:
37
src/services/userService.ts
Normal file
37
src/services/userService.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import bcrypt from 'bcryptjs'
|
||||
import UserRepository from '../repositories/userRepository'
|
||||
import prisma from '../utilities/prisma'
|
||||
import { User } from '@prisma/client'
|
||||
|
||||
class UserService {
|
||||
async login(username: string, password: string): Promise<boolean | User> {
|
||||
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 | User> {
|
||||
const user = await UserRepository.getByUsername(username)
|
||||
if (user) {
|
||||
return false
|
||||
}
|
||||
|
||||
const hashedPassword = await bcrypt.hash(password, 10)
|
||||
return prisma.user.create({
|
||||
data: {
|
||||
username,
|
||||
password: hashedPassword
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default UserService
|
Reference in New Issue
Block a user