feat(ADM-018): completed feature
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user