|
1 | | -import { ConflictException, Injectable, UnauthorizedException } from '@nestjs/common'; |
| 1 | +import { |
| 2 | + ConflictException, |
| 3 | + Injectable, |
| 4 | + UnauthorizedException, |
| 5 | +} from '@nestjs/common'; |
2 | 6 | import { UserService } from '../user/application/user.service'; |
3 | 7 | import { JwtService } from '@nestjs/jwt'; |
4 | 8 | import * as bcrypt from 'bcrypt'; |
5 | 9 | import { LoginDto } from './dto/login.dto'; |
6 | 10 | import { RegisterDto } from './dto/register.dto'; |
7 | 11 |
|
| 12 | +import { AuthResponse } from './interfaces/auth-response.interface'; |
| 13 | +import { User } from '@user/domain/user.entity'; |
| 14 | + |
8 | 15 | @Injectable() |
9 | 16 | export class AuthService { |
10 | 17 | constructor( |
11 | 18 | private userService: UserService, |
12 | 19 | private jwtService: JwtService, |
13 | 20 | ) {} |
14 | 21 |
|
15 | | - async validateUser(email: string, pass: string): Promise<any> { |
| 22 | + async validateUser( |
| 23 | + email: string, |
| 24 | + pass: string, |
| 25 | + ): Promise<Omit<User, 'passwordHash'> | null> { |
16 | 26 | const user = await this.userService.findByEmail(email); |
17 | 27 | if (user && user.passwordHash) { |
18 | 28 | const isMatch = await bcrypt.compare(pass, user.passwordHash); |
19 | 29 | if (isMatch) { |
20 | | - // eslint-disable-next-line @typescript-eslint/no-unused-vars |
21 | | - const { passwordHash, ...result } = user; |
22 | | - return result; |
| 30 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 31 | + const { passwordHash, ...result } = user; |
| 32 | + return result; |
23 | 33 | } |
24 | 34 | } |
25 | 35 | return null; |
26 | 36 | } |
27 | 37 |
|
28 | | - async login(loginDto: LoginDto) { |
| 38 | + async login(loginDto: LoginDto): Promise<AuthResponse> { |
29 | 39 | const user = await this.validateUser(loginDto.email, loginDto.password); |
30 | 40 | if (!user) { |
31 | 41 | throw new UnauthorizedException('Invalid credentials'); |
32 | 42 | } |
33 | | - const payload = { |
34 | | - email: user.email, |
35 | | - sub: user.id, |
| 43 | + const payload = { |
| 44 | + email: user.email, |
| 45 | + sub: user.id, |
36 | 46 | role: user.role, |
37 | 47 | firstName: user.firstName, |
38 | 48 | lastName: user.lastName, |
39 | | - photoUrl: user.photoUrl |
| 49 | + photoUrl: user.photoUrl, |
40 | 50 | }; |
41 | 51 | return { |
42 | 52 | access_token: this.jwtService.sign(payload), |
43 | 53 | }; |
44 | 54 | } |
45 | 55 |
|
46 | | - async register(registerDto: RegisterDto) { |
| 56 | + async register(registerDto: RegisterDto): Promise<AuthResponse> { |
47 | 57 | const existing = await this.userService.findByEmail(registerDto.email); |
48 | 58 | if (existing) { |
49 | | - throw new ConflictException('User with this email already exists'); |
| 59 | + throw new ConflictException('User with this email already exists'); |
50 | 60 | } |
51 | 61 |
|
52 | 62 | const salt = await bcrypt.genSalt(); |
53 | 63 | const passwordHash = await bcrypt.hash(registerDto.password, salt); |
54 | 64 |
|
55 | 65 | // Create user logic using UserService.create |
56 | 66 | // UserService.create expects Omit<User, 'id' | 'createdAt'> |
57 | | - // User constructor params: firstName, telegramId?, email?, passwordHash?, lastName?, username?, photoUrl?, role? |
58 | | - // We strictly follow User type used in UserService |
59 | | - |
60 | 67 | const newUser = await this.userService.create({ |
61 | | - firstName: registerDto.firstName, |
62 | | - lastName: registerDto.lastName, |
63 | | - email: registerDto.email, |
64 | | - passwordHash: passwordHash, |
65 | | - role: 'user', |
66 | | - // Optional fields mapping |
67 | | - username: registerDto.username, |
68 | | - // Must explicitly undefined or omitting optional fields? |
69 | | - // Typescript should allow missing optional fields if they are optional in type. |
70 | | - } as any); |
| 68 | + firstName: registerDto.firstName, |
| 69 | + lastName: registerDto.lastName, |
| 70 | + email: registerDto.email, |
| 71 | + passwordHash: passwordHash, |
| 72 | + role: 'user', |
| 73 | + username: registerDto.username, |
| 74 | + } as unknown as Omit<User, 'id' | 'createdAt'>); |
71 | 75 |
|
72 | | - const payload = { |
73 | | - email: newUser.email, |
74 | | - sub: newUser.id, |
| 76 | + const payload = { |
| 77 | + email: newUser.email, |
| 78 | + sub: newUser.id, |
75 | 79 | role: newUser.role, |
76 | 80 | firstName: newUser.firstName, |
77 | 81 | lastName: newUser.lastName, |
78 | | - photoUrl: newUser.photoUrl |
| 82 | + photoUrl: newUser.photoUrl, |
79 | 83 | }; |
80 | 84 | return { |
81 | 85 | access_token: this.jwtService.sign(payload), |
|
0 commit comments