From 41a9bfad98d8f47a9ff45dbf8123f50af145f0b4 Mon Sep 17 00:00:00 2001 From: M1000fr Date: Fri, 13 Dec 2024 13:32:12 +0100 Subject: [PATCH] refactor: remove providerId from User --- prisma/schema.prisma | 5 ++--- src/modules/auth/guards/jwt.guard.ts | 7 ++++--- src/modules/user/dto/create-user.dto.ts | 7 ++++++- src/modules/user/user.gateway.ts | 4 ++-- src/modules/user/user.service.ts | 23 +++++------------------ 5 files changed, 19 insertions(+), 27 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 02ded4c..c26511d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -8,11 +8,10 @@ datasource db { } model User { - id String @id @default(cuid()) - username String? @unique + id String @id + username String role Role @default(STUDENT) createdAt DateTime @default(now()) - providerId String @unique Class Class[] SentMessages UserMessage[] @relation("SentMessages") diff --git a/src/modules/auth/guards/jwt.guard.ts b/src/modules/auth/guards/jwt.guard.ts index ffed5cc..2d3444a 100644 --- a/src/modules/auth/guards/jwt.guard.ts +++ b/src/modules/auth/guards/jwt.guard.ts @@ -27,9 +27,10 @@ export class JwtAuthGuard implements CanActivate { try { const jwtPayload = await this.authService.checkToken(token); - let user = await this.userService.findOrCreateByProviderId({ - providerId: jwtPayload.sub.toString(), - username: jwtPayload[this.configService.get("auth.usernameField")], + let user = await this.userService.findOrCreate({ + id: jwtPayload.sub.toString(), + username: + jwtPayload[this.configService.get("auth.usernameField")], }); request.user = user; diff --git a/src/modules/user/dto/create-user.dto.ts b/src/modules/user/dto/create-user.dto.ts index 1bc3068..7ada6c2 100644 --- a/src/modules/user/dto/create-user.dto.ts +++ b/src/modules/user/dto/create-user.dto.ts @@ -1,12 +1,17 @@ import { ApiProperty } from "@nestjs/swagger"; +import { Role } from "@prisma/client"; import { IsString } from "class-validator"; export class CreateUserDTO { + @IsString() + @ApiProperty() + id: string; + @IsString() @ApiProperty() username: string; @IsString() @ApiProperty() - providerId: string; + role: Role; } diff --git a/src/modules/user/user.gateway.ts b/src/modules/user/user.gateway.ts index ab49037..841846c 100644 --- a/src/modules/user/user.gateway.ts +++ b/src/modules/user/user.gateway.ts @@ -35,8 +35,8 @@ export class UserGateway implements OnGatewayConnection, OnGatewayDisconnect { return client.disconnect(); } - const user = await this.userService.findOrCreateByProviderId({ - providerId: jwtPayload.sub.toString(), + const user = await this.userService.findOrCreate({ + id: jwtPayload.sub.toString(), username: jwtPayload[this.configService.get("auth.usernameField")], }); diff --git a/src/modules/user/user.service.ts b/src/modules/user/user.service.ts index 0885d93..c4f7403 100644 --- a/src/modules/user/user.service.ts +++ b/src/modules/user/user.service.ts @@ -37,24 +37,11 @@ export class UserService { }); } - async findByProviderId(providerId: string) { - return await this.prisma.user.findUniqueOrThrow({ - where: { - providerId, - }, - }); - } - - async findOrCreateByProviderId({ - providerId, - username, - }: { - providerId: string; - username: string; - }) { + async findOrCreate({ id, username }: { id: string; username: string }) { let user = await this.prisma.user.findFirst({ where: { - OR: [{ providerId }, { username }], + id, + username, }, }); @@ -63,8 +50,8 @@ export class UserService { user = await this.prisma.user.create({ data: { + id, username, - providerId, role: isFirstUser ? "ADMIN" : "STUDENT", }, }); @@ -76,8 +63,8 @@ export class UserService { async create(createUserDto: CreateUserDTO) { return await this.prisma.user.create({ data: { + id: createUserDto.id, username: createUserDto.username, - providerId: createUserDto.providerId, }, }); }