-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
62 lines (54 loc) · 1.42 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {
createSession,
getUser,
getSessionInfo,
getEssayInfo,
} from "./database/model";
const crypto = require("crypto");
import bcrypt from "bcryptjs";
import Cookies from "cookies";
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;
}
});
}
}
export async function cookiesTampered(req, res) {
const sid = req.cookies.sid;
const essayId = req.cookies.currEssay;
const cookies = new Cookies(req, res);
if (!sid || !essayId) {
cookies.set("currEssay");
return true;
}
const essayInfo = await getEssayInfo(essayId);
const userData = await getSessionInfo(sid);
const user_id = JSON.parse(userData.data).user_id;
if (user_id === essayInfo.user_id) {
return false;
} else {
cookies.set("currEssay");
return true;
}
}