Skip to content

Instantly share code, notes, and snippets.

@laniltee
Created October 26, 2018 15:20
Show Gist options
  • Save laniltee/59ce18df5996141d6abcc8de6a661a16 to your computer and use it in GitHub Desktop.
Save laniltee/59ce18df5996141d6abcc8de6a661a16 to your computer and use it in GitHub Desktop.
validate credentials
// Validate Credentials
app.post('/home', (req, res) => {
const username = req.body.inputUsername;
const password = req.body.inputPassword;
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();
console.log(`Generated Session ID: ${SESSION_ID}, CSRF Token: ${CSRF_TOKEN}`);
// Saving token with session ID
SESSION_IDS[SESSION_ID] = CSRF_TOKEN;
// Setting Cookie on Header
res.setHeader('Set-Cookie', [`session-id=${SESSION_ID}`, `time=${Date.now()}`]);
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