You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.4 KiB

3 years ago
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import './index.css';
  4. import App from './App';
  5. import reportWebVitals from './reportWebVitals';
  6. import Keycloak from "keycloak-js";
  7. const initOptions = {
  8. url: 'http://localhost:8080/auth', realm: 'demorealm', clientId: 'react-app', onLoad: 'login-required'
  9. }
  10. const keycloak = Keycloak(initOptions);
  11. keycloak.init({ onLoad: initOptions.onLoad }).then((auth) => {
  12. if (!auth) {
  13. window.location.reload();
  14. } else {
  15. ReactDOM.render(
  16. <React.StrictMode>
  17. <App keycloak={keycloak}/>
  18. </React.StrictMode>,
  19. document.getElementById('root')
  20. );
  21. // If you want to start measuring performance in your app, pass a function
  22. // to log results (for example: reportWebVitals(console.log))
  23. // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
  24. reportWebVitals();
  25. }
  26. //Token Refresh
  27. setInterval(() => {
  28. keycloak.updateToken(70).then((refreshed) => {
  29. if (refreshed) {
  30. console.info('Token refreshed' + refreshed);
  31. } else {
  32. console.warn('Token not refreshed, valid for '
  33. + Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds');
  34. }
  35. }).catch(() => {
  36. console.error('Failed to refresh token');
  37. });
  38. }, 1000)
  39. }).catch(() => {
  40. console.error("Authenticated Failed");
  41. });