Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that the user receives the authenticated user token. #312

Open
gauravjaincr7 opened this issue Apr 17, 2023 · 2 comments
Open

Ensure that the user receives the authenticated user token. #312

gauravjaincr7 opened this issue Apr 17, 2023 · 2 comments

Comments

@gauravjaincr7
Copy link

No description provided.

@Himanshu-holmes
Copy link

`const express = require('express');
const app = express();
const port = 3000;

const USERS = [];

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 SUBMISSIONS = [];

app.use(express.json());

app.post('/signup', function(req, res) {
const { email, password } = req.body;
console.log("got log" , req.body)

if (!email || !password) {
return res.status(400).send('Email and password are required');
}

const user = USERS.find(user => user.email === email);

if (user) {
return res.status(409).send('User already exists');
}

USERS.push({ email, password });

res.sendStatus(200);
});

app.post('/login', function(req, res) {
const { email, password } = req.body;

if (!email || !password) {
return res.status(400).send('Email and password are required');
}

const user = USERS.find(user => user.email === email);

if (!user || user.password !== password) {
return res.status(401).send('Invalid email or password');
}

const token = Math.random().toString(36).substring(7);

res.json({ token });
});

app.get('/questions', function(req, res) {
res.json(QUESTIONS);
});

app.get('/submissions', function(req, res) {
const { problemIndex } = req.query;

if (!problemIndex || isNaN(problemIndex)) {
return res.status(400).send('Invalid problem index');
}

const submissions = SUBMISSIONS.filter(submission => submission.problemIndex === Number(problemIndex));

res.json(submissions);
});

app.post('/submissions', function(req, res) {
const { problemIndex, code } = req.body;

if (!problemIndex || isNaN(problemIndex)) {
return res.status(400).send('Invalid problem index');
}

if (!code) {
return res.status(400).send('Code is required');
}

const accepted = Math.random() < 0.5;

const submission = {
problemIndex: Number(problemIndex),
code,
accepted
};

SUBMISSIONS.push(submission);

res.json(submission);
});

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

@Sagar2506
Copy link

There seems to be a syntax error in the code
The console.log() statement in the last line of the code is not properly formatted
const express = require('express');
const app = express();
const port = 3000;

const USERS = [];

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 SUBMISSIONS = [];

app.use(express.json());

app.post('/signup', function(req, res) {
const { email, password } = req.body;
console.log("got log", req.body)

if (!email || !password) {
return res.status(400).send('Email and password are required');
}

const user = USERS.find(user => user.email === email);

if (user) {
return res.status(409).send('User already exists');
}

USERS.push({ email, password });

res.sendStatus(200);
});

app.post('/login', function(req, res) {
const { email, password } = req.body;

if (!email || !password) {
return res.status(400).send('Email and password are required');
}

const user = USERS.find(user => user.email === email);

if (!user || user.password !== password) {
return res.status(401).send('Invalid email or password');
}

const token = Math.random().toString(36).substring(7);

res.json({ token });
});

app.get('/questions', function(req, res) {
res.json(QUESTIONS);
});

app.get('/submissions', function(req, res) {
const { problemIndex } = req.query;

if (!problemIndex || isNaN(problemIndex)) {
return res.status(400).send('Invalid problem index');
}

const submissions = SUBMISSIONS.filter(submission => submission.problemIndex === Number(problemIndex));

res.json(submissions);
});

app.post('/submissions', function(req, res) {
const { problemIndex, code } = req.body;

if (!problemIndex || isNaN(problemIndex)) {
return res.status(400).send('Invalid problem index');
}

if (!code) {
return res.status(400).send('Code is required');
}

const accepted = Math.random() < 0.5;

const submission = {
problemIndex: Number(problemIndex),
code,
accepted
};

SUBMISSIONS.push(submission);

res.json(submission);
});

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants