2024-06-05 14:35:05 -06:00
|
|
|
|
2024-06-11 15:14:05 -06:00
|
|
|
import { HttpException, Inject, Injectable, UnauthorizedException } from '@nestjs/common';
|
2024-06-05 14:35:05 -06:00
|
|
|
import { UsersService } from '../users/users.service';
|
|
|
|
|
import { JwtService } from '@nestjs/jwt';
|
2024-06-11 19:58:36 -06:00
|
|
|
import { registerDto } from './dto/registerDto.dto';
|
2024-06-11 15:14:05 -06:00
|
|
|
import { User } from 'src/users/entities/user.entity';
|
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
import { Repository } from 'typeorm';
|
|
|
|
|
import { hash } from 'bcrypt';
|
|
|
|
|
import { compare } from 'bcryptjs';
|
2024-06-12 11:40:20 -06:00
|
|
|
import { ConfigService } from '@nestjs/config';
|
2024-06-11 15:14:05 -06:00
|
|
|
|
2024-06-05 14:35:05 -06:00
|
|
|
@Injectable()
|
|
|
|
|
export class AuthService {
|
|
|
|
|
constructor(
|
|
|
|
|
private usersService: UsersService,
|
2024-06-11 15:14:05 -06:00
|
|
|
private jwtService: JwtService,
|
2024-06-12 11:40:20 -06:00
|
|
|
private configService:ConfigService,
|
2024-06-11 15:14:05 -06:00
|
|
|
@InjectRepository(User) private userRepository: Repository<User>
|
2024-06-05 14:35:05 -06:00
|
|
|
) {}
|
|
|
|
|
|
2024-06-11 15:14:05 -06:00
|
|
|
async register(data: registerDto){
|
2024-06-12 21:26:05 -06:00
|
|
|
const{password,} = data;
|
2024-06-11 19:58:36 -06:00
|
|
|
|
|
|
|
|
if(await this.usersService.findOne(data.username)){
|
2024-06-11 15:14:05 -06:00
|
|
|
throw new HttpException("User already exist",403);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 11:40:20 -06:00
|
|
|
const hashedpassword = await hash(password, 10);
|
2024-06-11 15:14:05 -06:00
|
|
|
|
|
|
|
|
data = {...data, password:hashedpassword}
|
|
|
|
|
return this.userRepository.save(
|
|
|
|
|
this.userRepository.create(data)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 11:40:20 -06:00
|
|
|
|
2024-06-11 15:14:05 -06:00
|
|
|
async signIn(data: registerDto) {
|
2024-06-11 19:58:36 -06:00
|
|
|
const{password} = data;
|
2024-06-11 15:14:05 -06:00
|
|
|
const user = await this.usersService.findOne(data.username);
|
|
|
|
|
|
|
|
|
|
const checkPassword = await compare(password, (await user).password)
|
|
|
|
|
|
|
|
|
|
if (!checkPassword) {
|
2024-06-05 14:35:05 -06:00
|
|
|
throw new UnauthorizedException();
|
|
|
|
|
}
|
2024-06-11 15:14:05 -06:00
|
|
|
const payload = {
|
|
|
|
|
idUser: user.userId,
|
|
|
|
|
username: user.username
|
2024-06-05 14:35:05 -06:00
|
|
|
};
|
2024-06-11 15:14:05 -06:00
|
|
|
const token = this.jwtService.sign(payload);
|
|
|
|
|
|
2024-06-11 19:58:36 -06:00
|
|
|
await this.usersService.updateTokenAndDates(user.userId,token, new Date(), new Date(Date.now() + 3600 * 1000));
|
2024-06-11 15:14:05 -06:00
|
|
|
|
2024-06-11 19:58:36 -06:00
|
|
|
return {token: token};
|
2024-06-05 14:35:05 -06:00
|
|
|
}
|
2024-06-07 13:46:18 -06:00
|
|
|
|
2024-06-12 11:40:20 -06:00
|
|
|
async create(data){
|
2024-06-07 13:46:18 -06:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async update(
|
|
|
|
|
id,
|
|
|
|
|
updateUserDto
|
|
|
|
|
){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async remove(
|
|
|
|
|
id
|
|
|
|
|
){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 11:40:20 -06:00
|
|
|
async validateToken(token: string): Promise<User> {
|
|
|
|
|
try {
|
|
|
|
|
const decoded = this.jwtService.verify(token, {
|
|
|
|
|
secret: this.configService.get<string>('JWT_SECRET'),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//verify JWT's username with DB's username
|
|
|
|
|
const user = await this.usersService.findOne(decoded.username);
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new UnauthorizedException('Invalid token');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//verify JWT doesn't expired
|
|
|
|
|
const currentTime = Math.floor(Date.now() / 1000);
|
|
|
|
|
if (decoded.exp < currentTime) {
|
|
|
|
|
throw new UnauthorizedException('Token has expired');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return user;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new UnauthorizedException('validation token failed');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 14:35:05 -06:00
|
|
|
}
|