Yield generated for 45f610c2-bc7a-46c7-901a-467c4af9d088
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.
 
 
 
 

30 lines
911 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 (!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);
}
}