feat(F-151): completed feature
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { FastifyInstance, FastifyReply } from 'fastify';
|
||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||
import type { FastifySchema } from 'fastify';
|
||||
import type pg from 'pg';
|
||||
import { z } from 'zod';
|
||||
@@ -123,7 +123,7 @@ export async function registerBackofficeRoutes(
|
||||
const input = parseResult.data;
|
||||
try {
|
||||
const result = await login.execute(input);
|
||||
setCookie(reply, result.token, true);
|
||||
setCookie(reply, result.token, isSecureRequest(request));
|
||||
return reply
|
||||
.code(200)
|
||||
.send({ id: result.user.id, email: result.user.email, role: result.user.role });
|
||||
@@ -142,7 +142,7 @@ export async function registerBackofficeRoutes(
|
||||
app.post('/backoffice/auth/logout', { schema: logoutSchema }, async (request, reply) => {
|
||||
const token = request.cookies[BACKOFFICE_SESSION_COOKIE_NAME];
|
||||
await logout.execute(token);
|
||||
clearCookie(reply, true);
|
||||
clearCookie(reply, isSecureRequest(request));
|
||||
return reply.code(204).send();
|
||||
});
|
||||
|
||||
@@ -159,6 +159,12 @@ export async function registerBackofficeRoutes(
|
||||
});
|
||||
}
|
||||
|
||||
function isSecureRequest(request: FastifyRequest): boolean {
|
||||
const forwardedProto = request.headers['x-forwarded-proto'];
|
||||
const proto = Array.isArray(forwardedProto) ? forwardedProto[0] : forwardedProto;
|
||||
return request.protocol === 'https' || proto === 'https';
|
||||
}
|
||||
|
||||
function setCookie(reply: FastifyReply, token: string, secure: boolean): void {
|
||||
void reply.setCookie(BACKOFFICE_SESSION_COOKIE_NAME, token, {
|
||||
path: '/',
|
||||
|
||||
Reference in New Issue
Block a user