11
2- import { ConflictException , ForbiddenException , InternalServerErrorException } from "@nestjs/common" ;
2+ import { ConflictException , ForbiddenException , HttpException , HttpStatus , InternalServerErrorException } from "@nestjs/common" ;
33import { JwtService } from "@nestjs/jwt" ;
44import * as bcrypt from 'bcrypt' ;
55import { Tokens } from "../types" ;
66import { PrismaService } from '../../prisma/prisma.service' ;
77import { HashingException , UpdateHashException } from './../../exceptions' ;
8+ import { User } from "@prisma/client" ;
89
910
1011export abstract class AuthJwt {
@@ -110,14 +111,16 @@ export abstract class AuthJwt {
110111 * user's ID or subject identifier. It is used to identify the user whose refresh token hash needs to
111112 * be updated to `null` during the logout process.
112113 */
113- async logout ( sub : number ) {
114+ async logout ( sub : number ) : Promise < boolean > {
114115 try {
115- await this . prismaService . user . update ( {
116+ const result = await this . prismaService . user . update ( {
116117 where : { id : sub } ,
117118 data : {
118119 refreshTokenHash : null ,
119120 } ,
120121 } ) ;
122+
123+ return ! ! result ;
121124 } catch ( error ) {
122125 throw new UpdateHashException ( sub , error . message ) ;
123126 }
@@ -172,4 +175,54 @@ export abstract class AuthJwt {
172175 throw new InternalServerErrorException ( 'Error al procesar el token de actualización.' ) ;
173176 }
174177 }
178+
179+ /**
180+ * This TypeScript function asynchronously finds a user by their email using Prisma.
181+ * @param {string } email - The `findUserEmail` function is an asynchronous function that takes an
182+ * email address as a parameter and returns a Promise that resolves to a `User` object. The function
183+ * uses the `prismaService` to query the database and find the first user with the specified email
184+ * address.
185+ * @returns The `findUserEmail` function is returning a Promise that resolves to a `User` object.
186+ */
187+ async findUserEmail ( email : string ) : Promise < User > {
188+ const user = await this . prismaService . user . findFirst ( {
189+ where : {
190+ email
191+ }
192+ } )
193+ return user ;
194+ }
195+
196+
197+ /**
198+ * The function `compareUserPassword` asynchronously compares an authentication password with a hashed
199+ * password using bcrypt and returns a boolean indicating whether they match.
200+ * @param {string } authPassword - The `authPassword` parameter is the password provided by the user
201+ * during authentication, typically entered through a login form or API request. This password needs to
202+ * be compared with the hashed password stored in the database (retrieved as `prismaPassword`) to
203+ * verify the user's identity. The `compare
204+ * @param {string } prismaPassword - The `prismaPassword` parameter in the `compareUserPassword`
205+ * function refers to the hashed password stored in your database, typically hashed using a secure
206+ * hashing algorithm like bcrypt. When a user tries to authenticate, their input password
207+ * (`authPassword`) is compared with the hashed password retrieved from the database to
208+ * @returns A boolean value is being returned. If the authentication password matches the Prisma
209+ * password, the function returns `true`.
210+ */
211+ async compareUserPassword ( authPassword : string , prismaPassword : string ) : Promise < boolean > {
212+ try {
213+
214+ const rtMatches = await bcrypt . compare ( authPassword , prismaPassword ) ;
215+ if ( ! rtMatches ) {
216+ throw new ForbiddenException ( 'Authentication failed. Please check your credentials.' ) ;
217+ }
218+
219+ return rtMatches ;
220+ } catch ( error ) {
221+ if ( error instanceof ForbiddenException ) {
222+ throw error
223+ }
224+ throw new HttpException ( "An unexpected error occurred. Please try again later." , HttpStatus . INTERNAL_SERVER_ERROR ) ;
225+ }
226+ }
227+
175228}
0 commit comments