-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
39 lines (34 loc) · 971 Bytes
/
auth.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
const crypto = require("crypto");
const bcrypt = require("bcryptjs");
const model = require("./database/model");
const COOKIE_OPTIONS = {
httpOnly: true,
maxAge: 600000,
sameSite: "strict",
signed: true,
};
function createUser(email, password, username) {
return bcrypt
.hash(password, 10)
.then((hash) => model.createUser(email, hash, username));
}
function saveUserSession(user) {
const randSID = crypto.randomBytes(18).toString("base64");
return model.createSession(randSID, { user });
}
function verifyUser(email, password) {
//console.log("model email", model.getUser(email))
return model.getUser(email)
.then((user) => {
return bcrypt.compare(password, user.password)
.then((match) => {
if(!match) {
throw new Error("password doesn't match!");
} else {
delete user.password;
return user;
}
})
})
}
module.exports = { COOKIE_OPTIONS, createUser, saveUserSession, verifyUser };