diff --git a/web-next/lib/change-email.ts b/web-next/lib/change-email.ts index a384942..c815623 100644 --- a/web-next/lib/change-email.ts +++ b/web-next/lib/change-email.ts @@ -5,7 +5,7 @@ import { authenticate } from './auth' import { bnetMakeRegistration, normalizeEmail, EMAIL_RE } from './bnet' import { checkSecurityToken } from './security-token' import { sendMail } from './mail' -import { confirmNewEmailHtml, confirmOldEmailHtml } from './emails' +import { confirmNewEmailHtml, confirmOldEmailHtml, oldEmailNotificationHtml } from './emails' import type { SessionData } from './session' @@ -71,7 +71,7 @@ export async function confirmOldEmail(hash: string): Promise { export async function confirmNewEmail(hash: string): Promise { const [rows] = await db(DB.default).query( - 'SELECT id, email, old_email, password FROM accountactivation WHERE hash = ? AND is_new_email_used = 0 AND old_email IS NOT NULL', + 'SELECT id, email, old_email, password, username FROM accountactivation WHERE hash = ? AND is_new_email_used = 0 AND old_email IS NOT NULL', [hash], ) const act = rows[0] @@ -100,6 +100,19 @@ export async function confirmNewEmail(hash: string): Promise { } catch { return { success: false, error: 'genericError' } } + + // Avisar al correo ANTIGUO de que ya no manda en la cuenta. Va después de que el + // cambio haya cuajado (si falla arriba, no hay nada que avisar) y sin enlaces: + // solo informa. Si alguien secuestra una sesión y cambia el correo, esta es la + // única forma que tiene el dueño de enterarse, porque al entrar se usa el correo. + // `sendMail` nunca lanza (devuelve false), así que no puede tumbar un cambio ya + // hecho: como mucho el aviso no sale. + await sendMail( + oldNorm, + `Cambio de correo realizado - NightSpire`, + oldEmailNotificationHtml(oldNorm, act.username ?? null, newNorm), + ) + await db(DB.default).query('DELETE FROM accountactivation WHERE id = ?', [act.id]) return { success: true } }