auth-service.service.ts111 lines, 2.7 KB
| 1 | import { DatabaseService, users } from '@app/database'; |
| 2 | import { KAFKA_SERVICE, KAFKA_TOPICS } from '@app/kafka'; |
| 3 | import { |
| 4 | ConflictException, |
| 5 | Inject, |
| 6 | Injectable, |
| 7 | OnModuleInit, |
| 8 | UnauthorizedException, |
| 9 | } from '@nestjs/common'; |
| 10 | import { JwtService } from '@nestjs/jwt'; |
| 11 | import { ClientKafka } from '@nestjs/microservices'; |
| 12 | import { eq } from 'drizzle-orm'; |
| 13 | import * as bcrypt from 'bcrypt'; |
| 14 | |
| 15 | @Injectable() |
| 16 | export class AuthServiceService implements OnModuleInit { |
| 17 | constructor( |
| 18 | @Inject(KAFKA_SERVICE) private readonly kafkClient: ClientKafka, |
| 19 | private readonly dbService: DatabaseService, |
| 20 | private readonly jwtService: JwtService, |
| 21 | ) {} |
| 22 | |
| 23 | async onModuleInit() { |
| 24 | // connect to kafka when module initializes |
| 25 | await this.kafkClient.connect(); |
| 26 | } |
| 27 | |
| 28 | getHello(): string { |
| 29 | return 'Hello World!'; |
| 30 | } |
| 31 | |
| 32 | async register(email: string, password: string, name: string) { |
| 33 | // check if user exists |
| 34 | const exitingUser = await this.dbService.db |
| 35 | .select() |
| 36 | .from(users) |
| 37 | .where(eq(users.email, email)) |
| 38 | .limit(1); |
| 39 | |
| 40 | if (exitingUser.length > 0) { |
| 41 | throw new ConflictException('User already exists'); |
| 42 | } |
| 43 | |
| 44 | // Hash password |
| 45 | const hashedPassword = await bcrypt.hash(password, 10); |
| 46 | |
| 47 | // create user |
| 48 | const [user] = await this.dbService.db |
| 49 | .insert(users) |
| 50 | .values({ email, password: hashedPassword, name }) |
| 51 | .returning(); |
| 52 | |
| 53 | // send user registered event |
| 54 | this.kafkClient.emit(KAFKA_TOPICS.USER_REGISTERED, { |
| 55 | userId: user.id, |
| 56 | email: user.email, |
| 57 | name: user.name, |
| 58 | timestamp: new Date().toISOString(), |
| 59 | }); |
| 60 | |
| 61 | return { message: 'User registered successfully', userId: user.id }; |
| 62 | } |
| 63 | |
| 64 | async login(email: string, password: string) { |
| 65 | const [user] = await this.dbService.db |
| 66 | .select() |
| 67 | .from(users) |
| 68 | .where(eq(users.email, email)) |
| 69 | .limit(1); |
| 70 | |
| 71 | if (!user || !(await bcrypt.compare(password, user.password))) { |
| 72 | throw new UnauthorizedException('Invalid credentials'); |
| 73 | } |
| 74 | |
| 75 | const token = this.jwtService.sign({ sub: user.id, email: user.email }); |
| 76 | |
| 77 | this.kafkClient.emit(KAFKA_TOPICS.USER_LOGIN, { |
| 78 | userId: user.id, |
| 79 | timestamp: new Date().toISOString(), |
| 80 | }); |
| 81 | |
| 82 | return { |
| 83 | access_token: token, |
| 84 | user: { |
| 85 | id: user.id, |
| 86 | email: user.email, |
| 87 | name: user.name, |
| 88 | role: user.role, |
| 89 | }, |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | async getProfile(userId: string) { |
| 94 | const [user] = await this.dbService.db |
| 95 | .select({ |
| 96 | id: users.id, |
| 97 | email: users.email, |
| 98 | name: users.name, |
| 99 | role: users.role, |
| 100 | }) |
| 101 | .from(users) |
| 102 | .where(eq(users.id, userId)) |
| 103 | .limit(1); |
| 104 | |
| 105 | if (!user) { |
| 106 | throw new UnauthorizedException('User not found'); |
| 107 | } |
| 108 | |
| 109 | return user; |
| 110 | } |
| 111 | } |