- Campaigns: DM can archive/restore; owner can delete. Archived campaigns move off the main list and stop accepting invite codes. - Encounters: DM can archive/restore and delete, and arrange the campaign's own order; the list can also be sorted by name, status, type or date. Archived encounters are hidden from players' lists. - Account page with change password (POST /auth/password). - Seed is now additive (safe on a live database; --reset for the old wipe) and builds a fuller Aldenfell: earned weapons, item pool, spells, NPCs, and 28 battle/quest scenes with descriptions, DM notes and rewards. - Vite preview host config for the deployed site. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
109 lines
3.7 KiB
JavaScript
109 lines
3.7 KiB
JavaScript
const express = require('express');
|
|
const bcrypt = require('bcrypt');
|
|
const jwt = require('jsonwebtoken');
|
|
const { z } = require('zod');
|
|
const prisma = require('../db');
|
|
const { requireAuth } = require('../middleware/auth');
|
|
|
|
const router = express.Router();
|
|
|
|
const registerSchema = z.object({
|
|
email: z.string().email(),
|
|
password: z.string().min(8, 'Password must be at least 8 characters'),
|
|
displayName: z.string().min(1).max(60),
|
|
});
|
|
|
|
const loginSchema = z.object({
|
|
email: z.string().email(),
|
|
password: z.string().min(1),
|
|
});
|
|
|
|
const changePasswordSchema = z.object({
|
|
currentPassword: z.string().min(1),
|
|
newPassword: z.string().min(8, 'Password must be at least 8 characters'),
|
|
});
|
|
|
|
function issueToken(user) {
|
|
return jwt.sign(
|
|
{ sub: user.id, email: user.email, displayName: user.displayName },
|
|
process.env.JWT_SECRET,
|
|
{ expiresIn: process.env.JWT_EXPIRES_IN || '7d' }
|
|
);
|
|
}
|
|
|
|
function publicUser(user) {
|
|
return { id: user.id, email: user.email, displayName: user.displayName };
|
|
}
|
|
|
|
router.post('/register', async (req, res, next) => {
|
|
try {
|
|
const parsed = registerSchema.safeParse(req.body);
|
|
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
|
|
const { email, password, displayName } = parsed.data;
|
|
|
|
const existing = await prisma.user.findUnique({ where: { email } });
|
|
if (existing) return res.status(409).json({ error: 'An account with that email already exists' });
|
|
|
|
const passwordHash = await bcrypt.hash(password, 12);
|
|
const user = await prisma.user.create({ data: { email, passwordHash, displayName } });
|
|
|
|
return res.status(201).json({ token: issueToken(user), user: publicUser(user) });
|
|
} catch (err) {
|
|
return next(err);
|
|
}
|
|
});
|
|
|
|
router.post('/login', async (req, res, next) => {
|
|
try {
|
|
const parsed = loginSchema.safeParse(req.body);
|
|
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
|
|
const { email, password } = parsed.data;
|
|
|
|
const user = await prisma.user.findUnique({ where: { email } });
|
|
if (!user) return res.status(401).json({ error: 'Invalid email or password' });
|
|
|
|
const valid = await bcrypt.compare(password, user.passwordHash);
|
|
if (!valid) return res.status(401).json({ error: 'Invalid email or password' });
|
|
|
|
return res.json({ token: issueToken(user), user: publicUser(user) });
|
|
} catch (err) {
|
|
return next(err);
|
|
}
|
|
});
|
|
|
|
router.get('/me', requireAuth, async (req, res, next) => {
|
|
try {
|
|
const user = await prisma.user.findUnique({ where: { id: req.user.id } });
|
|
if (!user) return res.status(404).json({ error: 'User not found' });
|
|
return res.json({ user: publicUser(user) });
|
|
} catch (err) {
|
|
return next(err);
|
|
}
|
|
});
|
|
|
|
// Tokens already issued stay valid until they expire; this only changes what
|
|
// the next login needs.
|
|
router.post('/password', requireAuth, async (req, res, next) => {
|
|
try {
|
|
const parsed = changePasswordSchema.safeParse(req.body);
|
|
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
|
|
const { currentPassword, newPassword } = parsed.data;
|
|
|
|
const user = await prisma.user.findUnique({ where: { id: req.user.id } });
|
|
if (!user) return res.status(404).json({ error: 'User not found' });
|
|
|
|
const valid = await bcrypt.compare(currentPassword, user.passwordHash);
|
|
if (!valid) return res.status(400).json({ error: 'Your current password is incorrect' });
|
|
if (newPassword === currentPassword) {
|
|
return res.status(400).json({ error: 'Your new password must be different from the current one' });
|
|
}
|
|
|
|
await prisma.user.update({ where: { id: user.id }, data: { passwordHash: await bcrypt.hash(newPassword, 12) } });
|
|
return res.status(204).end();
|
|
} catch (err) {
|
|
return next(err);
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|