Skip to content

Instantly share code, notes, and snippets.

@laniltee
Created October 26, 2018 15:54
Show Gist options
  • Save laniltee/29655718f9b7c25bcaceefc83511eda8 to your computer and use it in GitHub Desktop.
Save laniltee/29655718f9b7c25bcaceefc83511eda8 to your computer and use it in GitHub Desktop.
double submit 1
// Validate Credentials
app.post('/home', (req, res) => {
const username = req.body.inputUsername;
const password = req.body.inputPassword;
const sessionID = req.cookies['session-id'];
const cookieToken = req.cookies['csrf-token'];
if (username === 'root' && password === 'root') {
console.log("Home: Logged with valid credentials");
// Generating Session ID and Token
const SESSION_ID = uuidv1();
const CSRF_TOKEN = uuidv4();
if (!sessionID && !cookieToken) {
console.log(`Generated Session ID: ${SESSION_ID}, CSRF Token: ${CSRF_TOKEN}`);
// Setting Cookie on Header
res.setHeader('Set-Cookie', [`session-id=${SESSION_ID}`, `time=${Date.now()}`, `csrf-token=${CSRF_TOKEN}`]);
} else {
console.log('POST /home Some Session ID and CSRF Token Found')
}
res.sendFile('views/form.html', {root: __dirname});
} else {
const error = {status: 401, message: 'Invalid Credentials'};
res.sendFile('views/form-error.html', {root: __dirname});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment