feat(ADM-018): completed feature

This commit is contained in:
chattie
2026-08-17 22:23:10 +02:00
parent cf1c69fc8b
commit d595b4871f
871 changed files with 47411 additions and 281 deletions

View File

@@ -4,6 +4,7 @@
*/
import type { FastifyInstance, FastifyReply } from 'fastify';
import { z } from 'zod';
import type { Authenticate } from '../../../shared/auth.js';
import { parseJson } from '../../../shared/http-input.js';
import { AppError } from '../../../shared/errors.js';
import type pg from 'pg';
@@ -35,6 +36,8 @@ export interface IdentityRoutesDeps {
/** Test seams; production uses defaults. */
hasher?: PasswordHasher;
rateLimiter?: LoginRateLimiter;
/** Session authenticator. Created by createSessionAuthenticator in build-app.ts. */
authenticate?: Authenticate;
}
const credentialsSchema = z.object({
@@ -104,6 +107,18 @@ export async function registerIdentityRoutes(
clearSessionCookie(reply, cookieSecure);
return reply.code(204).send();
});
app.get('/auth/me', async (request, reply) => {
try {
const user = await deps.authenticate!(request);
return reply.send({ id: user.id, email: user.email, role: user.role });
} catch (error) {
if (error instanceof AppError && error.statusCode === 401) {
return reply.send({ user: null });
}
throw error;
}
});
}
function setSessionCookie(reply: FastifyReply, token: string, secure: boolean): void {