Files
pdi_api/src/auth/auth.service.ts
T

48 lines
1.1 KiB
TypeScript
Raw Normal View History

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
break;
case 2:
// Alumno
user = await this.usersService.findStudent(loginDto);
break;
case 3:
// Trabajadores académicos o base
user = await this.usersService.findWorker(loginDto);
default:
throw new BadRequestException();
break;
}
if (user === null) {
throw new UnauthorizedException();
}
const payload = { id: user.id, userType: user.tipo_usuario_id };
const access_token = await this.jwtService.signAsync(payload);
return user;
}
}