diff --git a/server/src/policies/AuthenticationControllerPolicy.js b/server/src/policies/AuthenticationControllerPolicy.js new file mode 100644 index 0000000000000000000000000000000000000000..93878894687a27b25e38ae2500e7956f22ebcab9 --- /dev/null +++ b/server/src/policies/AuthenticationControllerPolicy.js @@ -0,0 +1,48 @@ +const Joi = require('joi') + + +module.exports={ + register(reg, res, next){ + + const schema ={ + email: Joi.string().email(), + password: Joi.string().regex( + new regExp('^[a-zA-Z0-9]{8-32}$') + ) + } + + const {error, value} = Joi.Validate(req.body, schema) + + if(error){ + switch(error.details[0].context.key){ + + case 'email': + res.status(400).send({ + error: 'You must provide a valid email address' + }) + break + + case 'password': + res.status(400).send({ + error: ' The password provided failed to match the following rules <br> 1.It must contain only the following characters: lower case, upper case, numerics <br> 2.It must be at least 8 characters in lenght and not greater than 32 characters' + + }) + + break + + default: + res.status(400).send({ + error:'Invalid registration information' + }) + + } + + + } + else{ + next() + } + + + } +} diff --git a/server/src/routes.js b/server/src/routes.js index ffc356a941f31412520627b348f4deb14d44a20d..99dd95e2f53724e6a83e4ac4e0b7c4517237106f 100644 --- a/server/src/routes.js +++ b/server/src/routes.js @@ -1,7 +1,10 @@ const AuthenticationController = require('./controllers/AuthenticationController') +const AuthenticationControllerPolicy = require('./policies/AuthenticationControllerPolicy') + module.exports = (app) => { app.post('/register', + AuthenticationControllerPolicy.register, AuthenticationController.register) }