forked from hkirat/full-stack-assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (104 loc) · 3.33 KB
/
index.js
File metadata and controls
124 lines (104 loc) · 3.33 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
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
const express = require('express');
const bodyParser = require('body-parser');
const crypto = require('crypto');
const app = express();
const port = 3000;
// Add middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Use generateToken function to generate random token of string
const generateToken = () => {
const token = crypto.randomBytes(32).toString('hex');
return token;
};
const USERS = [
{
email: 'admin',
password: 'adminpw',
isAdmin: true,
},
];
const QUESTIONS = [
{
title: 'Two states',
description: 'Given an array , return the maximum of the array?',
testCases: [
{
input: '[1,2,3,4,5]',
output: '5',
},
],
},
];
const SUBMISSION = [];
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.post('/signup', function (req, res) {
// Add logic to decode body
const { email, password } = req.body;
// body should have email and password
if (!email || !password) {
return res.status(400).send('Missing email or password');
}
//Store email and password (as is for now) in the USERS array above (only if the user with the given email doesnt exist)
// Check if user with given email already exists
const user = USERS.find((user) => user.email === email);
if (user) {
return res.status(401).send('User already exists');
}
// Add new user to USERS array
const newUser = { email, password };
USERS.push(newUser);
console.log(USERS);
// return back 200 status code to the client
res.status(200).send('User registered successfully');
});
app.post('/login', function (req, res) {
// Add logic to decode body
const { email, password } = req.body;
// body should have email and password
if (!email || !password) {
return res.status(400).send('Missing email or password');
}
// Check if the user with the given email exists in the USERS array
const user = USERS.find((user) => user.email === email);
if (!user) {
return res.status(401).send('Invalid email or password');
}
// Also ensure that the password is the same
// If the password is not the same, return back 401 status code to the client
if (user.password !== password) {
return res.status(401).send('Invalid email or password');
}
// If the password is the same, return back 200 status code to the client
// Also send back a token (any random string will do for now)
if (user.isAdmin) {
res
.status(200)
.send({ message: 'Login successful as admin', token: generateToken() });
} else {
res
.status(200)
.send({ message: 'Login successful', token: generateToken() });
}
});
app.get('/questions', function (req, res) {
//return the user all the questions in the QUESTIONS array
res.status(200).send(QUESTIONS);
});
app.get('/submissions', function (req, res) {
// return the users submissions for this problem
res.send('Hello World from route 4!');
});
app.post('/submissions', function (req, res) {
// let the user submit a problem, randomly accept or reject the solution
// Store the submission in the SUBMISSION array above
res.send('Hello World from route 4!');
});
// leaving as hard todos
// Create a route that lets an admin add a new problem
// ensure that only admins can do that.
app.listen(port, function () {
console.log(`Example app listening on port ${port}`);
});