Skip to content

Commit ce300e7

Browse files
committed
AM - Perfil profesional - Recuperar contraseña
1 parent a1b10c4 commit ce300e7

2 files changed

Lines changed: 37 additions & 30 deletions

File tree

auth/auth.controller.ts

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -231,38 +231,42 @@ export async function sendOtpAndNotify(username): Promise<any> {
231231
const usuario = await AuthUsers.findOne({ usuario: username });
232232

233233
// Se mantiene la validación para usuarios temporales con email
234-
if (usuario && usuario.tipo === 'temporal' && usuario.email) {
235-
// Genera un código OTP de 6 dígitos
236-
const otpCode = crypto.randomInt(100000, 999999).toString();
237-
238-
usuario.otp = {
239-
code: otpCode,
240-
expiresAt: new Date(Date.now() + 60 * 60 * 1000), // 10 minutos en milisegundos
241-
};
242-
243-
usuario.audit(userScheduler);
244-
await usuario.save();
245-
246-
const extras: any = {
247-
titulo: 'Código de Verificación',
248-
usuario,
249-
otpCode,
250-
};
251-
const htmlToSend = await renderHTML('emails/otp-code.html', extras);
252-
253-
const options: MailOptions = {
254-
from: enviarMail.auth.user,
255-
to: usuario.email.toString(),
256-
subject: 'Tu código de verificación',
257-
text: '',
258-
html: htmlToSend,
259-
attachments: null,
260-
};
261-
await sendMail(options);
234+
if (usuario) {
235+
if (usuario.tipo === 'temporal' && usuario.email) {
236+
// Genera un código OTP de 6 dígitos
237+
const otpCode = crypto.randomInt(100000, 999999).toString();
238+
239+
usuario.otp = {
240+
code: otpCode,
241+
expiresAt: new Date(Date.now() + 60 * 60 * 1000), // 10 minutos en milisegundos
242+
};
243+
244+
usuario.audit(userScheduler);
245+
await usuario.save();
246+
247+
const extras: any = {
248+
titulo: 'Código de Verificación',
249+
usuario,
250+
otpCode,
251+
};
252+
const htmlToSend = await renderHTML('emails/otp-code.html', extras);
253+
254+
const options: MailOptions = {
255+
from: enviarMail.auth.user,
256+
to: usuario.email.toString(),
257+
subject: 'Tu código de verificación',
258+
text: '',
259+
html: htmlToSend,
260+
attachments: null,
261+
};
262+
await sendMail(options);
263+
} else {
264+
return null;
265+
}
262266
return usuario;
263267
} else {
264268
// El usuario no existe o no es un usuario temporal con email
265-
return null;
269+
throw { tipo: 'cuentaInexistenteAndes' };
266270
}
267271
} catch (error) {
268272
throw error;

auth/routes/routes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,10 @@ router.post('/sendOTPCode', async (req, res, next) => {
276276
} else {
277277
return next(403);
278278
}
279-
} catch (error) {
279+
} catch (error: any) {
280+
if (error.tipo === 'cuentaInexistenteAndes') {
281+
return res.json({ status: 'cuentaInexistenteAndes' });
282+
}
280283
return next(error);
281284
}
282285
});

0 commit comments

Comments
 (0)