Skip to content

Commit ba4103e

Browse files
committed
Fix error when authPRovider.checkError os not async
1 parent 36a452b commit ba4103e

File tree

1 file changed

+56
-57
lines changed

1 file changed

+56
-57
lines changed

packages/ra-core/src/auth/useLogoutIfAccessDenied.ts

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -55,78 +55,77 @@ const useLogoutIfAccessDenied = (): LogoutIfAccessDenied => {
5555
[navigate]
5656
);
5757

58-
const logoutIfAccessDenied = useCallback(
59-
(error?: any) => {
58+
const logoutIfAccessDenied = useCallback<LogoutIfAccessDenied>(
59+
async (error?: any) => {
6060
if (!authProvider) {
6161
return logoutIfAccessDeniedWithoutProvider();
6262
}
63-
return authProvider
64-
.checkError(error)
65-
.then(() => false)
66-
.catch(async e => {
67-
const logoutUser = e?.logoutUser ?? true;
68-
//manual debounce
69-
if (timer) {
70-
// side effects already triggered in this tick, exit
71-
return true;
72-
}
73-
timer = setTimeout(() => {
74-
timer = undefined;
75-
}, 0);
63+
try {
64+
await authProvider.checkError(error);
65+
return false;
66+
} catch (e: any) {
67+
const logoutUser = e?.logoutUser ?? true;
68+
// manual debounce
69+
if (timer) {
70+
return true; // side effects already triggered in this tick, exit
71+
}
72+
timer = setTimeout(() => {
73+
timer = undefined;
74+
}, 0);
7675

77-
const redirectTo =
78-
e && e.redirectTo != null
79-
? e.redirectTo
80-
: error && error.redirectTo
81-
? error.redirectTo
82-
: undefined;
76+
const redirectTo =
77+
e && e.redirectTo != null
78+
? e.redirectTo
79+
: error && error.redirectTo
80+
? error.redirectTo
81+
: undefined;
8382

84-
const shouldNotify = !(
85-
(e && e.message === false) ||
86-
(error && error.message === false) ||
87-
redirectTo?.startsWith('http')
88-
);
89-
if (shouldNotify) {
83+
const shouldNotify = !(
84+
(e && e.message === false) ||
85+
(error && error.message === false) ||
86+
redirectTo?.startsWith('http')
87+
);
88+
if (shouldNotify) {
89+
try {
9090
// notify only if not yet logged out
91-
authProvider
92-
.checkAuth({})
93-
.then(() => {
94-
if (logoutUser) {
95-
notify(
96-
getErrorMessage(
97-
e,
98-
'ra.notification.logged_out'
99-
),
100-
{ type: 'error' }
101-
);
102-
} else {
103-
notify(
104-
getErrorMessage(
105-
e,
106-
'ra.notification.not_authorized'
107-
),
108-
{ type: 'error' }
109-
);
110-
}
111-
})
112-
.catch(() => {});
91+
await authProvider.checkAuth({});
92+
if (logoutUser) {
93+
notify(
94+
getErrorMessage(
95+
e,
96+
'ra.notification.logged_out'
97+
),
98+
{ type: 'error' }
99+
);
100+
} else {
101+
notify(
102+
getErrorMessage(
103+
e,
104+
'ra.notification.not_authorized'
105+
),
106+
{ type: 'error' }
107+
);
108+
}
109+
} catch {
110+
// ignore
113111
}
112+
}
114113

115-
if (logoutUser) {
116-
logout({}, redirectTo);
117-
} else if (redirectTo) {
118-
handleRedirect(redirectTo);
119-
}
114+
if (logoutUser) {
115+
logout({}, redirectTo);
116+
} else if (redirectTo) {
117+
handleRedirect(redirectTo);
118+
}
120119

121-
return true;
122-
});
120+
return true;
121+
}
123122
},
124123
[authProvider, logout, notify, handleRedirect]
125124
);
126125
return logoutIfAccessDenied;
127126
};
128127

129-
const logoutIfAccessDeniedWithoutProvider = () => Promise.resolve(false);
128+
const logoutIfAccessDeniedWithoutProvider = async () => false;
130129

131130
/**
132131
* Call the authProvider.authError() method, using the error passed as argument.

0 commit comments

Comments
 (0)