-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.js
113 lines (102 loc) · 2.87 KB
/
render.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const config = require("../config.json");
const {User} = require("../conf/mongo_conf");
const userdb = require("../db/userdb");
let allowed;
exports.index = (req, res) => {
res.render("index", {
title: "Home",
config: config,
});
};
exports.login = (req, res) => {
let dat = {
title: "Login",
config: config,
invalid: (Object.keys(req.query).length > 0 && req.query.invalid.length >= 0)
};
res.render("login", dat);
};
exports.signUp = (req, res) => {
res.render("create", {
title: "Sign Up",
config: config,
});
};
exports.rooms = (req, res) => {
if (!req.session.user) {
res.redirect("/login");
return;
}
User.findOne(
{
username: req.session.user.username,
},
(err, user) => {
if (err) {
res.json(err);
return;
}
if (!user) {
res.redirect("/login");
return;
}
res.render("room", {
title: "Room",
users: user,
//The user should exist and be passed into the render
// Since this should be an authenticated end-point, this works.
// TestUser1's password is testing123
// Other accounts should be able to be created using the sign up page.
username: user.username,
avatar: user.avatar,
theme: user.theme,
id: user._id
});
}
);
};
exports.checkAccess = (req, res) => {
if (req.body.username == "" || req.body.password == null) {
res.redirect("/login?invalid");
return;
}
let userName = req.body.username;
let userPassword = req.body.password;
console.log("Attempting to authenticate user " + userName);
userdb.authenticateUser(userName, userPassword, (err, user) => {
if (err) {
if (
err.message == "Invalid credentials" ||
err.message == "User not found"
) {
res.redirect("/login?invalid");
return;
}
}
console.log("User authenticated!");
req.session.user = {
isAuthenticated: true,
username: req.body.username,
theme: req.body.theme
};
allowed = userName;
res.redirect("/app");
});
};
exports.logout = (req, res) => {
req.session.destroy((err) => {
if (err) {
console.log(err);
} else {
res.redirect("/");
}
});
};
exports.delete = function (req, res) {
userdb.deleteUser(req.params.id, (err, user) => {
if(err) return console.error(err);
console.log("Deleted user: " + user.username);
req.session.destroy();
res.json({success: true});
});
};