-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthentication.js
44 lines (36 loc) · 1.29 KB
/
authentication.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
const db = require("./db.js");
const TEST_MODE = true; //trueにすると、cookie認証が必ず成功するようになります。
const auth = async (req) => {
return new Promise((resolve) => {
if (TEST_MODE) return resolve(true);
const cookieRoot =
req.headers.cookie !== undefined ? req.headers.cookie : false;
if (!cookieRoot) return resolve(false);
const cookieDataList = cookieRoot
.split(";")
.map((cookieData) => cookieData.trim());
const fullName = cookieDataList.find((cookieData) =>
cookieData.startsWith("name=")
);
const fullPassword = cookieDataList.find((cookieData) =>
cookieData.startsWith("password=")
);
if (fullName === undefined || fullPassword === undefined) {
return resolve(false);
}
const name = decodeURIComponent(fullName.split("=")[1]);
const password = decodeURIComponent(fullPassword.split("=")[1]);
db.mysql_connection.connect((err) => {
const placeholder = [name, password];
db.mysql_connection.query(
"SELECT * FROM users WHERE name = ? AND password = ?;",
placeholder,
(err, result) => {
if (err) return resolve(false);
return resolve(result.length !== 0 ? true : false);
}
);
});
});
};
module.exports = auth;