-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using ReactKeycloakProvider for dealing with Access and Refresh Token
- Loading branch information
1 parent
2d516f1
commit 625937f
Showing
6 changed files
with
78 additions
and
56 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
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 |
---|---|---|
@@ -1,32 +1,49 @@ | ||
import { useEffect, useState, useRef } from "react" | ||
import Keycloak from "keycloak-js" | ||
|
||
const client = new Keycloak({ | ||
|
||
"realm": "nano", | ||
"url": "https://iam.ideaconsult.net/auth", | ||
"ssl-required": "external", | ||
"resource": "idea-ui", | ||
"public-client": true, | ||
"confidential-port": 0, | ||
"clientId": 'nano' | ||
|
||
}) | ||
|
||
// const useAuth = () => { | ||
// const isRun = useRef(false) | ||
// const [isLogin, setIsLoging] = useState(false) | ||
|
||
// useEffect(() => { | ||
// if (isRun.current) return | ||
|
||
// isRun.current = true; | ||
// client.init({ onLoad: "login-required" }).then((res) => setIsLoging(res)) | ||
// }, []) | ||
// return isLogin | ||
|
||
// } | ||
|
||
const doLogin = () => client.init({ onLoad: "login-required" }).then((res) => setIsLoging(res)); | ||
|
||
export default doLogin; | ||
// // index.js or App.js | ||
// import React, { useEffect, useState } from 'react'; | ||
// import ReactDOM from 'react-dom'; | ||
// import App from './App'; | ||
// import keycloak from './keycloak'; | ||
|
||
// const Root = () => { | ||
// const [authenticated, setAuthenticated] = useState(false); | ||
|
||
// useEffect(() => { | ||
// const initKeycloak = async () => { | ||
// try { | ||
// const auth = await keycloak.init({ | ||
// onLoad: 'login-required', | ||
// checkLoginIframe: false, // Disable iframe-based token refresh | ||
// }); | ||
// setAuthenticated(auth); | ||
|
||
// if (auth) { | ||
// // Save initial token to local storage | ||
// localStorage.setItem('token', keycloak.token); | ||
|
||
// // Set up token auto-refresh | ||
// setInterval(async () => { | ||
// try { | ||
// const refreshed = await keycloak.updateToken(70); // Refresh token if valid for less than 70 seconds | ||
// if (refreshed) { | ||
// console.log('Token refreshed'); | ||
// localStorage.setItem('token', keycloak.token); // Save new token | ||
// } else { | ||
// console.log('Token is still valid'); | ||
// } | ||
// } catch (error) { | ||
// console.error('Failed to refresh token', error); | ||
// } | ||
// }, 5 * 60 * 1000); // Check every 5 minutes | ||
// } | ||
// } catch (error) { | ||
// console.error('Keycloak initialization failed', error); | ||
// } | ||
// }; | ||
|
||
// initKeycloak(); | ||
// }, []); | ||
|
||
// return authenticated ? <App /> : <div>Loading...</div>; | ||
// }; | ||
|
||
// ReactDOM.render(<Root />, document.getElementById('root')); |
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