Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Revert "API: Export keycloak config into a keycloak.json (remove hard coded config)"" #111

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions config-overrides.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
// Overrides create-react-app webpack configs without ejecting
// https://github.com/timarney/react-app-rewired
const path = require('path');

const { useBabelRc, override } = require('customize-cra');
module.exports = override(useBabelRc());

module.exports = override(
// Use Babel config from .babelrc
useBabelRc(),

// Add Webpack alias for Keycloak configuration based on the environment
(config) => {
const isProduction = process.env.NODE_ENV === 'production';

// Define the path to the correct keycloak-config based on the environment
config.resolve.alias['keycloak-config'] = path.resolve(
__dirname,
isProduction
? 'src/keycloak-config.prod.json' // Production environment
: 'src/keycloak-config.json' // Development environment
);

// Return the modified Webpack config
return config;
}
);
36 changes: 16 additions & 20 deletions src/context/AuthContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import axios from 'axios';
import Keycloak, { KeycloakConfig, KeycloakInitOptions } from 'keycloak-js';
import React, { createContext, useEffect, useState, useMemo } from 'react';

if (!process.env.REACT_APP_KEYCLOAK_API_URL) {
throw new Error('REACT_APP_KEYCLOAK_API_URL is not defined');
import keycloakConfig from 'keycloak-config';
import Keycloak, { KeycloakInitOptions } from 'keycloak-js';
import React, { createContext, useEffect, useMemo, useState } from 'react';

const getDotEnvKeycloakApiUrl = (): string => {
if (!process.env.REACT_APP_KEYCLOAK_API_URL) {
throw new Error('REACT_APP_KEYCLOAK_API_URL is not defined');
}
return process.env.REACT_APP_KEYCLOAK_API_URL;
}

const keycloakConfig: KeycloakConfig = {
realm: 'gaia-x',
clientId: 'portal',
url: process.env.REACT_APP_KEYCLOAK_API_URL,
};

const keycloak = new Keycloak(keycloakConfig);

const keycloakInitOptions: KeycloakInitOptions = {
onLoad: 'check-sso',
checkLoginIframe: false,
pkceMethod: 'S256',
};
const keycloak = new Keycloak({
...keycloakConfig.config,
url: getDotEnvKeycloakApiUrl()
});
const initOptions = keycloakConfig.initOptions as KeycloakInitOptions;

export interface AuthContextType {
isAuthenticated: boolean;
Expand All @@ -35,7 +31,7 @@ const defaultAuthContextValues: AuthContextType = {
token: '',
login: () => Promise.resolve(),
logout: () => Promise.resolve(),
hasRole: (role: string) => false,
hasRole: (_role: string) => false,
redirectPath: null,
setRedirectPath: () => {},
};
Expand All @@ -58,7 +54,7 @@ const AuthContextProvider: React.FC<AuthContextProviderProps> = ({
// Initialise Keycloak
useEffect(() => {
keycloak
.init(keycloakInitOptions)
.init(initOptions)
.then((authenticated) => {
setIsAuthenticated(authenticated);
if (authenticated) {
Expand Down
11 changes: 11 additions & 0 deletions src/keycloak-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"config": {
"realm": "gaia-x",
"clientId": "portal"
},
"initOptions": {
"onLoad": "check-sso",
"checkLoginIframe": false,
"pkceMethod": "S256"
}
}
11 changes: 11 additions & 0 deletions src/keycloak-config.prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"config": {
"realm": "gaia-x",
"clientId": "portal"
},
"initOptions": {
"onLoad": "check-sso",
"checkLoginIframe": false,
"pkceMethod": "S256"
}
}