npm i jsonwebtoken
npm i cookie-parser
Creating a User
After creating a user correctly, we get a message saying that a user has been created.
Ensuring user creation correctness
In validator/index.js, we have function userSignupValidator function that ensures we check for the format of the data for when user is created.
We use this function as a middleware in
routes/auth.js
1 |
router.post('/signup', userSignupValidator, signup) |
This means that userSignupValidator is used to validate the incoming JSON’s properties. Only when it passes do we continue on to signup function.
validator/index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
exports.userSignupValidator = (req, res, next) => { console.log(`validator/index.js - userSignupValidator`); req.check("name", "Name is required").notEmpty() console.log(`name is not null and between 4-10 char √`); req.check("email", "Email must be between 3 to 32 characters") .matches(/.+\@.+..+/) .withMessage("email must contain @") .isLength({ min: 4, max: 50 }) console.log(`check for email √`); req.check("password", "Password is required").notEmpty(); req.check('password') .isLength({min: 6}) .withMessage("Password must contain at least 6 characters") .matches(/\d/) .withMessage("Password must contain a number") console.log(`check for password √`); const errors = req.validationErrors() if (errors) { const firstError = errors.map(error => error.msg)[0]; return res.status(400).json({error: firstError}); } console.log(`validator/index.js - no errors, moving on √`) next(); } |
Be sure that you include a number inside your password:
Make sure you include a name when you register for an account:
For your email, ensure to have it in an email format:
Successful Signin
In order to sign in, use /signin, then click on tab Body, choose raw and then JSON format.
Type in a JSON object with key/value “email” and “password”. Then click send. If your credentials are correct, you’ll get an object back with a token key/value.
Copy and paste the value into Headers. In Headers, create a key called “Authorization”.
In the value, type “Bearer ” with a space at the end. Then copy the token behind it.
You are not set up to create/update/delete posts, get/update/delete your own user data.