From d7f4e28456092d01e75db0f340132618d443d517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Skr=C3=B8vseth?= Date: Wed, 28 Aug 2024 18:30:08 +0200 Subject: [PATCH] Fixes --- frontend/src/hooks/use-user.ts | 8 ++++---- frontend/src/logging/logger.ts | 5 +++-- frontend/src/redux-api/user/api.ts | 7 +++---- frontend/src/redux/configure-store.ts | 11 +++++++++-- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/frontend/src/hooks/use-user.ts b/frontend/src/hooks/use-user.ts index 72697aab..39a05234 100644 --- a/frontend/src/hooks/use-user.ts +++ b/frontend/src/hooks/use-user.ts @@ -1,8 +1,8 @@ -import { skipToken } from '@reduxjs/toolkit/query'; -import { useGetUserQuery, useGetSessionQuery } from '@app/redux-api/user/api'; +import { SkipToken, skipToken } from '@reduxjs/toolkit/query'; +import { useGetSessionQuery, useGetUserQuery } from '@app/redux-api/user/api'; -export const useIsAuthenticated = () => { - const { data, ...rest } = useGetSessionQuery(undefined, { refetchOnFocus: true }); +export const useIsAuthenticated = (skip?: SkipToken) => { + const { data, ...rest } = useGetSessionQuery(skip, { refetchOnFocus: true }); return { ...rest, data: data?.session.active }; }; diff --git a/frontend/src/logging/logger.ts b/frontend/src/logging/logger.ts index 4c1df0a6..89cad1ea 100644 --- a/frontend/src/logging/logger.ts +++ b/frontend/src/logging/logger.ts @@ -74,7 +74,8 @@ class FrontendLogger { public setSessionEndsAt = (sessionEndsAt: string) => { this.sessionEndsAt = sessionEndsAt; - } + }; } -export const { apiEvent, appEvent, errorEvent, navigationEvent, sessionEvent, setTokenExpires, setSessionEndsAt } = new FrontendLogger(); +export const { apiEvent, appEvent, errorEvent, navigationEvent, sessionEvent, setTokenExpires, setSessionEndsAt } = + new FrontendLogger(); diff --git a/frontend/src/redux-api/user/api.ts b/frontend/src/redux-api/user/api.ts index 43d8a92d..a06f1b2c 100644 --- a/frontend/src/redux-api/user/api.ts +++ b/frontend/src/redux-api/user/api.ts @@ -42,21 +42,20 @@ export const oauthApi = createApi({ baseQuery: OAUTH_BASE_QUERY, tagTypes: ['session'], endpoints: (builder) => ({ - getSession: builder.query({ + getSession: builder.query({ query: () => ({ url: '/session', validateStatus: ({ status, ok }) => ok || status === 401 }), providesTags: ['session'], onQueryStarted: async (_, { dispatch, queryFulfilled }) => { const { data } = await queryFulfilled; - const hasData = data !== undefined; - + const hasData = data !== null; if (hasData) { setTokenExpires(data.tokens.expire_at); setSessionEndsAt(data.session.ends_at); } - if (!hasData || data.session.active !== true) { + if (!hasData || !data.session.active) { dispatch(userApi.util.updateQueryData('getUser', undefined, () => undefined)); } }, diff --git a/frontend/src/redux/configure-store.ts b/frontend/src/redux/configure-store.ts index e5b349d2..e2b1319b 100644 --- a/frontend/src/redux/configure-store.ts +++ b/frontend/src/redux/configure-store.ts @@ -30,7 +30,8 @@ const rtkQueryErrorLogger: Middleware = () => (next) => (action) => { console.error('rtkQueryError', action); if (action.payload.status === 401) { - userApi.util.invalidateTags(['isAuthenticated']); + userApi.util.invalidateTags(['user']); + oauthApi.util.invalidateTags(['session']); } } @@ -52,7 +53,13 @@ export const reduxStore = configureStore({ 'meta.arg.originalArgs.file', ], }, - }).concat([innsendingsytelserApi.middleware, userApi.middleware, caseApi.middleware, oauthApi.middleware, rtkQueryErrorLogger]), + }).concat([ + innsendingsytelserApi.middleware, + userApi.middleware, + caseApi.middleware, + oauthApi.middleware, + rtkQueryErrorLogger, + ]), }); export type AppDispatch = typeof reduxStore.dispatch;