|
1 |
| -import React, { useState, useEffect } from 'react'; |
| 1 | +import React from 'react'; |
2 | 2 | import { Route, RouteProps } from 'react-router-dom';
|
3 |
| -import { SendToSSO } from './send_to_sso'; |
4 |
| -import { useSession } from '../../context/auth_context/auth_context'; |
5 |
| -import { AuthenticationError } from '../../services/auth_service/auth_errors'; |
6 |
| -import { Logger } from '../../utilities/logger'; |
| 3 | +import { useKeycloak } from '@react-keycloak/web'; |
7 | 4 |
|
8 | 5 | export const PrivateRoute = (props: RouteProps) => {
|
9 |
| - const { checkIsAuthenticated, authError } = useSession(); |
10 |
| - const [authStatusChecked, setAuthStatusChecked] = useState<boolean>(false); |
11 |
| - const [isAuthed, setIsAuthed] = useState<boolean>(null); |
12 |
| - useEffect(() => { |
13 |
| - Logger.instance.debug('private_route:authError useEffect', authError); |
14 |
| - if (authError instanceof AuthenticationError) { |
15 |
| - setIsAuthed(false); |
16 |
| - } |
17 |
| - }, [authError]); |
18 |
| - useEffect(() => { |
19 |
| - if (!authStatusChecked) { |
20 |
| - checkIsAuthenticated().then(isAuthenticated => |
21 |
| - setIsAuthed(isAuthenticated) |
22 |
| - ); |
23 |
| - } |
24 |
| - setAuthStatusChecked(true); |
25 |
| - }, [checkIsAuthenticated, authStatusChecked]); |
26 |
| - |
27 |
| - if (isAuthed) { |
| 6 | + const { keycloak, initialized } = useKeycloak(); |
| 7 | + if (!initialized) { |
| 8 | + return <div />; |
| 9 | + } |
| 10 | + if (keycloak.authenticated) { |
28 | 11 | return <Route {...props} />;
|
29 |
| - } else if (isAuthed === false) { |
30 |
| - return <SendToSSO />; |
31 | 12 | } else {
|
32 |
| - return <div />; |
| 13 | + keycloak.login(); |
| 14 | + return <div /> |
33 | 15 | }
|
34 | 16 | };
|
0 commit comments