-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
authProvider.tsx
60 lines (51 loc) · 1.79 KB
/
authProvider.tsx
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
import { AuthProvider } from "react-admin";
import { signIn, signOut, useSession } from "next-auth/react";
import { OIDC_SERVER_URL } from "../../config/keycloak";
const authProvider: AuthProvider = {
// Nothing to do here, this function will never be called
login: async () => Promise.resolve(),
logout: async () => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { data: session } = useSession();
if (!session) {
return;
}
await signOut({
// @ts-ignore
callbackUrl: `${OIDC_SERVER_URL}/protocol/openid-connect/logout?id_token_hint=${session.idToken}&post_logout_redirect_uri=${window.location.origin}`,
});
},
checkError: async (error) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { data: session } = useSession();
const status = error.status;
// @ts-ignore
if (!session || session?.error === "RefreshAccessTokenError" || status === 401) {
await signIn("keycloak");
return;
}
if (status === 403) {
return Promise.reject({ message: "Unauthorized user!", logoutUser: false });
}
},
checkAuth: async () => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { data: session } = useSession();
// @ts-ignore
if (!session || session?.error === "RefreshAccessTokenError") {
await signIn("keycloak");
return;
}
return Promise.resolve();
},
getPermissions: () => Promise.resolve(),
// @ts-ignore
getIdentity: async () => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { data: session } = useSession();
return session ? Promise.resolve(session.user) : Promise.reject();
},
// Nothing to do here, this function will never be called
handleCallback: () => Promise.resolve(),
};
export default authProvider;