refactor: remove providerId from User

This commit is contained in:
M1000fr 2024-12-13 13:32:12 +01:00
parent e52c9acf2d
commit 41a9bfad98
5 changed files with 19 additions and 27 deletions

View File

@ -8,11 +8,10 @@ datasource db {
} }
model User { model User {
id String @id @default(cuid()) id String @id
username String? @unique username String
role Role @default(STUDENT) role Role @default(STUDENT)
createdAt DateTime @default(now()) createdAt DateTime @default(now())
providerId String @unique
Class Class[] Class Class[]
SentMessages UserMessage[] @relation("SentMessages") SentMessages UserMessage[] @relation("SentMessages")

View File

@ -27,9 +27,10 @@ export class JwtAuthGuard implements CanActivate {
try { try {
const jwtPayload = await this.authService.checkToken(token); const jwtPayload = await this.authService.checkToken(token);
let user = await this.userService.findOrCreateByProviderId({ let user = await this.userService.findOrCreate({
providerId: jwtPayload.sub.toString(), id: jwtPayload.sub.toString(),
username: jwtPayload[this.configService.get("auth.usernameField")], username:
jwtPayload[this.configService.get("auth.usernameField")],
}); });
request.user = user; request.user = user;

View File

@ -1,12 +1,17 @@
import { ApiProperty } from "@nestjs/swagger"; import { ApiProperty } from "@nestjs/swagger";
import { Role } from "@prisma/client";
import { IsString } from "class-validator"; import { IsString } from "class-validator";
export class CreateUserDTO { export class CreateUserDTO {
@IsString()
@ApiProperty()
id: string;
@IsString() @IsString()
@ApiProperty() @ApiProperty()
username: string; username: string;
@IsString() @IsString()
@ApiProperty() @ApiProperty()
providerId: string; role: Role;
} }

View File

@ -35,8 +35,8 @@ export class UserGateway implements OnGatewayConnection, OnGatewayDisconnect {
return client.disconnect(); return client.disconnect();
} }
const user = await this.userService.findOrCreateByProviderId({ const user = await this.userService.findOrCreate({
providerId: jwtPayload.sub.toString(), id: jwtPayload.sub.toString(),
username: jwtPayload[this.configService.get("auth.usernameField")], username: jwtPayload[this.configService.get("auth.usernameField")],
}); });

View File

@ -37,24 +37,11 @@ export class UserService {
}); });
} }
async findByProviderId(providerId: string) { async findOrCreate({ id, username }: { id: string; username: string }) {
return await this.prisma.user.findUniqueOrThrow({
where: {
providerId,
},
});
}
async findOrCreateByProviderId({
providerId,
username,
}: {
providerId: string;
username: string;
}) {
let user = await this.prisma.user.findFirst({ let user = await this.prisma.user.findFirst({
where: { where: {
OR: [{ providerId }, { username }], id,
username,
}, },
}); });
@ -63,8 +50,8 @@ export class UserService {
user = await this.prisma.user.create({ user = await this.prisma.user.create({
data: { data: {
id,
username, username,
providerId,
role: isFirstUser ? "ADMIN" : "STUDENT", role: isFirstUser ? "ADMIN" : "STUDENT",
}, },
}); });
@ -76,8 +63,8 @@ export class UserService {
async create(createUserDto: CreateUserDTO) { async create(createUserDto: CreateUserDTO) {
return await this.prisma.user.create({ return await this.prisma.user.create({
data: { data: {
id: createUserDto.id,
username: createUserDto.username, username: createUserDto.username,
providerId: createUserDto.providerId,
}, },
}); });
} }