@@ -14,7 +14,7 @@ export const useAuth = () => {
14
14
return value ;
15
15
} ;
16
16
17
- export const AuthProvider = ( { afterLogin , children } ) => {
17
+ export const AuthProvider = ( { children } ) => {
18
18
const navigate = useNavigate ( ) ;
19
19
const [ isAuthenticated , setAuthenticated ] = useState ( false ) ;
20
20
const location = useLocation ( ) ;
@@ -31,38 +31,47 @@ export const AuthProvider = ({ afterLogin, children }) => {
31
31
}
32
32
} ;
33
33
34
- const authenticate = async ( ) => {
35
- const access_token = getToken ( ) ;
34
+ const authenticate = async ( access_token ) => {
36
35
if ( access_token ) {
37
- const result = await customAxios ( )
36
+ const result = await customAxios ( {
37
+ headers : {
38
+ access_token,
39
+ Authorization : `Bearer ${ access_token } ` ,
40
+ } ,
41
+ } )
38
42
. get ( 'auth/authenticated-route' )
39
43
. then ( ( ) => true )
40
44
. catch ( ( ) => false ) ;
45
+ console . log ( 'authenticate result:' , result ) ;
41
46
setAuthenticated ( result ) ;
42
47
updateToken ( result ? access_token : null ) ;
48
+ } else {
49
+ setAuthenticated ( false ) ;
50
+ updateToken ( null ) ;
43
51
}
44
52
} ;
45
53
46
54
useEffect ( ( ) => {
47
- authenticate ( ) ;
55
+ console . log ( 'authenticate' ) ;
56
+ authenticate ( getToken ( ) ) ;
48
57
} , [ location ] ) ;
49
58
50
59
console . log ( '[Provider] token:' , getToken ( ) ) ;
51
60
52
- const handleLogin = ( { token, redirectUrl } ) => {
53
- console . log ( 'handleLogin' , token ) ;
54
- updateToken ( token ) ;
61
+ const handleLogin = async ( { token, redirectUrl } ) => {
62
+ console . log ( 'handleLogin' , token , redirectUrl ) ;
63
+ await authenticate ( token ) ;
55
64
const { location } = window ;
56
65
const url = new URL ( location . href ) ;
57
- const next = url . searchParams . get ( 'next' ) ;
58
- navigate ( redirectUrl || next || afterLogin || '/' ) ;
66
+ const next = url . searchParams . get ( 'next' ) || storage . get ( 'login_to' , '' ) || '' ;
67
+ storage . remove ( 'login_to' ) ;
68
+ navigate ( redirectUrl || next || '/' ) ;
59
69
} ;
60
70
61
- const handleLogout = ( options ) => {
71
+ const handleLogout = async ( options ) => {
62
72
const { redirectUrl } = options || { } ;
63
73
console . log ( 'handleLogout' , getToken ( ) ) ;
64
- updateToken ( null ) ;
65
- setAuthenticated ( false ) ;
74
+ await authenticate ( null ) ;
66
75
if ( redirectUrl ) {
67
76
navigate ( redirectUrl ) ;
68
77
}
@@ -89,6 +98,7 @@ export const ProtectedRoute = ({ redirectTo, children }) => {
89
98
const targetUrl = new URL ( redirectTo || '/' , origin ) ;
90
99
const next = location ?. pathname + location ?. search + location ?. hash ;
91
100
targetUrl . searchParams . append ( 'next' , encodeURI ( next ) ) ;
101
+ storage . set ( 'login_to' , next ) ;
92
102
const nextUrl = targetUrl . toString ( ) . replace ( targetUrl . origin , '' ) ;
93
103
return < Navigate to = { nextUrl || '/' } replace state = { { from : location } } /> ;
94
104
}
0 commit comments