Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- router.get('/perfil/edit/:id', isAuthenticated, async (req, res) => {
- res.render('perfil/edit-profile', { user: await User.findById(req.params.id) });
- });
- router.put('/perfil/edit-profile/:id', isAuthenticated, async (req, res) => {
- let { usuario, nombre, bloque, conjunto, email, telefono, password, confirm_password } = req.body;
- let errors = [];
- if (!usuario) errors.push({ text: 'Por favor ingresa tu nombre de usuario' });
- if (!nombre) errors.push({ text: 'Por favor ingresa tu nombre completo' });
- if (!bloque) errors.push({ text: 'Por favor ingresa tu bloque y casa' });
- if (!email) errors.push({ text: 'Por favor ingresa tu correo electronico' });
- if (!telefono) errors.push({ text: 'Por favor ingresa tu celular' });
- if (!password) errors.push({ text: 'Por favor ingresa tu contraseña' });
- if (!confirm_password) errors.push({ text: 'Por favor confirma tu contraseña' });
- if (password.length < 8) errors.push({ text: 'Tu contraseña debe ser mayor a 8 caracteres' });
- if (password != confirm_password) errors.push({ text: 'Las contraseñas no coinciden' });
- if (errors.length > 0) res.render('/perfil/edit-profile/:id', { errors, usuario, nombre, bloque, conjunto, email, telefono, password, confirm_password }); else {
- let username = await User.findOne({ usuario: usuario });
- let currentUser = await User.findOne({ _id: req.params.id });
- let telefonoUser = await User.findOne({ telefono: telefono });
- let emailUser = await User.findOne({ email: email });
- let nombreUser = await User.findOne({ nombre: nombre });
- if (username && currentUser.usuario !== usuario) {
- req.flash('error_msg', 'Este usuario ya esta registrado');
- return res.redirect('/perfil/perfil');
- }
- if (nombreUser && currentUser.nombre !== nombre) {
- req.flash('error_msg', 'Este nombre ya esta registrado');
- return res.redirect('/perfil/perfil');
- }
- if (emailUser && currentUser.email !== email) {
- req.flash('error_msg', 'Este correo ya esta registrado');
- return res.redirect('/perfil/perfil');
- }
- if (telefonoUser && currentUser.telefono !== telefono) {
- req.flash('error_msg', 'Este celular ya esta registrado');
- return res.redirect('/perfil/perfil');
- }
- let editUser = new User({ usuario, nombre, bloque, email, conjunto, telefono, password });
- editUser.password = await editUser.encryptPassword(password);
- let contra = editUser.password;
- console.log('editU', editUser.password);
- await User.findByIdAndUpdate(req.params.id, { usuario, nombre, bloque, email, conjunto, telefono, contra });
- req.flash('success_msg', 'Perfil actualizado exitosamente');
- res.redirect('/perfil/perfil');
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement