Skip to content

Instantly share code, notes, and snippets.

@laniltee
Created October 26, 2018 15:30
Show Gist options
  • Save laniltee/7d880638f9346fd1d4de056c6d6c0e14 to your computer and use it in GitHub Desktop.
Save laniltee/7d880638f9346fd1d4de056c6d6c0e14 to your computer and use it in GitHub Desktop.
form validation with session
// Submit Form Data
app.post('/posts', (req, res) => {
const inputTitle = req.body.inputTitle;
const inputContent = req.body.inputContent;
const inputToken = req.body.inputToken;
const sessionID = req.cookies['session-id'];
// Checking if Session ID matches CSRF Cookie
if (SESSION_IDS[sessionID] && SESSION_IDS[sessionID] === inputToken) {
console.log("Post Content: Valid Session Found !");
res.sendFile('views/form-success.html', {root: __dirname});
} else {
console.log("Post Content: No Valid Session Found !");
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