|
|
|
@ -20,25 +20,33 @@ export class UsersService { |
|
|
|
|
async register(createUserDto: CreateUserDto): Promise<{ message: string }> { |
|
|
|
|
try { |
|
|
|
|
createUserDto.password = await bcrypt.hash(createUserDto.password, parseInt(process.env.BCRYPT_SALT_ROUNDS || "10", 10)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const emailExists = await this.userModel.findOne({ |
|
|
|
|
where: { email: createUserDto.email }, |
|
|
|
|
}); |
|
|
|
|
if (emailExists) { |
|
|
|
|
throw new BadRequestException("Email is already registered."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const phoneExists = await this.userModel.findOne({ |
|
|
|
|
where: { phoneNumber: createUserDto.phoneNumber }, |
|
|
|
|
}); |
|
|
|
|
if (phoneExists) { |
|
|
|
|
throw new BadRequestException("Phone number is already registered."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const usernameExists = await this.userModel.findOne({ |
|
|
|
|
where: { username: createUserDto.username }, |
|
|
|
|
}); |
|
|
|
|
if (usernameExists) { |
|
|
|
|
throw new BadRequestException("Username is already registered."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
await this.userModel.create(createUserDto); |
|
|
|
|
const user = await this.userModel.findOne({ |
|
|
|
|
where: { email: createUserDto.email }, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const refreshToken = this.jwtService.sign( |
|
|
|
|
{ id: user.id }, |
|
|
|
|
{ |
|
|
|
@ -46,12 +54,12 @@ export class UsersService { |
|
|
|
|
expiresIn: this.configService.get<string | number>("JWT_REFRESH_EXPIRES") || "7d", |
|
|
|
|
}, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await this.userModel.update( |
|
|
|
|
{ refreshToken }, //
|
|
|
|
|
{ refreshToken }, |
|
|
|
|
{ where: { id: user.id } }, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { message: "User registered successfully." }; |
|
|
|
|
} catch (error) { |
|
|
|
|
if (error instanceof HttpException) { |
|
|
|
@ -61,14 +69,14 @@ export class UsersService { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// Login method
|
|
|
|
|
async login(loginUserDto: LoginUserDto): Promise<{ accessToken: string }> { |
|
|
|
|
async login(loginUserDto: LoginUserDto): Promise<{ accessToken: string , refreshToken:string}> { |
|
|
|
|
try { |
|
|
|
|
const user = await this.userModel.findOne({ |
|
|
|
|
where: { email: loginUserDto.email }, |
|
|
|
|
where: { email: loginUserDto.email, username:loginUserDto.username }, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!user) { |
|
|
|
|
throw new UnauthorizedException("Invalid email or password."); |
|
|
|
|
throw new UnauthorizedException("Invalid email , username or password."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const passwordMatch = await bcrypt.compare(loginUserDto.password, user.password); |
|
|
|
@ -97,7 +105,7 @@ export class UsersService { |
|
|
|
|
{ where: { id: user.id } }, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
return { accessToken: accessToken }; |
|
|
|
|
return { accessToken, refreshToken }; |
|
|
|
|
} catch (error) { |
|
|
|
|
if (error instanceof HttpException) { |
|
|
|
|
throw error; |
|
|
|
@ -106,21 +114,25 @@ export class UsersService { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// getting access token
|
|
|
|
|
async newAccessToken(userId: number) { |
|
|
|
|
const user = await this.userModel.findOne({ where: { id: userId } }); |
|
|
|
|
if (!user || !user.refreshToken) { |
|
|
|
|
async newAccessToken(refreshToken: string) { |
|
|
|
|
if (!refreshToken) { |
|
|
|
|
throw new HttpException("Refresh token is required.", HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let decoded; |
|
|
|
|
try { |
|
|
|
|
decoded = this.jwtService.verify(user.refreshToken, { secret: process.env.JWT_REFRESH_SECRET }); |
|
|
|
|
decoded = this.jwtService.verify(refreshToken, { secret: this.configService.get<string>("JWT_REFRESH_SECRET") }); |
|
|
|
|
} catch (error) { |
|
|
|
|
throw new HttpException("Invalid or expired token.", HttpStatus.UNAUTHORIZED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (user.id !== decoded.id) { |
|
|
|
|
throw new HttpException("User ID mismatch.", HttpStatus.FORBIDDEN); |
|
|
|
|
const user = await this.getProfile(decoded.id); |
|
|
|
|
if (!user) { |
|
|
|
|
throw new HttpException("User not found.", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (user.refreshToken !== refreshToken) { |
|
|
|
|
throw new HttpException("Invalid refresh token.", HttpStatus.FORBIDDEN); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const accessToken = this.jwtService.sign( |
|
|
|
|