forked from infonomic/remix.infonomic.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.server.ts
158 lines (134 loc) · 3.37 KB
/
user.server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import bcrypt from 'bcryptjs'
import { prisma } from '~/db.server'
import { SEARCH_PARAMS_DEFAULTS } from '~/models/user'
import type { Password, User } from '@prisma/client'
import type { SearchParams } from '~/models/user'
export type { User } from '@prisma/client'
export async function verifyLogin(email: User['email'], password: Password['hash']) {
const userWithPassword = await prisma.user.findUnique({
where: { email },
include: {
password: true,
},
})
if (!userWithPassword || !userWithPassword.password) {
return null
}
const isValid = await bcrypt.compare(password, userWithPassword.password.hash)
if (!isValid) {
return null
}
// eslint-disable-next-line @typescript-eslint/naming-convention
const { password: _password, ...userWithoutPassword } = userWithPassword
return userWithoutPassword
}
export async function getUsers({
page = SEARCH_PARAMS_DEFAULTS.page,
pageSize = SEARCH_PARAMS_DEFAULTS.pageSize,
orderBy = SEARCH_PARAMS_DEFAULTS.orderBy,
orderDesc = SEARCH_PARAMS_DEFAULTS.orderDesc,
}: SearchParams) {
const count = await prisma.user.count()
const meta = {
total: count,
pageSize: pageSize,
pageTotal: Math.ceil(count / pageSize),
currentPage: page,
}
const users = await prisma.user.findMany({
select: { id: true, email: true, createdAt: true },
skip: (page - 1) * pageSize,
take: pageSize,
orderBy: {
[orderBy]: orderDesc ? 'desc' : 'asc',
},
})
return { users, meta }
}
export async function getUserById(id: User['id']) {
return prisma.user.findUnique({ where: { id } })
}
export async function getUserByEmail(email: User['email']) {
return prisma.user.findUnique({ where: { email } })
}
export async function getUserWithNotes(id: User['id']) {
return prisma.user.findUnique({
where: { id },
include: {
notes: true,
},
})
}
export async function createUser(email: User['email'], password: string) {
const hashedPassword = await bcrypt.hash(password, 10)
return prisma.user.create({
data: {
email,
password: {
create: {
hash: hashedPassword,
},
},
},
})
}
export async function updateUserEmail(id: User['id'], email: User['email']) {
// Check for another user with this email address
const result = await prisma.user.findMany({
where: {
email,
NOT: {
id: {
equals: id,
},
},
},
select: {
email: true,
},
})
if (result.length > 0) return null
return prisma.user.update({
where: {
id,
},
data: {
email,
},
})
}
export async function updateUserPassword(
id: User['id'],
currentPassword: string,
newPassword: string
) {
const hashedNewPassword = await bcrypt.hash(newPassword, 10)
const userWithPassword = await prisma.user.findUnique({
where: { id },
include: {
password: true,
},
})
if (!userWithPassword || !userWithPassword.password) {
return null
}
const isValid = await bcrypt.compare(currentPassword, userWithPassword.password.hash)
if (!isValid) {
return null
}
return prisma.user.update({
where: {
id,
},
data: {
password: {
update: {
hash: hashedNewPassword,
},
},
},
})
}
export async function deleteUserByEmail(email: User['email']) {
return prisma.user.delete({ where: { email } })
}