Skip to content

Commit 8eb85c9

Browse files
Reduce log spam: Don't request user data if not authenticated
1 parent 78b4b2c commit 8eb85c9

File tree

6 files changed

+58
-9
lines changed

6 files changed

+58
-9
lines changed

frontend/src/hooks/use-user.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useIsAuthenticatedQuery } from '@app/redux-api/auth/api';
12
import { useGetUserQuery } from '@app/redux-api/user/api';
23
import { login } from '@app/user/login';
34
import { useEffect } from 'react';
@@ -15,20 +16,16 @@ interface LoadedAuth {
1516
type AuthResult = LoadingAuth | LoadedAuth;
1617

1718
export const useIsAuthenticated = (): AuthResult => {
18-
const { data, isSuccess, isError, isLoading } = useGetUserQuery();
19+
const { data, isSuccess, isError } = useIsAuthenticatedQuery();
1920

2021
if (isSuccess) {
21-
return { isAuthenticated: data !== undefined, isLoadingAuth: false };
22+
return { isAuthenticated: data?.session.active ?? false, isLoadingAuth: false };
2223
}
2324

2425
if (isError) {
2526
return { isAuthenticated: false, isLoadingAuth: false };
2627
}
2728

28-
if (isLoading) {
29-
return { isAuthenticated: undefined, isLoadingAuth: true };
30-
}
31-
3229
return { isAuthenticated: undefined, isLoadingAuth: true };
3330
};
3431

frontend/src/redux-api/auth/api.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { AUTH_BASE_QUERY } from '@app/redux-api/common';
2+
import { createApi } from '@reduxjs/toolkit/query/react';
3+
4+
export const authApi = createApi({
5+
reducerPath: 'authApi',
6+
baseQuery: AUTH_BASE_QUERY,
7+
tagTypes: ['isAuthenticated'],
8+
endpoints: (builder) => ({
9+
isAuthenticated: builder.query<Session | undefined, void>({
10+
query: () => ({
11+
url: '/oauth2/session',
12+
validateStatus: (response) => response.status === 200 || response.status === 401,
13+
}),
14+
providesTags: ['isAuthenticated'],
15+
}),
16+
}),
17+
});
18+
19+
export const { useIsAuthenticatedQuery } = authApi;
20+
21+
type Session = {
22+
session: {
23+
created_at: string;
24+
ends_at: string;
25+
timeout_at: string;
26+
ends_in_seconds: number;
27+
active: boolean;
28+
timeout_in_seconds: number;
29+
};
30+
tokens: {
31+
expire_at: string;
32+
refreshed_at: string;
33+
expire_in_seconds: number;
34+
next_auto_refresh_in_seconds: number;
35+
refresh_cooldown: boolean;
36+
refresh_cooldown_seconds: number;
37+
};
38+
};

frontend/src/redux-api/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const staggeredBaseQuery = (baseUrl: string) => {
7777

7878
export const API_PATH = '/api/klage-dittnav-api/api';
7979
export const API_BASE_QUERY = staggeredBaseQuery(API_PATH);
80+
export const AUTH_BASE_QUERY = staggeredBaseQuery('');
8081

8182
export const KODEVERK_API_PATH = '/api/klage-kodeverk-api/kodeverk';
8283
export const KODEVERK_API_BASE_QUERY = staggeredBaseQuery(KODEVERK_API_PATH);

frontend/src/redux-api/server-sent-events.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { AppEventEnum } from '@app/logging/action';
22
import { apiEvent, appEvent } from '@app/logging/logger';
3+
import { reduxStore } from '@app/redux/configure-store';
4+
import { authApi } from '@app/redux-api/auth/api';
35
import { userApi } from '@app/redux-api/user/api';
46

57
export enum ServerSentEventType {
@@ -96,7 +98,8 @@ export class ServerSentEventManager {
9698

9799
if (!preflightOK) {
98100
// Probably the session timed out. Double check the logged in status.
99-
userApi.util.invalidateTags(['user']);
101+
reduxStore.dispatch(userApi.util.invalidateTags(['user']));
102+
reduxStore.dispatch(authApi.util.invalidateTags(['isAuthenticated']));
100103

101104
return;
102105
}

frontend/src/redux/configure-store.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type RootState, rootReducer } from '@app/redux/root';
2+
import { authApi } from '@app/redux-api/auth/api';
23
import { caseApi } from '@app/redux-api/case/api';
34
import { innsendingsytelserApi } from '@app/redux-api/innsendingsytelser';
45
import { userApi } from '@app/redux-api/user/api';
@@ -30,7 +31,8 @@ const rtkQueryErrorLogger: Middleware = () => (next) => (action) => {
3031
console.error('rtkQueryError', action);
3132

3233
if (action.payload.status === 401) {
33-
userApi.util.invalidateTags(['user']);
34+
reduxStore.dispatch(userApi.util.invalidateTags(['user']));
35+
reduxStore.dispatch(authApi.util.invalidateTags(['isAuthenticated']));
3436
}
3537
}
3638

@@ -52,7 +54,13 @@ export const reduxStore = configureStore({
5254
'meta.arg.originalArgs.file',
5355
],
5456
},
55-
}).concat([innsendingsytelserApi.middleware, userApi.middleware, caseApi.middleware, rtkQueryErrorLogger]),
57+
}).concat([
58+
innsendingsytelserApi.middleware,
59+
authApi.middleware,
60+
userApi.middleware,
61+
caseApi.middleware,
62+
rtkQueryErrorLogger,
63+
]),
5664
});
5765

5866
export type AppDispatch = typeof reduxStore.dispatch;

frontend/src/redux/root.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { loggedOutModalSlice } from '@app/redux/logged-out-modal';
22
import { sessionSlice } from '@app/redux/session/session';
3+
import { authApi } from '@app/redux-api/auth/api';
34
import { caseApi } from '@app/redux-api/case/api';
45
import { innsendingsytelserApi } from '@app/redux-api/innsendingsytelser';
56
import { userApi } from '@app/redux-api/user/api';
67
import { combineReducers } from 'redux';
78

89
export const rootReducer = combineReducers({
910
[innsendingsytelserApi.reducerPath]: innsendingsytelserApi.reducer,
11+
[authApi.reducerPath]: authApi.reducer,
1012
[userApi.reducerPath]: userApi.reducer,
1113
[caseApi.reducerPath]: caseApi.reducer,
1214
session: sessionSlice.reducer,

0 commit comments

Comments
 (0)