-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathuseAuthState.ts
More file actions
209 lines (193 loc) · 6.39 KB
/
useAuthState.ts
File metadata and controls
209 lines (193 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { useEffect, useMemo } from 'react';
import {
QueryObserverResult,
useQuery,
UseQueryOptions,
} from '@tanstack/react-query';
import useAuthProvider, { defaultAuthParams } from './useAuthProvider';
import useLogout from './useLogout';
import { removeDoubleSlashes, useBasename } from '../routing';
import { useNotify } from '../notification';
import { useEvent } from '../util';
const emptyParams = {};
/**
* Hook for getting the authentication status
*
* Calls the authProvider.checkAuth() method asynchronously.
*
* The return value updates according to the authProvider request state:
*
* - isPending: true just after mount, while the authProvider is being called. false once the authProvider has answered.
* - authenticated: true while loading. then true or false depending on the authProvider response.
*
* To avoid rendering a component and force waiting for the authProvider response, use the useAuthState() hook
* instead of the useAuthenticated() hook.
*
* You can render different content depending on the authenticated status.
*
* @see useAuthenticated()
*
* @param {Object} params Any params you want to pass to the authProvider
*
* @param {Boolean} logoutOnFailure: Optional. Whether the user should be logged out if the authProvider fails to authenticate them. False by default.
*
* @returns The current auth check state. Destructure as { authenticated, error, isPending }.
*
* @example
* import { useAuthState, Loading } from 'react-admin';
*
* const MyPage = () => {
* const { isPending, authenticated } = useAuthState();
* if (isPending) {
* return <Loading />;
* }
* if (authenticated) {
* return <AuthenticatedContent />;
* }
* return <AnonymousContent />;
* };
*/
const useAuthState = <ErrorType = Error>(
params: any = emptyParams,
logoutOnFailure: boolean = false,
queryOptions: UseAuthStateOptions<ErrorType> = emptyParams
): UseAuthStateResult<ErrorType> => {
const authProvider = useAuthProvider();
const logout = useLogout();
const basename = useBasename();
const notify = useNotify();
const { onSuccess, onError, onSettled, ...options } = queryOptions;
const queryResult = useQuery<boolean, any>({
queryKey: ['auth', 'checkAuth', params],
queryFn: ({ signal }) => {
// The authProvider is optional in react-admin
if (!authProvider) {
return true;
}
return authProvider
.checkAuth({ ...params, signal })
.then(() => true)
.catch(error => {
// This is necessary because react-query requires the error to be defined
if (error != null) {
throw error;
}
throw new Error();
});
},
retry: false,
...options,
});
const onSuccessEvent = useEvent(onSuccess ?? noop);
const onSettledEvent = useEvent(onSettled ?? noop);
const onErrorEvent = useEvent(
onError ??
((error: any) => {
if (!logoutOnFailure) return;
const loginUrl = removeDoubleSlashes(
`${basename}/${defaultAuthParams.loginUrl}`
);
logout(
{},
error && error.redirectTo != null
? error.redirectTo
: loginUrl
);
const shouldSkipNotify = error && error.message === false;
!shouldSkipNotify &&
notify(getErrorMessage(error, 'ra.auth.auth_check_error'), {
type: 'error',
});
})
);
useEffect(() => {
if (queryResult.data === undefined || queryResult.isFetching) return;
if (queryOptions.enabled === false) return;
onSuccessEvent(queryResult.data);
}, [
onSuccessEvent,
queryResult.data,
queryResult.isFetching,
queryOptions.enabled,
]);
useEffect(() => {
if (queryResult.error == null || queryResult.isFetching) return;
if (queryOptions.enabled === false) return;
onErrorEvent(queryResult.error);
}, [
onErrorEvent,
queryResult.error,
queryResult.isFetching,
queryOptions.enabled,
]);
useEffect(() => {
if (queryResult.status === 'pending' || queryResult.isFetching) return;
if (queryOptions.enabled === false) return;
onSettledEvent(queryResult.data, queryResult.error);
}, [
onSettledEvent,
queryResult.data,
queryResult.error,
queryResult.status,
queryResult.isFetching,
queryOptions.enabled,
]);
const result = useMemo(() => {
return {
...queryResult,
authenticated: queryResult.error ? false : queryResult.data,
};
}, [queryResult]);
return authProvider != null
? result
: (noAuthProviderQueryResult as unknown as UseAuthStateResult<ErrorType>);
};
type UseAuthStateOptions<ErrorType = Error> = Omit<
UseQueryOptions<boolean, ErrorType>,
'queryKey' | 'queryFn'
> & {
onSuccess?: (data: boolean) => void;
onError?: (err: ErrorType) => void;
onSettled?: (data?: boolean, error?: Error) => void;
};
export type UseAuthStateResult<ErrorType = Error> = QueryObserverResult<
boolean,
ErrorType
> & {
authenticated?: QueryObserverResult<boolean, ErrorType>['data'];
};
export default useAuthState;
const getErrorMessage = (error, defaultMessage) =>
typeof error === 'string'
? error
: typeof error === 'undefined' || !error.message
? defaultMessage
: error.message;
const noop = () => {};
const noAuthProviderQueryResult = {
authenticated: true,
data: true,
dataUpdatedAt: 0,
error: null,
errorUpdatedAt: 0,
errorUpdateCount: 0,
failureCount: 0,
failureReason: null,
fetchStatus: 'idle',
isError: false,
isInitialLoading: false,
isLoading: false,
isLoadingError: false,
isFetched: true,
isFetchedAfterMount: true,
isFetching: false,
isPaused: false,
isPlaceholderData: false,
isPending: false,
isRefetchError: false,
isRefetching: false,
isStale: false,
isSuccess: true,
status: 'success',
refetch: () => Promise.resolve(noAuthProviderQueryResult),
};