From 0ed60ab6e7b63b2026d8e324653ef7127f3dc46d Mon Sep 17 00:00:00 2001 From: jasquat Date: Mon, 11 Jul 2022 14:20:58 -0400 Subject: [PATCH] added a config to turn keycloak on and off w/ burnettk --- cypress/support/commands.js | 10 +++++++--- src/config.tsx | 2 ++ src/index.tsx | 1 - src/services/UserService.ts | 40 +++++++++++++++++++++---------------- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/cypress/support/commands.js b/cypress/support/commands.js index bc5051e..8216ee8 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -1,3 +1,5 @@ +import { AUTH_WITH_KEYCLOAK } from '../../src/config'; + // *********************************************** // This example commands.js shows you how to // create various custom commands and overwrite @@ -26,9 +28,11 @@ Cypress.Commands.add('signInToAdmin', (selector, ...args) => { cy.visit('/admin'); - // cy.get('#username').type('ciadmin1'); - // cy.get('#password').type('ciadmin1'); - // cy.get('#kc-login').click(); + if (AUTH_WITH_KEYCLOAK) { + cy.get('#username').type('ciadmin1'); + cy.get('#password').type('ciadmin1'); + cy.get('#kc-login').click(); + } }); Cypress.Commands.add('getBySel', (selector, ...args) => { diff --git a/src/config.tsx b/src/config.tsx index 9888ece..4d7fb40 100644 --- a/src/config.tsx +++ b/src/config.tsx @@ -29,3 +29,5 @@ export const PROCESS_STATUSES = [ ]; export const DATE_FORMAT = 'yyyy-MM-dd HH:mm:ss'; + +export const AUTH_WITH_KEYCLOAK = true; diff --git a/src/index.tsx b/src/index.tsx index aaa1817..f10db1d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -20,7 +20,6 @@ const doRender = () => { }; UserService.initKeycloak(doRender); -// doRender(); // If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.log)) diff --git a/src/services/UserService.ts b/src/services/UserService.ts index 1be0a4a..686e927 100644 --- a/src/services/UserService.ts +++ b/src/services/UserService.ts @@ -1,4 +1,5 @@ import Keycloak from 'keycloak-js'; +import { AUTH_WITH_KEYCLOAK } from '../config'; const keycloakClient = new Keycloak('/keycloak.json'); @@ -15,8 +16,11 @@ const updateToken = (successCallback: any) => const getUsername = () => keycloakClient.tokenParsed?.preferred_username; -const hasRole = (_roles: string[]) => { - // roles.some((role: string) => keycloakClient.hasRealmRole(role)); +const hasRole = (roles: string[]) => { + if (AUTH_WITH_KEYCLOAK) { + return roles.some((role: string) => keycloakClient.hasRealmRole(role)); + } + return true; }; @@ -26,21 +30,23 @@ const hasRole = (_roles: string[]) => { * @param onAuthenticatedCallback */ const initKeycloak = (onAuthenticatedCallback: any) => { - // FIXME actually auth - onAuthenticatedCallback(); - // keycloakClient - // .init({ - // onLoad: 'check-sso', - // silentCheckSsoRedirectUri: `${window.location.origin}/silent-check-sso.html`, - // pkceMethod: 'S256', - // }) - // .then((authenticated: any) => { - // if (!authenticated) { - // doLogin(); - // } - // onAuthenticatedCallback(); - // }) - // .catch(console.error); + if (AUTH_WITH_KEYCLOAK) { + keycloakClient + .init({ + onLoad: 'check-sso', + silentCheckSsoRedirectUri: `${window.location.origin}/silent-check-sso.html`, + pkceMethod: 'S256', + }) + .then((authenticated: any) => { + if (!authenticated) { + doLogin(); + } + onAuthenticatedCallback(); + }) + .catch(console.error); + } else { + onAuthenticatedCallback(); + } }; const UserService = {