refactor: remove providerId from User
This commit is contained in:
parent
e52c9acf2d
commit
41a9bfad98
@ -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")
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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")],
|
||||
});
|
||||
|
||||
|
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user