Changeset 6d882b1 in Klonkt for src/routes/account.js


Ignore:
Timestamp:
06/20/2026 01:24:26 AM (3 months ago)
Author:
roboburr <roboburr@…>
Branches:
main
Children:
dfc5379
Parents:
11ba289
Message:

feat(account): email address editable

Profile form gets an email field; POST /account/profile validates format +
uniqueness (no other account with that address) and updates email + session.

Co-Authored-By: Claude <noreply@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/routes/account.js

    r11ba289 r6d882b1  
    108108router.post('/profile', requireAuth, (req, res) => {
    109109  const bio = (req.body.bio || '').toString().slice(0, 500).trim();
     110
     111  // E-mail (optioneel mee te wijzigen). Validatie: geldig formaat + niet al door
     112  // een ander account in gebruik. E-mail is het login-/reset-anker, dus uniek.
     113  const email = (req.body.email || '').toString().trim();
     114  if (email) {
     115    if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) || email.length > 254) {
     116      return res.redirect('/account?error=' + encodeURIComponent('Voer een geldig e-mailadres in.'));
     117    }
     118    const taken = db.prepare('SELECT 1 FROM users WHERE LOWER(email) = LOWER(?) AND id != ?')
     119      .get(email, req.session.user.id);
     120    if (taken) {
     121      return res.redirect('/account?error=' + encodeURIComponent('Dit e-mailadres is al in gebruik.'));
     122    }
     123    db.prepare('UPDATE users SET email = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?')
     124      .run(email, req.session.user.id);
     125    req.session.user.email = email; // sessie bijwerken zodat de UI klopt
     126  }
     127
    110128  db.prepare('UPDATE users SET bio = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?')
    111129    .run(bio || null, req.session.user.id);
    112   res.redirect('/account?success=' + encodeURIComponent('Profile updated'));
     130  res.redirect('/account?success=' + encodeURIComponent('Profiel bijgewerkt'));
    113131});
    114132
Note: See TracChangeset for help on using the changeset viewer.