-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
151 lines (131 loc) · 4.33 KB
/
users.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// User POST endpoint
const {response} = require("express");
const db = require("../db/userdb");
const roomdb = require("../db/roomdb");
const io = require("../io-manager");
// Post to /user, accepts input in JSON format
exports.createUser = (req, res) => {
let user = {
username: req.body.username,
password: req.body.password,
avatar: `https://ui-avatars.com/api/size=256&name=${req.body.username}`,
};
db.createUser(user, (err, user) => {
if (!err) {
roomdb.authorizeRoomAccess(user.username, "public", "");
}
});
res.redirect("/login");
};
// User GET API endpoint
// Takes username as path variable, e.g. /user/john_doe
exports.getUser = (req, res) => {
let username = req.params.username;
db.getUser(username, (err, user) => buildResponse(res, err, user));
};
// User PATCH endpoint
// Patch to /user/username
exports.updateUser = (req, res) => {
let username = req.params.username;
let updates = {
avatar: req.body.avatar,
username,
password: req.body.password,
// joined_rooms: req.body.joined_rooms, // Joined rooms will all be handled from the server. The client shouldn't be allowed to change these values.
};
if (updates.avatar) {
//Get applicable socket for the user and update the socket's avatar.
let client = io.getClientByUsername(username);
if (client) {
client.getSocket().avatar = updates.avatar;
}
}
db.updateUser(updates, (err, user) => buildResponse(res, err, user));
};
// User DELETE endpoint
// Delete at /user/username
exports.deleteUser = (req, res) => {
let username = req.params.username;
db.deleteUser(username, (err, user) => buildResponse(res, err, user));
};
exports.setTheme = (req, res) => {
let username = req.params.username;
let theme = req.params.theme;
db.updateUser({username, theme}, (err, user) => buildResponse(res, err, user));
};
// User GET authentication endpoint
// Authenticate /user/authenticate
exports.authenticateUser = (req, res) => {
let auth = btoa(req.headers.Authorization.substring(6)).split(":"); // Remove leading 'Basic ' and convert from base64
let username = auth[0];
let password = auth.slice(1).join(":");
db.authenticateUser(username, password, (err, user) =>
buildAuthResponse(res, err, user)
);
};
const buildCreationResponse = (res, err, room) => {
buildResponse(res, err, room, true);
};
const buildResponse = (res, err, user, created = false) => {
let response;
let user_path = user ? "/" + encodeURIComponent(user.username) : "";
if (err) {
response = {
timestamp: new Date().toISOString(),
path: "/user" + user_path,
error: err.message,
};
if (err.message == "User not found") {
response.status = 404;
} else if (err.message == "User already exists") {
response.status = 403;
} else {
response.status = 500;
}
} else {
response = {
timestamp: new Date().toISOString(),
status: 200,
path: "/user" + user_path,
};
if (user) {
let tempUser = {
username: user.username,
joined_rooms: user.joined_rooms,
avatar: user.avatar,
theme: user.theme,
rooms: user.rooms
}; //Make sure not to send back the password!
response.data = tempUser;
}
if (created) response.status = 201;
}
res.status(response.status);
res.json(response);
};
const buildAuthResponse = (res, err, authorities) => {
let response;
if (err) {
response = {
timestamp: new Date().toISOString(),
path: "/user/authenticate",
error: err.message,
};
if (err.message == "User not found") {
response.status = 404;
} else if (err.message == "Invalid credentials") {
response.status = 401;
} else {
response.status = 500;
}
} else {
response = {
timestamp: new Date().toISOString(),
status: 200,
path: "/user/authenticate",
authorities: authorities,
};
}
res.status(response.status);
res.json(response);
};