diff --git a/infra/controller.js b/infra/controller.js index bee5225..c1b7a39 100644 --- a/infra/controller.js +++ b/infra/controller.js @@ -14,11 +14,12 @@ function onNoMatchHandler(request, response) { } function onErrorHandler(error, request, response) { - if ( - error instanceof ValidationError || - error instanceof NotFoundError || - error instanceof UnauthorizedError - ) { + if (error instanceof ValidationError || error instanceof NotFoundError) { + return response.status(error.statusCode).json(error); + } + + if (error instanceof UnauthorizedError) { + clearSessionCookie(response); return response.status(error.statusCode).json(error); } diff --git a/models/session.js b/models/session.js index 51e0144..5e3e4e9 100644 --- a/models/session.js +++ b/models/session.js @@ -62,7 +62,7 @@ async function create(userId) { async function renew(sessionId) { const expiresAt = new Date(Date.now() + EXPIRATION_IN_MILLISECONDS); - const renewedSessionObject = runUpdateQuery(sessionId, expiresAt); + const renewedSessionObject = await runUpdateQuery(sessionId, expiresAt); return renewedSessionObject; async function runUpdateQuery(sessionId, expiresAt) { diff --git a/tests/integration/api/v1/user/get.test.js b/tests/integration/api/v1/user/get.test.js index 2cceaf9..8302f83 100644 --- a/tests/integration/api/v1/user/get.test.js +++ b/tests/integration/api/v1/user/get.test.js @@ -116,6 +116,19 @@ describe("GET api/v1/user", () => { action: "Verifique se este usuário está logado e tente novamente.", status_code: 401, }); + + // Set-Cookie assertions + const parsedSetCookie = setCookieParser(response, { + map: true, + }); + + expect(parsedSetCookie.session_id).toEqual({ + name: "session_id", + value: "invalid", + maxAge: -1, + path: "/", + httpOnly: true, + }); }); }); });