-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.server.ts
154 lines (129 loc) · 3.23 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
import type { password, user, review } 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"]) {
const user = await prisma.user.findUnique({ where: { id: id } });
return user;
}
export async function getUserByEmail(email: user["email"]) {
const user = await prisma.user.findUnique({ where: { email } });
return user;
}
export async function getUserByUsername(username: user["username"]) {
const user = await prisma.user.findUnique({ where: { username: username! } });
return user;
}
export async function getUserByReviewId(reviewId: review["id"]) {
const userObject = await prisma.review.findFirst({
where: { id: reviewId },
select: {
userId: true,
},
});
const user = await prisma.user.findFirst({
where: {
id: userObject?.userId,
},
});
return user;
}
export async function createUser(
email: user["email"],
username: user["username"],
password: string,
) {
const hashedPassword = await bcrypt.hash(password, 10);
return prisma.user.create({
data: {
email,
username,
password: {
create: {
hash: hashedPassword,
},
},
},
});
}
export async function deleteUserByEmail(email: user["email"]) {
return prisma.user.delete({ where: { email } });
}
export async function verifyWithUsername(
username: user["username"],
password: password["hash"],
) {
const userWithPassword = await prisma.user.findUnique({
where: { username },
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;
console.log(_password.userId);
return userWithoutPassword;
}
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;
}
const { password: _password, ...userWithoutPassword } = userWithPassword;
console.log(_password.userId);
return userWithoutPassword;
}
export async function follow(
followerId: user["id"],
beingFollowedId: user["id"],
) {
// const follow = await prisma.follows.create({
// data: {
// followerId,
// followingId: beingFollowedId,
// },
// });
return prisma.user.update({
where: { id: followerId },
data: {
following: {
connect: {
followerId_followingId: {
followerId,
followingId: beingFollowedId,
},
},
},
},
});
}
export function unfollow(followerId: user["id"], beingFollowedId: user["id"]) {
return prisma.follows.delete({
where: {
followerId_followingId: {
followerId,
followingId: beingFollowedId,
},
},
});
}