From 5c3a57c43a89cb6ce7673e46f41a180e5c3027a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matilda=20Landstr=C3=B6m?= Date: Tue, 14 May 2024 08:51:31 +0200 Subject: [PATCH] chore: add invalidateSessions endpoint and query --- lib/api/endpoints.ts | 1 + server/routers/user/query.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/api/endpoints.ts b/lib/api/endpoints.ts index 2efc39cd8..b4f8f4761 100644 --- a/lib/api/endpoints.ts +++ b/lib/api/endpoints.ts @@ -9,6 +9,7 @@ export namespace endpoints { profile = "profile/v1/Profile", upcomingStays = "booking/v1/Stays/future", previousStays = "booking/v1/Stays/past", + invalidateSessions = "profile/v1/Profile/invalidateSessions", } } diff --git a/server/routers/user/query.ts b/server/routers/user/query.ts index 3f8efdedc..124ca56b5 100644 --- a/server/routers/user/query.ts +++ b/server/routers/user/query.ts @@ -21,6 +21,36 @@ function fakingRequest(payload: T): Promise { } export const userQueryRouter = router({ + invalidateSessions: protectedProcedure.query(async function ({ ctx }) { + try { + const apiResponse = await api.post(api.endpoints.v1.invalidateSessions, { + cache: "no-store", + headers: { + Authorization: `Bearer ${ctx.session.token.access_token}`, + }, + body: {}, + }) + + if (!apiResponse.ok) { + switch (apiResponse.status) { + case 400: + throw badRequestError(apiResponse) + case 401: + throw unauthorizedError(apiResponse) + case 403: + throw forbiddenError(apiResponse) + default: + throw internalServerError(apiResponse) + } + } + return true + } catch (error) { + console.info(`Invalidate Sessions Error`) + console.error(error) + throw internalServerError() + } + }), + get: protectedProcedure.query(async function ({ ctx }) { const apiResponse = await api.get(api.endpoints.v1.profile, { cache: "no-store",