-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.js
38 lines (33 loc) · 968 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
const crypto = require("crypto");
const bcrypt = require("bcryptjs");
//const model = require("./database/model.js");
const { match } = require("assert");
const COOKIE_OPTIONS = {
httpOnly: true,
maxAge: 600000,
sameSite: "strict",
signed: true,
};
function makeUser(username, email, password) {
return bcrypt.hash(password, 10).then((hash) => {
return model.createUser(username, email, hash);
});
}
function userSession(user) {
console.log("userSession", user);
const sid = crypto.randomBytes(18).toString("base64");
return model.makeSession(sid, { user });
}
function validateUser(email, password) {
return model.getUser(email).then((user) => {
bcrypt.compare(password, user.password).then((match) => {
if (!match) {
throw new Error("Password mismatch");
} else {
delete user.password;
return user;
}
});
});
}
module.exports = { COOKIE_OPTIONS, makeUser, userSession, validateUser };