ADRE9 / bunk-manager-mern

Building React Web App to manage attendance in a university.
https://salty-brook-29410.herokuapp.com/
MIT License
129 stars 67 forks source link

Add validator to every required field in the form like Email, username, password etc using express-validator #77

Open Mustafiz04 opened 3 years ago

Mustafiz04 commented 3 years ago

Write validator function as middleware. It should be before the actual controller function in every routes that take input from client side.

For example -

Email - should be email with proper formate Username - should be unique and at-least 6 character long Password - should be 8 character long which include at-least 1 special character, 1 capital letter Semester - should not be greater than 8

and so on.

Have any doubt feel free to ask her. Good Luck

Muditxofficial commented 3 years ago

@Mustafiz04 i would like to work on this issue.

Mustafiz04 commented 3 years ago

Yes you can @Muditxofficial I am assigning to you.

Muditxofficial commented 3 years ago

@Mustafiz04 What should i do about error messages should i just console log it. Also, i have to use express-validator right just to confirm as i need to install it .

Mustafiz04 commented 3 years ago

@Muditxofficial return the message in similar way we return the data that can be used by frontend to display that message. Yes, use express-validator.

https://express-validator.github.io/docs/check-api.html for reference

don't forget to use it as middleware.

Mustafiz04 commented 3 years ago

@Muditxofficial How you work going on ??

Muditxofficial commented 3 years ago

@Mustafiz04 I have started doing login but on which route should i check it. github2 http://localhost:3000/api/auth/login does not work

Mustafiz04 commented 3 years ago

Sorry for late. Wait I am explaining you.

Mustafiz04 commented 3 years ago

Validator file

const { check, validationResult } = require('express-validator');

exports.validateSignupRequest = [
    check('name').notEmpty().withMessage("First name is required"),
    check('email').isEmail().withMessage("Email is required"),
    check('password').isLength({min : 6}).withMessage("Password must be atleast 6 character long")
];

exports.isRequestValidated = (req, res, next) => {
    const errors = validationResult(req);
    if( errors.array().length > 0 ){
        return res.status(400).json({
            errors : errors.array()[0]
        })
    }
    next();
}

in route file

const { validateSignupRequest, isRequestValidated } = require('./../../middleware/validator/auth');

router.post('/signup', validateSignupRequest, isRequestValidated, signup );

Apply to all route with take input from frontend.

Muditxofficial commented 3 years ago

@Mustafiz04 Is there a way to check if my implementation is right using postman ? i found two different url localhost:3000/user/signup and localhost:3000/api/auth/new/signup

Mustafiz04 commented 3 years ago

yes you can check every API on postman. localhost:3000/api/auth/new/signup this is correct I give only example how to write validator and apply that to route as middleware. Follow that ways, create and apply middleware to every route.

Mustafiz04 commented 3 years ago

When will you complete this task because it take lots of time.

Muditxofficial commented 3 years ago

Validator

 const { check, validationResult } = require('express-validator');

  exports.validateSignupRequest = [
      check('name').notEmpty().withMessage("First name is required"),
      check('email').isEmail().withMessage("Email is required"),
      check('password').isLength({min : 6}).withMessage("Password must be atleast 6 character long")
  ];

exports.validateLogin = [
    check('email').isEmail().withMessage("Email is required"),
    check('password').isLength({min : 6}).withMessage("Password must be atleast 6 character long")
]

exports.isRequestValidated = (req, res, next) => {
    const errors = validationResult(req);
    if( errors.array().length > 0 ){
        console.log(errors.array()[0])
        return res.status(400).json({
            errors : errors.array()[0]
        })
    }
    next();
}

Routes

const { validateSignupRequest, isRequestValidated, validateLogin } = require("../middlewares/validators")
router.post('/api/auth/login',isRequestValidated,validateLogin,loginUser);
router.post('/api/auth/new/signup',validateSignupRequest,isRequestValidated,createUser);

Is it ok or should i add something?

Mustafiz04 commented 3 years ago

correct but isRequestValidated always after validator function like validateLogin