-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 201 - Logout button + cleanup (#214)
- Loading branch information
Showing
15 changed files
with
157 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,5 @@ TEST-*xml | |
venv/ | ||
coverage/ | ||
minio/minio_files/cthub/ | ||
decoder.env | ||
decoder.env | ||
docker-compose-local-dev.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { useState, useEffect } from 'react' | ||
import { KeycloakContext } from '../../contexts' | ||
import settings from '../settings' | ||
|
||
const KeycloakProvider = ({authClient, initOptions, LoadingComponent, children}) => { | ||
const keycloakEnabled = settings.ENABLE_KEYCLOAK | ||
const [loading, setLoading] = useState(keycloakEnabled ? true : false) | ||
const [keycloak, setKeycloak] = useState({}) | ||
|
||
useEffect(() => { | ||
if (keycloakEnabled) { | ||
authClient.init(initOptions).then(() => { | ||
setKeycloak(authClient) | ||
setLoading(false) | ||
}) | ||
} | ||
}, [keycloakEnabled, authClient, initOptions]) | ||
|
||
if (loading) { | ||
return <LoadingComponent/> | ||
} | ||
return ( | ||
<KeycloakContext.Provider value={keycloak}> | ||
{children} | ||
</KeycloakContext.Provider> | ||
) | ||
} | ||
|
||
export default KeycloakProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react' | ||
import { CircularProgress } from '@mui/material' | ||
|
||
const Loading = () => { | ||
return ( | ||
<div> | ||
<CircularProgress color="inherit" /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Loading |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
import useKeycloak from '../utilities/useKeycloak' | ||
|
||
const Logout = () => { | ||
const keycloak = useKeycloak(); | ||
if (keycloak.authenticated) { | ||
return ( | ||
<button | ||
onClick={() => { | ||
keycloak.logout() | ||
}} | ||
> | ||
Log out | ||
</button> | ||
) | ||
} | ||
return null | ||
} | ||
|
||
export default Logout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import axios from 'axios' | ||
import settings from '../settings'; | ||
import useKeycloak from './useKeycloak' | ||
|
||
const useAxios = (useDefault = false, opts = {}) => { | ||
if (useDefault) { | ||
return axios.create(opts) | ||
} | ||
const keycloak = useKeycloak() | ||
const instance = axios.create({ | ||
baseURL: settings.API_BASE, | ||
...opts, | ||
}) | ||
instance.interceptors.request.use(async (config) => { | ||
if (keycloak.authenticated) { | ||
try { | ||
await keycloak.updateToken(30) | ||
config.headers = { | ||
'Authorization': `Bearer ${keycloak.token}`, | ||
} | ||
} catch(error) { | ||
// do something here? | ||
} | ||
} | ||
return config | ||
}) | ||
return instance | ||
} | ||
|
||
export default useAxios |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useContext } from 'react' | ||
import { KeycloakContext } from '../../contexts' | ||
|
||
const useKeycloak = () => { | ||
const keycloak = useContext(KeycloakContext) | ||
return keycloak | ||
} | ||
|
||
export default useKeycloak |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createContext } from 'react' | ||
|
||
export const KeycloakContext = createContext({}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,26 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import ReactDOM from 'react-dom' | ||
import Keycloak from 'keycloak-js'; | ||
|
||
import Keycloak from './keycloak'; | ||
import settings from './app/settings'; | ||
import KeycloakProvider from './app/components/KeycloakProvider'; | ||
import App from './App'; | ||
import Loading from './app/components/Loading'; | ||
|
||
import './app/styles/index.scss'; | ||
|
||
import App from './App'; | ||
|
||
if (settings.ENABLE_KEYCLOAK) { | ||
ReactDOM.render( | ||
<Keycloak />, | ||
document.getElementById('root'), | ||
); | ||
} else { | ||
ReactDOM.render(<App />, document.getElementById('root')); | ||
const keycloak = new Keycloak() | ||
const keycloakInitOptions = { | ||
onLoad: 'check-sso', | ||
pkceMethod: 'S256' | ||
} | ||
|
||
ReactDOM.render( | ||
<KeycloakProvider | ||
authClient={keycloak} | ||
initOptions={keycloakInitOptions} | ||
LoadingComponent={Loading} | ||
> | ||
<App /> | ||
</KeycloakProvider>, | ||
document.getElementById('root') | ||
) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.