-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.js
37 lines (31 loc) · 886 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
'use strict';
const model = require('./database/model');
const crypto = require('crypto');
const bcrypt = require('bcryptjs');
const COOKIE_OPTIONS = {
httpOnly: true,
maxAge: 600000,
sameSite: 'strict',
signed: true,
};
function createUser(name, email, password) {
return bcrypt
.hash(password, 10)
.then((hash) => model.createUser(name, email, hash));
}
function verifyUser(email, password) {
return model.getUser(email).then((user) => {
return bcrypt.compare(password, user.password).then((match) => {
if (!match) {
throw new Error('Wrong password!');
} else {
return user;
}
});
});
}
function saveUserSession(user) {
const randomSid = crypto.randomBytes(18).toString('base64');
return model.createSession(randomSid, { user });
}
module.exports = { COOKIE_OPTIONS, createUser, verifyUser, saveUserSession };