Yield generated for 342f1d9d-abda-461c-8db9-47eb19400c58
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

32 lines
982 B

import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UserService } from '../../user/user.service';
import { JwtService } from '@nestjs/jwt';
export interface JwtPayload {
idUser: number;
}
@Injectable()
export class AuthService {
constructor(
private readonly usersService: UserService,
private readonly jwtService: JwtService,
) {}
async signIn(email: string, password: string): Promise<string> {
const userFound = (await this.usersService.getOnWithEmail(
email,
)).orElseThrow(() => new UnauthorizedException());
if (!(await this.usersService.doPasswordMatch(userFound, password))) {
throw new UnauthorizedException();
}
const user: JwtPayload = { idUser: userFound.id };
return this.jwtService.sign(user);
}
async validateUser(payload: JwtPayload): Promise<any> {
return (await this.usersService.getOneById(payload.idUser)).orElseThrow(
() => new UnauthorizedException(),
);
}
}