-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.js
33 lines (30 loc) · 874 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
import { createSession, getUser } from "./database/model";
const crypto = require("crypto");
import bcrypt from "bcryptjs";
export const cookie_options = {
httpOnly: true,
maxAge: 60 * 60 * 1000 * 24,
sameSite: "strict",
signed: true,
};
export async function saveSession(data) {
//create random sid
const sid = crypto.randomBytes(18).toString("base64");
//run create session in model.js, which inserts session into db (data and sid)
return createSession(sid, data);
}
//called in log-in
export async function verifyUser(email, password) {
//calls getUser in model
const savedUser = await getUser(email);
if (savedUser[0]) {
return bcrypt.compare(password, savedUser[0].password).then((match) => {
if (!match) {
return undefined;
} else {
delete savedUser[0].password;
return savedUser;
}
});
}
}