-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprofile.js
62 lines (58 loc) · 2.29 KB
/
profile.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
// this file has functions to exectute the second half of the OAuth after the request to Ion API has been completed
const Client = require('pg').Client;
const {pl} = require('./routes/sql');
async function check(user_data, req, res) {
try {
// communicate with the database
const cl = await pl.connect();
// fetch our user from the database
// prompt other behavior if they're new
let results = await cl.query("SELECT * FROM users WHERE id = ($1)", [user_data.id]);
if (results.rows.length == 0) { // first time user
cl.release();
req.session.user_data = user_data;
res.redirect("/start");
} else {
cl.release();
req.session.name = results.rows[0].display_name;
req.session.username = results.rows[0].username;
req.session.userid = results.rows[0].id;
req.session.admin = results.rows[0].admin;
req.session.usaco_div = results.rows[0].usaco_division;
req.session.cf_handle = results.rows[0].cf_handle;
req.session.tjioi = Math.floor(parseInt(req.session.userid) / 100) >= 2024 && Math.floor(parseInt(req.session.userid) / 100) < 10000;
if (req.session.admin) {
req.session.name = "[Admin] " + req.session.name;
}
req.session.loggedin = true;
if (req.session.mobile) {
res.redirect("/grade/attendance");
} else {
res.redirect("/grade/profile");
}
}
} catch (error) {
res.redirect("/");
console.log(error);
}
}
// file in the data into the database for a first-time user
async function populate(user_data, req, res) {
try {
const cl = await pl.connect();
await cl.query("INSERT INTO users VALUES ($1, $2, $3, $4, $5, $6)", [user_data.id, user_data.display_name, user_data.ion_username, false, user_data.email, user_data.pass]);
cl.release();
await check(user_data, req, res);
} catch (error) {
console.log(error);
res.send(error);
}
}
module.exports = {
check: (user_data, req, res) => {
return check(user_data, req, res);
},
populate: (user_data, req, res) => {
return populate(user_data, req, res);
}
}