Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x 1x 3x 3x 2x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 2x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Inject, Injectable, forwardRef } from "@nestjs/common"; import { UserService, UserException } from "src/user/user.service"; import * as bcrypt from "bcrypt"; import { JwtService } from "@nestjs/jwt"; import { ConfigService } from "@nestjs/config"; import { instanceToPlain } from "class-transformer"; type AuthError = | "WRONG CREDENTIALS"; export class AuthException extends Error { name: AuthError; constructor(name: AuthError) { super(); this.name = name; } } @Injectable() export class AuthService { constructor( @Inject(forwardRef(() => UserService)) private userService: UserService, private jwtService: JwtService, private configService: ConfigService, ) { } async signIn(email: string, password: string) { try { const user = await this.userService.findOneByEmail(email); if (!await bcrypt.compare(password, user.hashPassword)) { throw new AuthException("WRONG CREDENTIALS"); } const payload = { sub: user.username }; return { token: await this.jwtService.signAsync(payload, { secret: this.configService.get("JWT_SECRET") }), user: instanceToPlain(user), }; } catch (e) { if (e instanceof UserException) { throw new AuthException("WRONG CREDENTIALS"); } throw e; } } async signUp(username: string, password: string) { const hash = await bcrypt.hash(password, 10); const payload = { sub: username }; return { hash, jwt: await this.jwtService.signAsync(payload, { secret: this.configService.get("JWT_SECRET"), }), }; } } |