(payload: z.infer<typeof zodSchemas.userCredential>)
| 74 | }, |
| 75 | // 重设账户密码 |
| 76 | async resetPassword(payload: z.infer<typeof zodSchemas.userCredential>) { |
| 77 | const { email, password } = zodSchemas.userCredential.parse(payload) |
| 78 | const { hash, salt } = saltAndHashPassword(password) |
| 79 | |
| 80 | const user = await db.transaction(async (tx) => { |
| 81 | const targetUser = await tx.query.users.findFirst({ |
| 82 | where: eq(schema.users.email, email), |
| 83 | with: { credential: true }, |
| 84 | }) |
| 85 | |
| 86 | if (!targetUser) { |
| 87 | throw new Error('用户不存在') |
| 88 | } |
| 89 | |
| 90 | if (targetUser.credential) { |
| 91 | await tx |
| 92 | .update(schema.credentials) |
| 93 | .set({ |
| 94 | password: hash, |
| 95 | salt, |
| 96 | }) |
| 97 | .where(eq(schema.credentials.userId, targetUser.id)) |
| 98 | } else { |
| 99 | await tx.insert(schema.credentials).values({ |
| 100 | userId: targetUser.id, |
| 101 | password: hash, |
| 102 | salt, |
| 103 | }) |
| 104 | } |
| 105 | |
| 106 | const credentialsAccount = await tx.query.accounts.findFirst({ |
| 107 | where: and( |
| 108 | eq(schema.accounts.provider, 'credentials'), |
| 109 | eq(schema.accounts.providerAccountId, email) |
| 110 | ), |
| 111 | }) |
| 112 | |
| 113 | if (!credentialsAccount) { |
| 114 | await tx.insert(schema.accounts).values({ |
| 115 | userId: targetUser.id, |
| 116 | type: 'email', |
| 117 | provider: 'credentials', |
| 118 | providerAccountId: email, |
| 119 | }) |
| 120 | } |
| 121 | |
| 122 | return targetUser |
| 123 | }) |
| 124 | |
| 125 | return user |
| 126 | }, |
| 127 | // 删除账户 |
| 128 | async delete(email: string) { |
| 129 | const user = await db.query.users.findFirst({ |
nothing calls this directly
no test coverage detected