| 1 | /**
|
|---|
| 2 | * Account routes — profile, password, avatar.
|
|---|
| 3 | *
|
|---|
| 4 | * Sections:
|
|---|
| 5 | * GET / -> render full account page (profile + password + avatar + danger)
|
|---|
| 6 | * POST /profile -> update bio
|
|---|
| 7 | * POST /password -> change password (verify current, hash new)
|
|---|
| 8 | * POST /avatar -> upload avatar image (multer)
|
|---|
| 9 | * POST /avatar/remove -> clear avatar_url
|
|---|
| 10 | *
|
|---|
| 11 | * Each form has its own POST handler. After success, redirects back to
|
|---|
| 12 | * /account?success=... so the page picks it up via query string.
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | import express from 'express';
|
|---|
| 16 | import path from 'path';
|
|---|
| 17 | import fs from 'fs';
|
|---|
| 18 | import { fileURLToPath } from 'url';
|
|---|
| 19 | import bcrypt from 'bcryptjs';
|
|---|
| 20 | import multer from 'multer';
|
|---|
| 21 | import { v4 as uuid } from 'uuid';
|
|---|
| 22 | import db from '../config/database.js';
|
|---|
| 23 | import { renderPage } from '../middleware/render.js';
|
|---|
| 24 | import { requireAuth } from '../middleware/auth.js';
|
|---|
| 25 |
|
|---|
| 26 | const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|---|
| 27 | const AVATAR_DIR = path.resolve(
|
|---|
| 28 | process.env.AVATAR_PATH || path.join(__dirname, '..', '..', 'storage', 'media', 'avatars')
|
|---|
| 29 | );
|
|---|
| 30 | fs.mkdirSync(AVATAR_DIR, { recursive: true });
|
|---|
| 31 |
|
|---|
| 32 | const ALLOWED_AVATAR_EXT = new Set(['.jpg', '.jpeg', '.png', '.webp', '.gif']);
|
|---|
| 33 | const MAX_AVATAR_BYTES = 5 * 1024 * 1024;
|
|---|
| 34 |
|
|---|
| 35 | const avatarStorage = multer.diskStorage({
|
|---|
| 36 | destination: (req, file, cb) => cb(null, AVATAR_DIR),
|
|---|
| 37 | filename: (req, file, cb) => {
|
|---|
| 38 | const ext = path.extname(file.originalname).toLowerCase();
|
|---|
| 39 | cb(null, `${uuid()}${ext}`);
|
|---|
| 40 | },
|
|---|
| 41 | });
|
|---|
| 42 | const avatarUpload = multer({
|
|---|
| 43 | storage: avatarStorage,
|
|---|
| 44 | limits: { fileSize: MAX_AVATAR_BYTES },
|
|---|
| 45 | fileFilter: (req, file, cb) => {
|
|---|
| 46 | const ext = path.extname(file.originalname).toLowerCase();
|
|---|
| 47 | if (!ALLOWED_AVATAR_EXT.has(ext)) {
|
|---|
| 48 | return cb(new Error('Avatar must be jpg/png/webp/gif'));
|
|---|
| 49 | }
|
|---|
| 50 | cb(null, true);
|
|---|
| 51 | },
|
|---|
| 52 | });
|
|---|
| 53 |
|
|---|
| 54 | const router = express.Router();
|
|---|
| 55 |
|
|---|
| 56 | // ==================== GET account page ====================
|
|---|
| 57 | router.get('/', requireAuth, (req, res) => {
|
|---|
| 58 | const account = db.prepare(`
|
|---|
| 59 | SELECT id, username, email, role, bio, avatar_url, created_at, password_hash
|
|---|
| 60 | FROM users WHERE id = ?
|
|---|
| 61 | `).get(req.session.user.id);
|
|---|
| 62 | const hasPassword = !!(account && account.password_hash && account.password_hash !== '!google-oauth');
|
|---|
| 63 | if (account) delete account.password_hash; // niet naar de view lekken
|
|---|
| 64 |
|
|---|
| 65 | renderPage(req, res, 'pages/account', {
|
|---|
| 66 | pageTitle: 'Account',
|
|---|
| 67 | bodyClass: 'on-special',
|
|---|
| 68 | account,
|
|---|
| 69 | hasPassword,
|
|---|
| 70 | success: req.query.success || null,
|
|---|
| 71 | error: req.query.error || null,
|
|---|
| 72 | });
|
|---|
| 73 | });
|
|---|
| 74 |
|
|---|
| 75 | // ==================== UPDATE BIO ====================
|
|---|
| 76 | router.post('/profile', requireAuth, (req, res) => {
|
|---|
| 77 | const bio = (req.body.bio || '').toString().slice(0, 500).trim();
|
|---|
| 78 | db.prepare('UPDATE users SET bio = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?')
|
|---|
| 79 | .run(bio || null, req.session.user.id);
|
|---|
| 80 | res.redirect('/account?success=' + encodeURIComponent('Profile updated'));
|
|---|
| 81 | });
|
|---|
| 82 |
|
|---|
| 83 | // P57 — /preferences route removed. Per-user theme/palette was a multi-tenant
|
|---|
| 84 | // holdover that conflicts with the site-default model: visitors should see
|
|---|
| 85 | // the site's appearance, not whatever a user once picked. The users.theme and
|
|---|
| 86 | // users.palette columns stay in the schema (no migration needed) but are no
|
|---|
| 87 | // longer read or written.
|
|---|
| 88 |
|
|---|
| 89 | // ==================== CHANGE PASSWORD ====================
|
|---|
| 90 | router.post('/password', requireAuth, (req, res) => {
|
|---|
| 91 | const { current, new_password, confirm } = req.body;
|
|---|
| 92 | if (!current || !new_password || !confirm) {
|
|---|
| 93 | return res.redirect('/account?error=' + encodeURIComponent('Alle wachtwoordvelden zijn verplicht'));
|
|---|
| 94 | }
|
|---|
| 95 | if (new_password.length < 8) {
|
|---|
| 96 | return res.redirect('/account?error=' + encodeURIComponent('Nieuw wachtwoord moet minstens 8 tekens zijn'));
|
|---|
| 97 | }
|
|---|
| 98 | if (new_password !== confirm) {
|
|---|
| 99 | return res.redirect('/account?error=' + encodeURIComponent('Nieuwe wachtwoorden komen niet overeen'));
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | const row = db.prepare('SELECT password_hash FROM users WHERE id = ?').get(req.session.user.id);
|
|---|
| 103 | // Google-only accounts (luisteraars) hebben geen echt wachtwoord.
|
|---|
| 104 | if (!row || !row.password_hash || row.password_hash === '!google-oauth') {
|
|---|
| 105 | return res.redirect('/account?error=' + encodeURIComponent('Dit account heeft geen wachtwoord (Google-login)'));
|
|---|
| 106 | }
|
|---|
| 107 | if (!bcrypt.compareSync(current, row.password_hash)) {
|
|---|
| 108 | return res.redirect('/account?error=' + encodeURIComponent('Huidig wachtwoord is onjuist'));
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | const newHash = bcrypt.hashSync(new_password, 10);
|
|---|
| 112 | db.prepare('UPDATE users SET password_hash = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?')
|
|---|
| 113 | .run(newHash, req.session.user.id);
|
|---|
| 114 |
|
|---|
| 115 | res.redirect('/account?success=' + encodeURIComponent('Wachtwoord gewijzigd'));
|
|---|
| 116 | });
|
|---|
| 117 |
|
|---|
| 118 | // ==================== UPLOAD AVATAR ====================
|
|---|
| 119 | router.post('/avatar', requireAuth, (req, res) => {
|
|---|
| 120 | avatarUpload.single('avatar')(req, res, (err) => {
|
|---|
| 121 | if (err) {
|
|---|
| 122 | return res.redirect('/account?error=' + encodeURIComponent(err.message));
|
|---|
| 123 | }
|
|---|
| 124 | if (!req.file) {
|
|---|
| 125 | return res.redirect('/account?error=' + encodeURIComponent('No file uploaded'));
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | const url = `/media/avatars/${req.file.filename}`;
|
|---|
| 129 |
|
|---|
| 130 | // Remove the old avatar file (if it lives in our avatar dir)
|
|---|
| 131 | const old = db.prepare('SELECT avatar_url FROM users WHERE id = ?').get(req.session.user.id)?.avatar_url;
|
|---|
| 132 | if (old && old.startsWith('/media/avatars/')) {
|
|---|
| 133 | const oldPath = path.join(AVATAR_DIR, path.basename(old));
|
|---|
| 134 | try { fs.unlinkSync(oldPath); } catch {}
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | db.prepare('UPDATE users SET avatar_url = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?')
|
|---|
| 138 | .run(url, req.session.user.id);
|
|---|
| 139 | req.session.user.avatar_url = url;
|
|---|
| 140 |
|
|---|
| 141 | res.redirect('/account?success=' + encodeURIComponent('Avatar updated'));
|
|---|
| 142 | });
|
|---|
| 143 | });
|
|---|
| 144 |
|
|---|
| 145 | // ==================== REMOVE AVATAR ====================
|
|---|
| 146 | router.post('/avatar/remove', requireAuth, (req, res) => {
|
|---|
| 147 | const old = db.prepare('SELECT avatar_url FROM users WHERE id = ?').get(req.session.user.id)?.avatar_url;
|
|---|
| 148 | if (old && old.startsWith('/media/avatars/')) {
|
|---|
| 149 | const oldPath = path.join(AVATAR_DIR, path.basename(old));
|
|---|
| 150 | try { fs.unlinkSync(oldPath); } catch {}
|
|---|
| 151 | }
|
|---|
| 152 | db.prepare('UPDATE users SET avatar_url = NULL, updated_at = CURRENT_TIMESTAMP WHERE id = ?')
|
|---|
| 153 | .run(req.session.user.id);
|
|---|
| 154 | req.session.user.avatar_url = null;
|
|---|
| 155 | res.redirect('/account?success=' + encodeURIComponent('Avatar removed'));
|
|---|
| 156 | });
|
|---|
| 157 |
|
|---|
| 158 | export default router;
|
|---|