Login.js
1.35 KB
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
const axios = require("axios");
const express = require("express");
const router = express.Router();
const { login } = require("../libs/E_Campus.js");
const parseICal = require("../libs/ICal.js");
const { jcalToSQL } = require("../libs/MySQL.js");
// (id, pw) => failed | error | {peed, subjects, name, ID}
router.post("/", async (req, res) => {
console.log(`listened /login ${req.body.id} ${req.body.pw}`);
const result = await login(req.body.id, req.body.pw);
if (result && result !== "error") {
try {
const { peed, subjects } = result;
const resUser = await axios.post("http://3.34.173.161:3001/db/users", {
loginID: req.body.id,
loginPW: req.body.pw,
ical: peed,
});
const userID = resUser.data.ID;
const { data: subList } = await axios.post(
"http://3.34.173.161:3001/db/subjects/s",
{
list: subjects,
}
);
await axios.post("http://3.34.173.161:3001/db/user-subject/s", {
userID,
subList,
});
const { data: ical } = await axios({
url: peed,
method: "GET",
responseType: "blob",
});
await jcalToSQL(parseICal(ical), userID);
res.send({ userID });
} catch (e) {
console.log(e);
res.send("error");
}
} else res.send(result ? "failed" : "error");
});
module.exports = router;