Repository navigation
Add CSRF and Flash Message Support
Bedrock now has CSRF middleware to help with request forgeries, and flash message support. The following npm modules were used:
Refer to their READMEs for more information.
CSRF
To enable CSRF on your forms, you can add a hidden input field with its value set to {{_csrf}} to your forms. The CSRF check will happen automatically, no need for you to do anything in your route handlers.
<input type="text" value="{{_csrf}}" hidden>You should do this to all forms that change the state of your application (anything except GET requests, basically).
Flash Messaging
Flash messages allow your application to send a one-time message to the client. For example, if a user submits a form and they forget a field, you may want to send a flash message back saying "You need to fill out all fields".
You can now do this via the flash() method on the request object:
router.post('/submit', function (req, res) {
if (someConditionIsNotMet) {
req.flash('error', 'You need to fill out all fields.');
return res.redirect('back');
}
});A Handlebars partial called flash.handlebars has been included with Bedrock that will render out an alert view with the appropriate style and message. You'll need to add this partial in your layout file or your handlebars views (via {{> flash}}).
Check out the Bedrock Website for more documentation and instructions on how to use Bedrock in your next NodeJS web app.