Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,38 @@ const SUBMISSION = [
]

app.post('/signup', function(req, res) {
// Add logic to decode body
// body should have email and password
const {username, email, password} = req.body;
try{
let emailExists = USERS.some(user => user.email === email);
if(!emailExists){
USERS.push({username: username, email:email, password:password})
res.status(201).json({'User created'})
}else{
res.status(409).json({ error: 'user with this email already exists' });
}
}catch(error){
res.status(500).json({ error: 'An error occured' });
}})


//Store email and password (as is for now) in the USERS array above (only if the user with the given email doesnt exist)


// return back 200 status code to the client
res.send('Hello World!')
})

app.post('/login', function(req, res) {
// Add logic to decode body
// body should have email and password

// Check if the user with the given email exists in the USERS array
// Also ensure that the password is the same


// 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 the password is not the same, return back 401 status code to the client


res.send('Hello World from route 2!')
{email, password} = req.body;
function generateRandomString(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let randomString = '';
for (let i = 0; i < length; i++) {
randomString += characters.charAt(Math.floor(Math.random() * characters.length));
}
return randomString;
}

const token = generateRandomString(10);
let emailPassExists = USERS.some(user => user.email === email && user.password === password);
if(emailPassExists){
res.status(201).json(token)
}else{
res.status(401).send('Invalid email or password');
}
})

app.get('/questions', function(req, res) {
Expand All @@ -70,4 +77,4 @@ app.post("/submissions", function(req, res) {

app.listen(port, function() {
console.log(`Example app listening on port ${port}`)
})
})