import jwt from "jsonwebtoken"; export const validateRegister = (req, res, next) => { // full name empty if (!req.body.fullName) { return res.status(400).send({ message: 'Please enter a full name', }); } // rather an email nor a phonenumber if (!req.body.email && !req.body.phonenumber) { return res.status(400).send({ message: 'Please enter an email or a phonenumber', }); } // validate email if (req.body.email && !(validateEMail(req.body.email))) { return res.status(400).send({ message: 'Please choose a valid email', }); } // validate phonenumber if (req.body.phonenumber && !(validatePhonenumber(req.body.phonenumber))) { return res.status(400).send({ message: 'Please choose a valid phonenumber', }); } // username min length 5 if (!req.body.username || req.body.username.length < 5) { return res.status(400).send({ message: 'Please enter an username with min. 5 chars', }); } // password min 8 chars if (!req.body.password || req.body.password.length < 8) { return res.status(400).send({ message: 'Please enter a password with min. 8 chars', }); } // password (repeat) must match if (req.body.password != req.body.password_repeat) { return res.status(400).send({ message: 'Both passwords must match', }); } next(); }; export const isLoggedIn = (req, res, next) => { if (!req.headers.authorization) { return res.status(400).send({ message: 'Your session is not valid!', }); } try { const authHeader = req.headers.authorization; const token = authHeader.split(' ')[1]; const decoded = jwt.verify(token, 'SECRETTUEITKEY'); req.userData = decoded; next(); } catch (err) { return res.status(400).send({ message: 'Authority to access this resource missing', }); } } // to validate the email function validateEMail(email) { const emailRegex = /^[A-Za-z0-9_!#$%&'*+\/=?`{|}~^.-]+@[A-Za-z0-9.-]+$/; if (email === "") { return true; } return emailRegex.test(email); }; // to validate the phonenumber function validatePhonenumber(number) { const mobilephonenumberRegex = new RegExp(/^(((((00|\+){1,2})49([ \-/]?|[ \()/]?))|0)[1]([0-2]|[4-7])([0-9]{1})([ \-/]?|[ \()/]?)([0-9]{8}))$/, "gm"); const landlineRegex = new RegExp(/^0(30|40|69|89|20[12389]|21[24]|228|234|335|340|3[3-9][15]|906|[2-9][1-9]1|3(?:3(?:[0237][127]|0[3-6]|2[89]|3[458]|[46][1246]|7[589]|8[1256]|9[145])|4(?:[2347][135]|[34]7|[69][146]|48|62|76|93)|5(?:[04679][14]|[238][135]|[2469]2|[4789]6|[278]8|63|73|37|29)|6(?:[024689][13]|[03789]5|06|2[2489]|3[1246]|44|47|7[1279]|82|86)|7(?:[236][135]|2[2467]|37|4[145]|62|64|7[1-4])|8(?:[234678]1|3[468]|4[347]|6[035-9]|7[467]|83|86)|9(?:[236][135]|[04689]4|[04789]1|[03468]7|02|09|28|4[369]|6[2689]|73|76|96|98))|3[3-9][0-9]{3}|13[078]|180[235]|18[01]|19[0-9]{3}|[789]00|[2-9][0-9]{3})([0-9]{1,})$/, "gm"); if (number === "") { return true } if (mobilephonenumberRegex.test(number)) { return true } else if (landlineRegex.test(number)) { return true; } return false; };