Advertisement
Guest User

Untitled

a guest
Apr 11th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. router.get('/perfil/edit/:id', isAuthenticated, async (req, res) => {
  2.   res.render('perfil/edit-profile', { user: await User.findById(req.params.id) });
  3. });
  4.  
  5. router.put('/perfil/edit-profile/:id', isAuthenticated, async (req, res) => {
  6.   let { usuario, nombre, bloque, conjunto, email, telefono, password, confirm_password } = req.body;
  7.   let errors = [];
  8.   if (!usuario) errors.push({ text: 'Por favor ingresa tu nombre de usuario' });
  9.   if (!nombre) errors.push({ text: 'Por favor ingresa tu nombre completo' });
  10.   if (!bloque) errors.push({ text: 'Por favor ingresa tu bloque y casa' });
  11.   if (!email) errors.push({ text: 'Por favor ingresa tu correo electronico' });
  12.   if (!telefono) errors.push({ text: 'Por favor ingresa tu celular' });
  13.   if (!password) errors.push({ text: 'Por favor ingresa tu contraseña' });
  14.   if (!confirm_password) errors.push({ text: 'Por favor confirma tu contraseña' });
  15.   if (password.length < 8) errors.push({ text: 'Tu contraseña debe ser mayor a 8 caracteres' });
  16.   if (password != confirm_password) errors.push({ text: 'Las contraseñas no coinciden' });
  17.   if (errors.length > 0) res.render('/perfil/edit-profile/:id', { errors, usuario, nombre, bloque, conjunto, email, telefono, password, confirm_password }); else {
  18.     let username = await User.findOne({ usuario: usuario });
  19.     let currentUser = await User.findOne({ _id: req.params.id });
  20.     let telefonoUser = await User.findOne({ telefono: telefono });
  21.     let emailUser = await User.findOne({ email: email });
  22.     let nombreUser = await User.findOne({ nombre: nombre });
  23.     if (username && currentUser.usuario !== usuario) {
  24.       req.flash('error_msg', 'Este usuario ya esta registrado');
  25.       return res.redirect('/perfil/perfil');
  26.     }
  27.     if (nombreUser && currentUser.nombre !== nombre) {
  28.       req.flash('error_msg', 'Este nombre ya esta registrado');
  29.       return res.redirect('/perfil/perfil');
  30.     }
  31.     if (emailUser && currentUser.email !== email) {
  32.       req.flash('error_msg', 'Este correo ya esta registrado');
  33.       return res.redirect('/perfil/perfil');
  34.     }
  35.     if (telefonoUser && currentUser.telefono !== telefono) {
  36.       req.flash('error_msg', 'Este celular ya esta registrado');
  37.       return res.redirect('/perfil/perfil');
  38.     }
  39.     let editUser = new User({ usuario, nombre, bloque, email, conjunto, telefono, password });
  40.     editUser.password = await editUser.encryptPassword(password);
  41.     let contra = editUser.password;
  42.     console.log('editU', editUser.password);
  43.     await User.findByIdAndUpdate(req.params.id, { usuario, nombre, bloque, email, conjunto, telefono, contra });
  44.     req.flash('success_msg', 'Perfil actualizado exitosamente');
  45.     res.redirect('/perfil/perfil');
  46.   }
  47. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement