From 50cc236171ccbdafe1a878b287f45af4b9847ce9 Mon Sep 17 00:00:00 2001
From: V S Tharunika <1vstharu279@gmail.com>
Date: Sun, 27 Dec 2020 15:41:45 +0530
Subject: [PATCH] authentication to input added

---
 .../AuthenticationControllerPolicy.js         | 48 +++++++++++++++++++
 server/src/routes.js                          |  3 ++
 2 files changed, 51 insertions(+)
 create mode 100644 server/src/policies/AuthenticationControllerPolicy.js

diff --git a/server/src/policies/AuthenticationControllerPolicy.js b/server/src/policies/AuthenticationControllerPolicy.js
new file mode 100644
index 00000000..93878894
--- /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 ffc356a9..99dd95e2 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)
 }
-- 
GitLab