If user experience is what you think about, page redirection is strong. Providing a smooth flow for people visiting your site is important to prevent drops, and since the forms are not so pleasant to fill out, their use is basic. You do not want to reload your page, which might take some time to load only the error message. Once the form is valid and you have created the user's cookie, the redirect will be fine, although even if you can do something in the client application to prevent it, itβs out of scope.
As pointed out by Levent, you should check the express-validator , which is a more established solution for this kind of purpose.
req.check('profileRealName', 'Bad name provided').notEmpty().isAlpha() req.check('profileLocation', 'Invalid location').optional().isAlpha(); req.getValidationResult().then(function (result) { if (result.isEmpty()) { return null } var errors = result.array() // [ // { param: "profileRealName", msg: "Bad name provided", value: ".." }, // { param: "profileLocation", msg: "Invalid location", value: ".." } // ] res.status(400).send(errors) }) .then(function () { // everything is fine! insert into the DB and respond.. })
Be that as it may, I can assume that you are using MongoDB. Given this, I would recommend using ODM like Mongoose . This will allow you to define models for your circuits and set limits directly on it, allowing the models to handle these redundant checks for you.
For example, a model for your user might be
var User = new Schema({ name: { type: String, required: [true, 'Name required'] }, bio: { type: String, match: /[az]/ }, age: { type: Number, min: 18 },
Using this pattern on your route will look like
var user = new User({ name: form.profileRealName, bio: form.profileBio, url: form.profileUrl, location: form.profileLocation }) user.save(function (err) {
As you can see, this provides a pretty cool api that you should read about in your docs if you want to know more.
About CRSF, you should install csurf , which has pretty good instructions and usage examples in their readme.
After that you did a very good job, I donβt think about doing without having to keep track of your critical dependencies if day 0 occurs, for example the one that was shown in 2015 with JWT, but this is still rare.