-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
172 lines (161 loc) · 4.04 KB
/
server.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const jsonServer = require("json-server");
const server = jsonServer.create();
const middlewares = jsonServer.defaults();
// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares);
// To handle POST, PUT and PATCH you need to use a body-parser
server.use(jsonServer.bodyParser);
const players = [
{
id: 0,
username: "harish.kulur",
password: "secret",
showEmailPhoneScreen: true,
showTermsAndCondition: false,
showWelcomeScreen: true
},
{
id: 1,
username: "johnny.mirkovic",
password: "secret",
showEmailPhoneScreen: false,
email: "[email protected]",
phone: "46-700111000",
acceptMarketing: false,
showTermsAndCondition: true,
showWelcomeScreen: false
},
{
id: 2,
username: "murat.bosnak",
password: "secret",
showEmailPhoneScreen: false,
email: "[email protected]",
phone: "46-700111000",
showTermsAndCondition: false,
acceptMarketing: true,
acceptTerms: true,
showWelcomeScreen: false
},
{
id: 3,
username: "majid.parvin",
password: "secret",
showEmailPhoneScreen: false,
email: "[email protected]",
phone: "46-700111000",
showTermsAndCondition: false,
acceptMarketing: true,
acceptTerms: true,
showWelcomeScreen: true
}
];
const authenticate = (req, res) => {
const username = req.body.username;
const password = req.body.password;
const player = players.find(player => player.username === username);
if (player && player.password === password) {
const response = { ...player };
delete response.password;
res.status(200).json({
status: "SUCCESS",
response
});
} else if (!player) {
const newPlayer = {
id: players.length,
username,
password,
showEmailPhoneScreen: true,
showTermsAndCondition: false,
showWelcomeScreen: false
};
players.push(newPlayer);
const response = { ...newPlayer };
delete response.password;
res.status(200).json({
status: "SUCCESS",
response
});
} else {
res.status(401).json({
status: "FAILURE",
response: {
errorKey: "INVALID_CREDENTIALS",
errorDescription: "Invalid username and password"
}
});
}
};
const playerAttributes = (req) => {
let overrides = {}
const { email, acceptTerms } = req.body
if (email) {
overrides = {
showTermsAndCondition: true,
showEmailPhoneScreen: false
}
}
if (acceptTerms) {
overrides = {
showTermsAndCondition: false,
showEmailPhoneScreen: false,
showWelcomeScreen: true
}
}
return overrides
}
const updatePlayer = (req, res) => {
const id = req.body.id;
const playerIndex = players.findIndex(player => player.id === id);
if (playerIndex > -1) {
let newPlayer = { ...players[playerIndex], ...req.body };
// Adding the override to let user navigate further
const overrides = playerAttributes(req)
newPlayer = {...newPlayer, ...overrides}
players[playerIndex] = newPlayer;
const response = {...newPlayer};
delete response.password;
res.status(200).json({
status: "SUCCESS",
response
});
} else {
res.status(200).json({
status: "FAILURE",
response: {
errorKey: "PLAYER_NOT_FOUND",
errorDescription: "Player not found"
}
});
}
};
server.use((req, res, next) => {
if (req.method === "POST") {
if (req.path === "/authenticate") {
return authenticate(req, res);
} else if (req.path === "/logout") {
const user = players.filter(player => player.id === req.body.id)[0]
if (user) {
res.status(200).json({
status: "SUCCESS"
});
} else {
res.status(400).json({
status: "FAILURE",
error: "Username do not match!"
});
}
}
}
if (req.method === "PUT") {
if (req.path === "/player") {
return updatePlayer(req, res);
}
}
// Continue to JSON Server router
next();
});
server.listen(3003, () => {
console.log("JSON Server is running at 3003");
});