-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathuser.js
104 lines (94 loc) · 2.78 KB
/
user.js
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
import bcrypt from 'bcryptjs';
import { ObjectId } from 'mongodb';
import normalizeEmail from 'validator/lib/normalizeEmail';
export async function findUserWithEmailAndPassword(db, email, password) {
email = normalizeEmail(email);
const user = await db.collection('users').findOne({ email });
if (user && (await bcrypt.compare(password, user.password))) {
return { ...user, password: undefined }; // filtered out password
}
return null;
}
export async function findUserForAuth(db, userId) {
return db
.collection('users')
.findOne({ _id: new ObjectId(userId) }, { projection: { password: 0 } })
.then((user) => user || null);
}
export async function findUserById(db, userId) {
return db
.collection('users')
.findOne({ _id: new ObjectId(userId) }, { projection: dbProjectionUsers() })
.then((user) => user || null);
}
export async function findUserByUsername(db, username) {
return db
.collection('users')
.findOne({ username }, { projection: dbProjectionUsers() })
.then((user) => user || null);
}
export async function findUserByEmail(db, email) {
email = normalizeEmail(email);
return db
.collection('users')
.findOne({ email }, { projection: dbProjectionUsers() })
.then((user) => user || null);
}
export async function updateUserById(db, id, data) {
return db
.collection('users')
.findOneAndUpdate(
{ _id: new ObjectId(id) },
{ $set: data },
{ returnDocument: 'after', projection: { password: 0 } }
)
.then(({ value }) => value);
}
export async function insertUser(
db,
{ email, originalPassword, bio = '', name, profilePicture, username }
) {
const user = {
emailVerified: false,
profilePicture,
email,
name,
username,
bio,
};
const password = await bcrypt.hash(originalPassword, 10);
const { insertedId } = await db
.collection('users')
.insertOne({ ...user, password });
user._id = insertedId;
return user;
}
export async function updateUserPasswordByOldPassword(
db,
id,
oldPassword,
newPassword
) {
const user = await db.collection('users').findOne(new ObjectId(id));
if (!user) return false;
const matched = await bcrypt.compare(oldPassword, user.password);
if (!matched) return false;
const password = await bcrypt.hash(newPassword, 10);
await db
.collection('users')
.updateOne({ _id: new ObjectId(id) }, { $set: { password } });
return true;
}
export async function UNSAFE_updateUserPassword(db, id, newPassword) {
const password = await bcrypt.hash(newPassword, 10);
await db
.collection('users')
.updateOne({ _id: new ObjectId(id) }, { $set: { password } });
}
export function dbProjectionUsers(prefix = '') {
return {
[`${prefix}password`]: 0,
[`${prefix}email`]: 0,
[`${prefix}emailVerified`]: 0,
};
}