1- import { type AuthProvider , PreviousLocationStorageKey } from "ra-core" ;
2-
1+ import { type AuthProvider , PreviousLocationStorageKey } from 'ra-core' ;
32
43export type ApisixAuthProviderParams = {
54 loginURL ?: string ;
@@ -27,56 +26,48 @@ export type ApisixAuthProviderParams = {
2726 * meURL: 'http://localhost:9080/oidc/me',
2827 * });
2928 */
30- export const apisixOidcAuthProvider : ( options ?: ApisixAuthProviderParams ) => AuthProvider = ( options ) => {
29+ export const apisixOidcAuthProvider = (
30+ options ?: ApisixAuthProviderParams
31+ ) : AuthProvider => {
3132 const {
3233 loginURL = `${ window . location . origin } /oidc/login` ,
3334 logoutURL = `${ window . location . origin } /oidc/logout` ,
3435 userInfoURL = `${ window . location . origin } /oidc/me` ,
3536 storage = localStorage ,
3637 } = options || { } ;
37- let isRedirecting = false ;
3838 return {
3939 login : ( ) => {
4040 return Promise . reject ( ) ;
4141 } ,
4242 logout : async ( ) => {
43- const accessToken = storage . getItem ( " access_token" ) ;
43+ const accessToken = storage . getItem ( ' access_token' ) ;
4444 if ( ! accessToken ) {
45- return Promise . resolve ( ) ;
45+ return Promise . reject ( ) ;
4646 }
47- storage . removeItem ( "access_token" ) ;
48- return Promise . resolve ( logoutURL ) ;
47+ storage . removeItem ( 'access_token' ) ;
48+ window . location . href = logoutURL ;
49+ return Promise . reject ( ) ;
4950 } ,
50- checkError : ( error ) => {
51+ checkError : error => {
5152 if ( error . status === 401 ) {
52- storage . removeItem ( "access_token" ) ;
53- if ( ! isRedirecting ) {
54- isRedirecting = true ;
55- saveCurrentLocation ( storage ) ;
56- setTimeout ( ( ) => {
57- window . location . href = loginURL ;
58- } , 100 ) ;
59- }
60- return Promise . reject ( { logoutUser : false } ) ;
53+ storage . removeItem ( 'access_token' ) ;
54+ saveCurrentLocation ( storage ) ;
55+ window . location . href = loginURL ;
56+ return Promise . reject ( ) ;
6157 }
6258 return Promise . resolve ( ) ;
6359 } ,
6460 checkAuth : async ( ) => {
65- const accessToken = storage . getItem ( " access_token" ) ;
61+ const accessToken = storage . getItem ( ' access_token' ) ;
6662 if ( ! accessToken ) {
67- if ( ! isRedirecting ) {
68- isRedirecting = true ;
69- saveCurrentLocation ( storage ) ;
70- setTimeout ( ( ) => {
71- window . location . href = loginURL ;
72- } , 100 ) ;
73- }
74- return Promise . reject ( { redirectTo : false } ) ;
63+ saveCurrentLocation ( storage ) ;
64+ window . location . href = loginURL ;
65+ return Promise . reject ( ) ;
7566 }
7667 return Promise . resolve ( ) ;
7768 } ,
7869 getIdentity : async ( ) => {
79- const accessToken = storage . getItem ( " access_token" ) ;
70+ const accessToken = storage . getItem ( ' access_token' ) ;
8071 if ( ! accessToken ) {
8172 return Promise . reject ( ) ;
8273 }
@@ -96,9 +87,9 @@ export const apisixOidcAuthProvider: (options?: ApisixAuthProviderParams) => Aut
9687 const identity = {
9788 ...user ,
9889 id : user . sub ,
99- fullName : user . preferred_username || user . name || "" ,
100- avatar : user . picture || "" ,
101- email : user . email || "" ,
90+ fullName : user . preferred_username || user . name || '' ,
91+ avatar : user . picture || '' ,
92+ email : user . email || '' ,
10293 roles : user . roles || [ ] ,
10394 } ;
10495 return Promise . resolve ( identity ) ;
@@ -112,17 +103,17 @@ export const apisixOidcAuthProvider: (options?: ApisixAuthProviderParams) => Aut
112103 if ( ! body . accessToken ) {
113104 return Promise . reject ( ) ;
114105 }
115- storage . setItem ( " access_token" , body . accessToken ) ;
106+ storage . setItem ( ' access_token' , body . accessToken ) ;
116107 } ,
117- }
108+ } ;
118109} ;
119110
120111const saveCurrentLocation = ( storage : Storage ) => {
121- if ( window . location . href . includes ( " login" ) ) {
112+ if ( window . location . href . includes ( ' login' ) ) {
122113 return ; // Do not save the location if it's the login page
123114 }
124115 const locationToSave = window . location . href
125- . replace ( window . location . origin , "" )
126- . replace ( " /#/" , "/" ) ;
116+ . replace ( window . location . origin , '' )
117+ . replace ( ' /#/' , '/' ) ;
127118 storage . setItem ( PreviousLocationStorageKey , locationToSave ) ;
128119} ;
0 commit comments