|
1 | 1 | import { Injectable, Logger } from '@nestjs/common'; |
2 | | -import { Prisma, PrismaClient } from '@prisma/client'; |
| 2 | +import { Prisma, PrismaClient, User } from '@prisma/client'; |
3 | 3 |
|
4 | 4 | import { |
5 | 5 | Config, |
@@ -106,32 +106,76 @@ export class UserService { |
106 | 106 | }); |
107 | 107 | } |
108 | 108 |
|
109 | | - async findUserByEmail(email: string) { |
| 109 | + async findUserByEmail( |
| 110 | + email: string |
| 111 | + ): Promise<Pick<User, keyof typeof this.defaultUserSelect> | null> { |
110 | 112 | validators.assertValidEmail(email); |
111 | | - return this.prisma.user.findFirst({ |
112 | | - where: { |
113 | | - email: { |
114 | | - equals: email, |
115 | | - mode: 'insensitive', |
116 | | - }, |
117 | | - }, |
118 | | - select: this.defaultUserSelect, |
119 | | - }); |
| 113 | + const rows = await this.prisma.$queryRaw< |
| 114 | + // see [this.defaultUserSelect] |
| 115 | + { |
| 116 | + id: string; |
| 117 | + name: string; |
| 118 | + email: string; |
| 119 | + email_verified: Date | null; |
| 120 | + avatar_url: string | null; |
| 121 | + registered: boolean; |
| 122 | + created_at: Date; |
| 123 | + }[] |
| 124 | + >` |
| 125 | + SELECT "id", "name", "email", "email_verified", "avatar_url", "registered", "created_at" |
| 126 | + FROM "users" |
| 127 | + WHERE lower("email") = lower(${email}) |
| 128 | + `; |
| 129 | + |
| 130 | + const user = rows[0]; |
| 131 | + |
| 132 | + if (!user) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + return { |
| 137 | + ...user, |
| 138 | + emailVerifiedAt: user.email_verified, |
| 139 | + avatarUrl: user.avatar_url, |
| 140 | + createdAt: user.created_at, |
| 141 | + }; |
120 | 142 | } |
121 | 143 |
|
122 | 144 | /** |
123 | 145 | * supposed to be used only for `Credential SignIn` |
124 | 146 | */ |
125 | | - async findUserWithHashedPasswordByEmail(email: string) { |
| 147 | + async findUserWithHashedPasswordByEmail(email: string): Promise<User | null> { |
126 | 148 | validators.assertValidEmail(email); |
127 | | - return this.prisma.user.findFirst({ |
128 | | - where: { |
129 | | - email: { |
130 | | - equals: email, |
131 | | - mode: 'insensitive', |
132 | | - }, |
133 | | - }, |
134 | | - }); |
| 149 | + |
| 150 | + // see https://www.prisma.io/docs/orm/prisma-client/using-raw-sql/raw-queries#typing-queryraw-results |
| 151 | + const rows = await this.prisma.$queryRaw< |
| 152 | + { |
| 153 | + id: string; |
| 154 | + name: string; |
| 155 | + email: string; |
| 156 | + password: string | null; |
| 157 | + email_verified: Date | null; |
| 158 | + avatar_url: string | null; |
| 159 | + registered: boolean; |
| 160 | + created_at: Date; |
| 161 | + }[] |
| 162 | + >` |
| 163 | + SELECT * |
| 164 | + FROM "users" |
| 165 | + WHERE lower("email") = lower(${email}) |
| 166 | + `; |
| 167 | + |
| 168 | + const user = rows[0]; |
| 169 | + if (!user) { |
| 170 | + return null; |
| 171 | + } |
| 172 | + |
| 173 | + return { |
| 174 | + ...user, |
| 175 | + emailVerifiedAt: user.email_verified, |
| 176 | + avatarUrl: user.avatar_url, |
| 177 | + createdAt: user.created_at, |
| 178 | + }; |
135 | 179 | } |
136 | 180 |
|
137 | 181 | async signIn(email: string, password: string) { |
|
0 commit comments