2024-09-11 22:35:57 -06:00
|
|
|
import {
|
|
|
|
|
BadRequestException,
|
|
|
|
|
Injectable,
|
|
|
|
|
UnauthorizedException,
|
|
|
|
|
} from '@nestjs/common';
|
|
|
|
|
import { UsersService } from '../users/users.service';
|
|
|
|
|
import { LoginDTO } from '../dtos/loginDTO';
|
|
|
|
|
import { JwtService } from '@nestjs/jwt';
|
2024-09-11 23:21:46 -06:00
|
|
|
import { UsuarioEntity } from '../entities/usuario.entity';
|
2024-09-10 17:51:20 -06:00
|
|
|
|
|
|
|
|
@Injectable()
|
2024-09-11 22:35:57 -06:00
|
|
|
export class AuthService {
|
|
|
|
|
constructor(
|
|
|
|
|
private usersService: UsersService,
|
|
|
|
|
private jwtService: JwtService,
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
async signIn(loginDto: LoginDTO): Promise<any> {
|
|
|
|
|
let user: UsuarioEntity;
|
|
|
|
|
switch (loginDto.tipo_usuario) {
|
|
|
|
|
case 1:
|
|
|
|
|
// Administrador
|
2024-10-02 19:13:08 -06:00
|
|
|
user = await this.usersService.findAdmin(loginDto);
|
2024-09-11 22:35:57 -06:00
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
// Alumno
|
|
|
|
|
user = await this.usersService.findStudent(loginDto);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 3:
|
2024-10-01 12:24:43 -06:00
|
|
|
// Trabajadores
|
2024-09-11 22:35:57 -06:00
|
|
|
user = await this.usersService.findWorker(loginDto);
|
2024-09-24 11:50:25 -06:00
|
|
|
break;
|
2024-10-01 12:24:43 -06:00
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
|
// Académicos
|
|
|
|
|
user = await this.usersService.findWorker(loginDto);
|
|
|
|
|
break;
|
|
|
|
|
|
2024-09-11 22:35:57 -06:00
|
|
|
default:
|
|
|
|
|
throw new BadRequestException();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (user === null) {
|
|
|
|
|
throw new UnauthorizedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const payload = { id: user.id, userType: user.tipo_usuario_id };
|
2024-09-12 19:17:35 -06:00
|
|
|
const AT = await this.jwtService.signAsync(payload);
|
|
|
|
|
return { accessToken: AT };
|
2024-09-11 22:35:57 -06:00
|
|
|
}
|
|
|
|
|
}
|