36 lines
1.1 KiB
36 lines
1.1 KiB
import { Injectable } from '@nestjs/common'; |
|
import { InjectModel } from '@nestjs/sequelize'; |
|
import { User } from './entities/user.entity'; |
|
import * as bcrypt from 'bcrypt'; |
|
import { Response } from 'express'; |
|
|
|
@Injectable() |
|
export class UsersService { |
|
constructor(@InjectModel(User) private readonly userModel: typeof User) {} |
|
|
|
async register(email: string, password: string, res: Response): Promise<Response> { |
|
try { |
|
const hashedPassword = await bcrypt.hash(password, 10); |
|
|
|
const userExists = await this.userModel.findOne({ where: { email } }); |
|
if (userExists) { |
|
return res.status(400).json({ message: 'Email is already taken' }); |
|
} |
|
|
|
const user = await this.userModel.create({ |
|
email, |
|
password: hashedPassword, |
|
}); |
|
|
|
return res.status(201).json({ |
|
message: 'Account created successfully!', |
|
user: { id: user.id, email: user.email }, |
|
}); |
|
} catch (error) { |
|
return res.status(500).json({ |
|
message: 'Failed to create account. Please try again later.', |
|
error: error.message, |
|
}); |
|
} |
|
} |
|
}
|
|
|