diff --git a/apps/server/src/auth/auth.service.ts b/apps/server/src/auth/auth.service.ts index d435dadc..605f192a 100644 --- a/apps/server/src/auth/auth.service.ts +++ b/apps/server/src/auth/auth.service.ts @@ -8,7 +8,7 @@ import { import { ConfigService } from "@nestjs/config"; import { JwtService } from "@nestjs/jwt"; import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library"; -import { AuthProvidersDto, LoginDto, RegisterDto } from "@reactive-resume/dto"; +import { AuthProvidersDto, LoginDto, RegisterDto, UserWithSecrets } from "@reactive-resume/dto"; import { ErrorMessage } from "@reactive-resume/utils"; import * as bcryptjs from "bcryptjs"; import { randomBytes } from "crypto"; @@ -110,7 +110,7 @@ export class AuthService { // Do not `await` this function, otherwise the user will have to wait for the email to be sent before the response is returned this.sendVerificationEmail(user.email); - return user; + return user as UserWithSecrets; } catch (error) { if (error instanceof PrismaClientKnownRequestError && error.code === "P2002") { throw new BadRequestException(ErrorMessage.UserAlreadyExists); @@ -341,6 +341,6 @@ export class AuthService { secrets: { update: { twoFactorBackupCodes: backupCodes } }, }); - return user; + return user as UserWithSecrets; } } diff --git a/libs/dto/package.json b/libs/dto/package.json index 87afd8f3..0ce1a092 100644 --- a/libs/dto/package.json +++ b/libs/dto/package.json @@ -12,7 +12,6 @@ "@swc/helpers": "~0.5.2", "nestjs-zod": "^3.0.0", "@reactive-resume/utils": "*", - "@reactive-resume/schema": "*", - "@prisma/client": "^5.4.2" + "@reactive-resume/schema": "*" } } diff --git a/libs/dto/src/index.ts b/libs/dto/src/index.ts index 3eb2d194..21c3991c 100644 --- a/libs/dto/src/index.ts +++ b/libs/dto/src/index.ts @@ -1,5 +1,6 @@ export * from "./auth"; export * from "./contributors"; export * from "./resume"; +export * from "./secrets"; export * from "./statistics"; export * from "./user"; diff --git a/libs/dto/src/secrets/index.ts b/libs/dto/src/secrets/index.ts new file mode 100644 index 00000000..66f29af1 --- /dev/null +++ b/libs/dto/src/secrets/index.ts @@ -0,0 +1 @@ +export * from "./secrets"; diff --git a/libs/dto/src/secrets/secrets.ts b/libs/dto/src/secrets/secrets.ts new file mode 100644 index 00000000..dfc5f5ac --- /dev/null +++ b/libs/dto/src/secrets/secrets.ts @@ -0,0 +1,17 @@ +import { idSchema } from "@reactive-resume/schema"; +import { createZodDto } from "nestjs-zod/dto"; +import { z } from "nestjs-zod/z"; + +export const secretsSchema = z.object({ + id: idSchema, + password: z.string().nullable(), + lastSignedIn: z.date().nullable(), + verificationToken: z.string().nullable(), + twoFactorSecret: z.string().nullable(), + twoFactorBackupCodes: z.array(z.string()).default([]), + refreshToken: z.string().nullable(), + resetToken: z.string().nullable(), + userId: idSchema, +}); + +export class SecretsDto extends createZodDto(secretsSchema) {} diff --git a/libs/dto/src/user/user.ts b/libs/dto/src/user/user.ts index 1cd780d1..73a8c7a2 100644 --- a/libs/dto/src/user/user.ts +++ b/libs/dto/src/user/user.ts @@ -1,8 +1,9 @@ -import type { Prisma } from "@prisma/client"; import { idSchema } from "@reactive-resume/schema"; import { createZodDto } from "nestjs-zod/dto"; import { z } from "nestjs-zod/z"; +import { secretsSchema } from "../secrets"; + export const usernameSchema = z .string() .min(3) @@ -28,4 +29,6 @@ export const userSchema = z.object({ export class UserDto extends createZodDto(userSchema) {} -export type UserWithSecrets = Prisma.UserGetPayload<{ include: { secrets: true } }>; +export const userWithSecretsSchema = userSchema.merge(z.object({ secrets: secretsSchema })); + +export class UserWithSecrets extends createZodDto(userWithSecretsSchema) {}