litebb/validators/user.validator.js

1const joi = require("joi"); 2 3const signUpNewUser = (req, res, next) => { 4 const schema = joi.object({ 5 username: joi.string().required(), 6 email: joi.string().email().required(), 7 password: joi.string().min(8).max(20).required(), 8 password_confirm: joi 9 .string() 10 .min(8) 11 .max(20) 12 .required() 13 .valid(joi.ref("password")), 14 }); 15 16 const { error, value } = schema.validate(req.body); 17 18 if (error) { 19 // console.log(error.details) 20 req.flash( 21 "error", 22 error.details.map((err) => err.message), 23 ); 24 res.render("signup", { 25 title: "Sign Up", 26 body: req.body, 27 flashes: req.flash(), 28 }); 29 return; 30 } else { 31 req.xop = { 32 username: value.username, 33 password: value.password, 34 email: value.email, 35 }; 36 next(); 37 } 38}; 39 40const loginUser = (req, res, next) => { 41 const schema = joi.object({ 42 username: joi.string().required(), 43 password: joi.string().min(8).max(20).required(), 44 }); 45 46 const { error, value } = schema.validate(req.body); 47 48 if (error) { 49 // console.log(error.details) 50 req.flash( 51 "error", 52 error.details.map((err) => err.message), 53 ); 54 res.render("login", { 55 title: "Log In", 56 body: req.body, 57 flashes: req.flash(), 58 }); 59 return; 60 } else { 61 req.xop = { 62 username: value.username, 63 password: value.password, 64 }; 65 next(); 66 } 67}; 68 69const sendForgotPasswordMail = (req, res, next) => { 70 const schema = joi.object({ 71 email: joi.string().email().required(), 72 }); 73 74 const { error, value } = schema.validate(req.body); 75 76 if (error) { 77 // console.log(error.details) 78 req.flash( 79 "error", 80 error.details.map((err) => err.message), 81 ); 82 res.render("forgot_password", { 83 title: "Forgot Password", 84 body: req.body, 85 flashes: req.flash(), 86 }); 87 return; 88 } else { 89 req.xop = { 90 email: value.email, 91 }; 92 next(); 93 } 94}; 95 96const getResetPasswordForm = (req, res, next) => { 97 const schema = joi.object({ 98 token: joi.string().min(8).max(8).required(), 99 email: joi.string().email().required(), 100 }); 101 102 const { error, value } = schema.validate(req.query); 103 104 if (error) { 105 // console.log(error.details) 106 res.redirect("/login"); 107 return; 108 } else { 109 req.xop = { 110 email: value.email, 111 token: value.token, 112 }; 113 next(); 114 } 115}; 116 117const resetPassword = (req, res, next) => { 118 const schema = joi.object({ 119 token: joi.string().min(8).max(8).required(), 120 email: joi.string().email().required(), 121 password: joi.string().min(8).max(20).required(), 122 password_confirm: joi 123 .string() 124 .min(8) 125 .max(20) 126 .required() 127 .valid(joi.ref("password")), 128 }); 129 130 const { error, value } = schema.validate(req.body); 131 132 if (error) { 133 // console.log(error.details) 134 req.flash( 135 "error", 136 error.details.map((err) => err.message), 137 ); 138 res.render("reset_password", { 139 title: "Reset Password", 140 body: req.body, 141 flashes: req.flash(), 142 }); 143 return; 144 } else { 145 req.xop = { 146 email: value.email, 147 token: value.token, 148 }; 149 next(); 150 } 151}; 152 153const updateUserProfile = (req, res, next) => { 154 const schema = joi.object({ 155 avatar: joi.string().allow(""), 156 website: joi.string().allow(""), 157 }); 158 159 const { error, value } = schema.validate(req.body); 160 161 if (error) { 162 // console.log(error.details) 163 req.flash( 164 "error", 165 error.details.map((err) => err.message), 166 ); 167 168 const url = req.header("Referer") || "/"; 169 res.redirect(url); 170 return; 171 } else { 172 req.xop = { 173 avatar: value.avatar, 174 website: value.website, 175 }; 176 next(); 177 } 178}; 179 180const changeUserPassword = (req, res, next) => { 181 const schema = joi.object({ 182 password_old: joi.string().min(8).max(20).required(), 183 password: joi.string().min(8).max(20).required(), 184 password_confirm: joi 185 .string() 186 .min(8) 187 .max(20) 188 .required() 189 .valid(joi.ref("password")), 190 }); 191 192 const { error, value } = schema.validate(req.body); 193 194 if (error) { 195 // console.log(error.details) 196 req.flash( 197 "error", 198 error.details.map((err) => err.message), 199 ); 200 201 const url = req.header("Referer") || "/"; 202 res.redirect(url); 203 return; 204 } else { 205 req.xop = { 206 password_old: value.password_old, 207 password: value.password, 208 }; 209 next(); 210 } 211}; 212module.exports = { 213 signUpNewUser, 214 loginUser, 215 sendForgotPasswordMail, 216 getResetPasswordForm, 217 resetPassword, 218 updateUserProfile, 219 changeUserPassword, 220};