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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 3x 3x 3x 3x 10x 10x 2x 1x 2x 1x 4x 4x 4x 3x 1x 1x 1x 1x 3x 2x 2x | import { Injectable } from "@nestjs/common"; import { UserService } from "../users/user.service"; import { JwtAuthService } from "./jwt.service"; import { User } from "../users/user.entity"; import { RegisterDto } from "./dto/register.dto"; import { AuthResponse } from "@calendar-todo/shared-types"; @Injectable() export class AuthService { constructor( private readonly userService: UserService, private readonly jwtAuthService: JwtAuthService, ) {} async register(registerDto: RegisterDto): Promise<AuthResponse> { const user = await this.userService.create(registerDto); return this.generateTokens(user); } async validateUser(email: string, password: string): Promise<User | null> { return this.userService.validatePassword(email, password); } async login(user: User, rememberMe = false): Promise<AuthResponse> { return this.generateTokens(user, rememberMe); } async refreshToken(refreshToken: string): Promise<AuthResponse> { try { const tokens = await this.jwtAuthService.refreshAccessToken(refreshToken); if (!tokens) { throw new Error("Invalid refresh token"); } const payload = this.jwtAuthService.verifyRefreshToken( tokens.refreshToken, ); const user = await this.userService.findById(payload.sub); Iif (!user || !user.isActive) { throw new Error("User not found or inactive"); } return { accessToken: tokens.accessToken, refreshToken: tokens.refreshToken, user: user.toProfile(), }; } catch { throw new Error("Invalid refresh token"); } } async logout(userId: string, accessToken: string): Promise<void> { // Revoke refresh token await this.jwtAuthService.revokeRefreshToken(userId); // Blacklist access token await this.jwtAuthService.blacklistToken(accessToken); } async validateToken(token: string): Promise<boolean> { try { // Check if token is blacklisted const isBlacklisted = await this.jwtAuthService.isTokenBlacklisted(token); Iif (isBlacklisted) { return false; } // Verify token signature and expiration this.jwtAuthService.verifyAccessToken(token); return true; } catch { return false; } } private async generateTokens( user: User, rememberMe = false, ): Promise<AuthResponse> { const tokens = await this.jwtAuthService.generateTokenPair( user.id, user.email, rememberMe, ); return { accessToken: tokens.accessToken, refreshToken: tokens.refreshToken, user: user.toProfile(), }; } } |