05f8e2e3b6
- Create reusable components: auth, api-client, shared-types - ModeTemplate with Hono backend and SvelteKit frontend - Auth store refactored to class-based Svelte 5 pattern - SSR-safe hydrate() pattern for localStorage access - Frontend builds successfully with Svelte 5 runes
73 lines
1.7 KiB
TypeScript
73 lines
1.7 KiB
TypeScript
/**
|
|
* Backend Server
|
|
*
|
|
* Hono API server with auth routes pre-configured.
|
|
*/
|
|
|
|
import { Hono } from 'hono';
|
|
import { cors } from 'hono/cors';
|
|
import { getAuthUrl, handleCallback, getConfigFromEnv } from './modules/auth';
|
|
|
|
const app = new Hono();
|
|
|
|
// CORS for frontend
|
|
app.use('/*', cors({
|
|
origin: ['http://localhost:5173', 'http://localhost:3005'],
|
|
credentials: true,
|
|
}));
|
|
|
|
// Health check
|
|
app.get('/health', (c) => c.json({ status: 'ok' }));
|
|
|
|
// ============================================
|
|
// AUTH ROUTES
|
|
// ============================================
|
|
|
|
const authConfig = getConfigFromEnv();
|
|
|
|
app.get('/auth/login', (c) => {
|
|
return c.redirect(getAuthUrl(authConfig));
|
|
});
|
|
|
|
app.get('/auth/callback', async (c) => {
|
|
const code = c.req.query('code');
|
|
|
|
if (!code) {
|
|
return c.json({ error: 'No authorization code' }, 400);
|
|
}
|
|
|
|
try {
|
|
const result = await handleCallback(code, authConfig);
|
|
|
|
// Return token and user info
|
|
// Frontend should store token and redirect
|
|
return c.json(result);
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : 'Auth failed';
|
|
return c.json({ error: message }, 403);
|
|
}
|
|
});
|
|
|
|
// ============================================
|
|
// API ROUTES
|
|
// ============================================
|
|
|
|
app.get('/api/me', async (c) => {
|
|
// TODO: Validate token from Authorization header
|
|
// Return current user info
|
|
return c.json({ message: 'Implement me' });
|
|
});
|
|
|
|
// ============================================
|
|
// START SERVER
|
|
// ============================================
|
|
|
|
const port = Number(process.env.PORT) || 3005;
|
|
|
|
console.log(`[Server] Starting on port ${port}...`);
|
|
|
|
export default {
|
|
port,
|
|
fetch: app.fetch,
|
|
};
|