-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.server.ts
163 lines (135 loc) · 3.62 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
159
160
161
162
163
import type { password, player, Prisma, user } from "@prisma/client";
import bcrypt from "bcryptjs";
import { prisma } from "~/db.server";
export type { user } from "@prisma/client";
export async function getUserById(id: user["id"]) {
if (!id) {
return null;
}
return prisma.user.findFirst({ where: { id }, include: { player: true } });
}
export async function getUserByEmail(email: user["email"]) {
if (!email) {
return null;
}
return prisma.user.findFirst({ where: { email }, include: { player: true } });
}
export async function getPlayerByUserId(userId: user["id"]) {
if (!userId) {
return null;
}
return prisma.player.findFirst({
where: { user: { some: { id: userId } } },
});
}
export async function getUserByPlayerName(playerName: player["name"]) {
if (!playerName) {
return null;
}
return prisma.user.findFirst({
where: { player: { name: playerName } },
include: { player: true },
});
}
export async function getUserByPlayerId(playerId: player["id"]) {
if (!playerId) {
return null;
}
return prisma.user.findFirst({
where: { player: { id: playerId } },
include: {
player: { include: { player_alt_playerToplayer_alt_alt_id: true } },
},
});
}
export async function createUser(
email: user["email"],
password: string,
player: player
) {
const existingMain = await getUserByPlayerName(player.name);
if (existingMain) {
return null;
}
let existingPlayer = await prisma.player.findFirst({
where: { name: player.name.trim().toLowerCase() },
});
if (!existingPlayer) {
existingPlayer = await prisma.player.create({
data: {
name: player.name?.trim().toLowerCase(),
class: player.class?.trim().toLowerCase(),
level: player.level,
},
});
} else {
existingPlayer.level = player.level;
existingPlayer.class = player.class;
await prisma.player.update({
data: { class: player.class, level: player.level },
where: { name: player.name.trim().toLowerCase() },
});
}
if (!existingPlayer) {
return null;
}
const hashedPassword = await bcrypt.hash(password, 10);
const data: Prisma.userCreateInput = {
// @ts-ignore
player_id: existingPlayer.id as unknown as number,
password: {
create: {
hash: hashedPassword,
},
},
};
if (email && email.includes("@")) {
data.email = email?.includes("@") ? email : null;
}
return prisma.user.create({
data,
});
}
export async function deleteUserById(id: user["id"]) {
return await prisma.player.update({
data: { deleted_at: new Date() },
where: { id },
});
}
export async function undeleteUserById(id: user["id"]) {
return await prisma.player.update({
data: { deleted_at: null },
where: { id },
});
}
export async function verifyLogin(
emailOrUsername: user["email"],
password: password["hash"]
) {
let userWithPassword = await prisma.user.findFirst({
where: { email: emailOrUsername?.toLowerCase().trim() },
include: {
password: true,
},
});
if (!userWithPassword && emailOrUsername && !emailOrUsername.includes("@")) {
userWithPassword = await prisma.user.findFirst({
where: { player: { name: emailOrUsername?.toLowerCase().trim() } },
include: {
password: true,
},
});
}
if (!userWithPassword || !userWithPassword.password) {
return null;
}
const isValid = await bcrypt.compare(
password,
userWithPassword.password.hash
);
if (!isValid) {
return null;
}
const { password: _password, ...userWithoutPassword } = userWithPassword;
return userWithoutPassword;
}