1
- import { useGetSessionQuery , useGetUserQuery } from '@app/redux-api/user/api' ;
2
- import type { IUser } from '@app/redux-api/user/types' ;
1
+ import { useGetUserQuery } from '@app/redux-api/user/api' ;
3
2
import { login } from '@app/user/login' ;
4
- import { type SkipToken , skipToken } from '@reduxjs/toolkit/query' ;
5
3
import { useEffect } from 'react' ;
6
4
7
5
interface LoadingAuth {
@@ -16,73 +14,37 @@ interface LoadedAuth {
16
14
17
15
type AuthResult = LoadingAuth | LoadedAuth ;
18
16
19
- export const useIsAuthenticated = ( skip ?: SkipToken ) : AuthResult => {
20
- const { data, isSuccess } = useGetSessionQuery ( skip , {
21
- refetchOnFocus : true ,
22
- refetchOnReconnect : true ,
23
- } ) ;
17
+ export const useIsAuthenticated = ( ) : AuthResult => {
18
+ const { data, isSuccess, isError, isLoading } = useGetUserQuery ( ) ;
24
19
25
20
if ( isSuccess ) {
26
- return { isAuthenticated : data ?. session . active ?? false , isLoadingAuth : false } ;
21
+ return { isAuthenticated : data !== undefined , isLoadingAuth : false } ;
27
22
}
28
23
29
- return { isAuthenticated : undefined , isLoadingAuth : true } ;
30
- } ;
31
-
32
- interface LoadedUser {
33
- user : IUser ;
34
- isLoadingUser : false ;
35
- isAuthenticated : true ;
36
- }
37
-
38
- interface LoadingUser {
39
- user : undefined ;
40
- isLoadingUser : true ;
41
- isAuthenticated : true ;
42
- }
43
-
44
- interface LoadedAnonymous {
45
- user : undefined ;
46
- isLoadingUser : false ;
47
- isAuthenticated : false ;
48
- }
49
-
50
- const LOADED_ANONYMOUS : LoadedAnonymous = { user : undefined , isLoadingUser : false , isAuthenticated : false } ;
51
- const LOADING_USER : LoadingUser = { user : undefined , isLoadingUser : true , isAuthenticated : true } ;
52
-
53
- type UserResult = LoadedUser | LoadingUser | LoadedAnonymous ;
54
-
55
- export const useUser = ( ) : UserResult => {
56
- const { isAuthenticated, isLoadingAuth } = useIsAuthenticated ( ) ;
57
- const { data : user , isLoading : isLoadingUser } = useGetUserQuery ( isAuthenticated === true ? undefined : skipToken ) ;
58
-
59
- if ( isAuthenticated === false ) {
60
- return LOADED_ANONYMOUS ;
24
+ if ( isError ) {
25
+ return { isAuthenticated : false , isLoadingAuth : false } ;
61
26
}
62
27
63
- if ( user === undefined ) {
64
- return isLoadingAuth || isLoadingUser ? LOADING_USER : LOADED_ANONYMOUS ;
28
+ if ( isLoading ) {
29
+ return { isAuthenticated : undefined , isLoadingAuth : true } ;
65
30
}
66
31
67
- return { user , isLoadingUser : false , isAuthenticated : true } ;
32
+ return { isAuthenticated : undefined , isLoadingAuth : true } ;
68
33
} ;
69
34
70
35
/** Only for use in authorized contexts.
71
36
* If the user is unauthorized, it will redirect to the login page.
72
37
* It will return a loading state until the user is loaded or redirected.
73
38
*/
74
- export const useUserRequired = ( ) : LoadingUser | LoadedUser => {
75
- const { user, isLoadingUser, isAuthenticated } = useUser ( ) ;
39
+ export const useUserRequired = ( ) => {
40
+ const user = useGetUserQuery ( ) ;
41
+ const { data, isSuccess } = user ;
76
42
77
43
useEffect ( ( ) => {
78
- if ( isAuthenticated === false ) {
44
+ if ( isSuccess && data === undefined ) {
79
45
login ( ) ;
80
46
}
81
- } , [ isAuthenticated ] ) ;
82
-
83
- if ( isLoadingUser || ! isAuthenticated ) {
84
- return LOADING_USER ;
85
- }
47
+ } , [ data , isSuccess ] ) ;
86
48
87
- return { user, isLoadingUser : false , isAuthenticated : true } ;
49
+ return user ;
88
50
} ;
0 commit comments