Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,38 +231,42 @@ export async function sendOtpAndNotify(username): Promise<any> {
const usuario = await AuthUsers.findOne({ usuario: username });

// Se mantiene la validación para usuarios temporales con email
if (usuario && usuario.tipo === 'temporal' && usuario.email) {
// Genera un código OTP de 6 dígitos
const otpCode = crypto.randomInt(100000, 999999).toString();

usuario.otp = {
code: otpCode,
expiresAt: new Date(Date.now() + 60 * 60 * 1000), // 10 minutos en milisegundos
};

usuario.audit(userScheduler);
await usuario.save();

const extras: any = {
titulo: 'Código de Verificación',
usuario,
otpCode,
};
const htmlToSend = await renderHTML('emails/otp-code.html', extras);

const options: MailOptions = {
from: enviarMail.auth.user,
to: usuario.email.toString(),
subject: 'Tu código de verificación',
text: '',
html: htmlToSend,
attachments: null,
};
await sendMail(options);
if (usuario) {
if (usuario.tipo === 'temporal' && usuario.email) {
// Genera un código OTP de 6 dígitos
const otpCode = crypto.randomInt(100000, 999999).toString();

usuario.otp = {
code: otpCode,
expiresAt: new Date(Date.now() + 60 * 60 * 1000), // 10 minutos en milisegundos
};

usuario.audit(userScheduler);
await usuario.save();

const extras: any = {
titulo: 'Código de Verificación',
usuario,
otpCode,
};
const htmlToSend = await renderHTML('emails/otp-code.html', extras);

const options: MailOptions = {
from: enviarMail.auth.user,
to: usuario.email.toString(),
subject: 'Tu código de verificación',
text: '',
html: htmlToSend,
attachments: null,
};
await sendMail(options);
} else {
return null;
}
return usuario;
} else {
// El usuario no existe o no es un usuario temporal con email
return null;
throw { tipo: 'cuentaInexistenteAndes' };
}
} catch (error) {
throw error;
Expand Down
5 changes: 4 additions & 1 deletion auth/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ router.post('/sendOTPCode', async (req, res, next) => {
} else {
return next(403);
}
} catch (error) {
} catch (error: any) {
if (error.tipo === 'cuentaInexistenteAndes') {
return res.json({ status: 'cuentaInexistenteAndes' });
}
return next(error);
}
});
Expand Down
Loading