Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cskrov committed Aug 28, 2024
1 parent fd0d046 commit d7f4e28
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
8 changes: 4 additions & 4 deletions frontend/src/hooks/use-user.ts
Original file line number Diff line number Diff line change
@@ -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 };
};
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/logging/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
7 changes: 3 additions & 4 deletions frontend/src/redux-api/user/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,20 @@ export const oauthApi = createApi({
baseQuery: OAUTH_BASE_QUERY,
tagTypes: ['session'],
endpoints: (builder) => ({
getSession: builder.query<OAuthSessionData | undefined, void>({
getSession: builder.query<OAuthSessionData | null, void>({
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));
}
},
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/redux/configure-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

Expand All @@ -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;
Expand Down

0 comments on commit d7f4e28

Please sign in to comment.