-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.js
43 lines (38 loc) · 1006 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
40
41
42
43
const crypto = require('crypto');
const bcrypt = require('bcryptjs');
const model = require('./database/model');
const COOKIE_OPTIONS = {
httpOnly: true,
maxAge: 600000,
sameSite: 'lax',
signed: true,
};
function createUser(name, email, password, avatar) {
return bcrypt
.hash(password, 10)
.then((hash) => {
console.log('auth.js:', avatar);
return model.createUser(name, email, hash, avatar);
})
.catch(console.warn);
}
function verifyUser(email, password) {
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;
}
});
})
.catch(console.warn);
}
function saveUserSession(user) {
const sid = crypto.randomBytes(18).toString('base64');
return model.createSession(sid, { user }).catch(console.warn);
}
module.exports = { verifyUser, saveUserSession, createUser, COOKIE_OPTIONS };