code refractor
This commit is contained in:
6
src/services/Map.ts
Normal file
6
src/services/Map.ts
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
class MapService {
|
||||
|
||||
}
|
||||
|
||||
export default MapService;
|
35
src/services/User.ts
Normal file
35
src/services/User.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import bcrypt from "bcryptjs";
|
||||
import prisma from "../helpers/prisma";
|
||||
import UserRepository from "../repositories/User";
|
||||
|
||||
class UserService {
|
||||
async createUser(username: string, password: string): Promise<void> {
|
||||
try {
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
username,
|
||||
password: hashedPassword,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
// Handle error
|
||||
throw new Error(`Failed to create user: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async validateUserCredentials(username: string, password: string): Promise<boolean> {
|
||||
try {
|
||||
const user = await UserRepository.getByUsername(username);
|
||||
|
||||
if (!user) return false;
|
||||
|
||||
return bcrypt.compareSync(password, user.password);
|
||||
} catch (error) {
|
||||
// Handle error
|
||||
throw new Error(`Failed to validate user credentials: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new UserService;
|
Reference in New Issue
Block a user