Files
AIHostingTycoon/apps/server/src/routes/saves.ts
T
josh 2a6629af79 Revitalize backend: working cloud saves, logout, and account UX
Cloud saves were fully built but never wired up — useCloudSave() hook was
never called, no load-from-cloud flow existed, and there was no way to
continue a saved game. Logout was completely missing (no endpoint, no UI).
Accounts felt like a gate behind the invite wall rather than real accounts.

Backend: add tokenVersion to users for server-side token invalidation,
POST /auth/logout bumps it to revoke all JWTs, GET /auth/me returns
profile, GET /saves/latest returns most recent save with full gameData.
All createToken calls now include tokenVersion. Auth middleware rejects
tokens with stale tokenVersion.

Frontend: wire up useCloudSave() in App (auto-saves every 60 ticks with
error handling), fetch cloud save on startup for registered users, show
"Continue Your Game" card on NewGameScreen, add Log Out button with
confirmation in Settings, show username in sidebar, 401 interceptor
clears auth and reloads.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-28 19:32:03 -04:00

121 lines
2.7 KiB
TypeScript

import { Hono } from 'hono';
import { eq, and, desc } from 'drizzle-orm';
import { db } from '../db';
import { saves } from '../db/schema';
import { authMiddleware } from '../middleware/auth';
import type { AppEnv } from '../types';
const savesRouter = new Hono<AppEnv>();
savesRouter.use('*', authMiddleware);
savesRouter.get('/', async (c) => {
const userId = c.get('userId') as string;
const userSaves = await db
.select({
id: saves.id,
companyName: saves.companyName,
era: saves.era,
tickCount: saves.tickCount,
updatedAt: saves.updatedAt,
})
.from(saves)
.where(eq(saves.userId, userId))
.orderBy(desc(saves.updatedAt))
.limit(10);
return c.json({ saves: userSaves });
});
savesRouter.get('/latest', async (c) => {
const userId = c.get('userId') as string;
const [save] = await db
.select()
.from(saves)
.where(eq(saves.userId, userId))
.orderBy(desc(saves.updatedAt))
.limit(1);
return c.json({ save: save ?? null });
});
savesRouter.get('/:id', async (c) => {
const userId = c.get('userId') as string;
const saveId = c.req.param('id');
const [save] = await db
.select()
.from(saves)
.where(and(eq(saves.id, saveId), eq(saves.userId, userId)))
.limit(1);
if (!save) {
return c.json({ error: 'Save not found' }, 404);
}
return c.json({ save });
});
savesRouter.put('/', async (c) => {
const userId = c.get('userId') as string;
const body = await c.req.json<{
companyName: string;
saveVersion: number;
gameData: unknown;
tickCount: number;
era: string;
}>();
const existing = await db
.select({ id: saves.id })
.from(saves)
.where(eq(saves.userId, userId))
.orderBy(desc(saves.updatedAt))
.limit(1);
if (existing.length > 0) {
await db
.update(saves)
.set({
companyName: body.companyName,
saveVersion: body.saveVersion,
gameData: body.gameData,
tickCount: body.tickCount,
era: body.era,
updatedAt: new Date(),
})
.where(eq(saves.id, existing[0].id));
return c.json({ id: existing[0].id, updated: true });
}
const [newSave] = await db
.insert(saves)
.values({
userId,
companyName: body.companyName,
saveVersion: body.saveVersion,
gameData: body.gameData,
tickCount: body.tickCount,
era: body.era,
})
.returning({ id: saves.id });
return c.json({ id: newSave.id, created: true });
});
savesRouter.delete('/:id', async (c) => {
const userId = c.get('userId') as string;
const saveId = c.req.param('id');
await db
.delete(saves)
.where(and(eq(saves.id, saveId), eq(saves.userId, userId)));
return c.json({ deleted: true });
});
export { savesRouter };