@@ -26,6 +26,8 @@ import { useQuery } from '@tanstack/react-query';
2626import { useEffect } from 'react' ;
2727import { Trans , useTranslation } from 'react-i18next' ;
2828import { useDispatch } from 'react-redux' ;
29+ import { useLocation } from 'react-router' ;
30+ import { getAppUrl } from '../../helpers/getAppUrl' ;
2931import { getCluster } from '../../lib/cluster' ;
3032import { getSelectedClusters } from '../../lib/cluster' ;
3133import { useClustersConf } from '../../lib/k8s' ;
@@ -143,7 +145,11 @@ const fetchConfig = (dispatch: Dispatch<UnknownAction>) => {
143145 clustersToConfig [ cluster . name ] = cluster ;
144146 } ) ;
145147
146- const configToStore = { ...config , clusters : clustersToConfig } ;
148+ const configToStore = {
149+ ...config ,
150+ clusters : clustersToConfig ,
151+ oidcAutoLogin : config . oidcAutoLogin ,
152+ } ;
147153
148154 if ( clusters === null ) {
149155 dispatch ( setConfig ( configToStore ) ) ;
@@ -188,6 +194,7 @@ export default function Layout({}: LayoutProps) {
188194 const isFullWidth = useTypedSelector ( state => state . ui . isFullWidth ) ;
189195 const { t } = useTranslation ( ) ;
190196 const allClusters = useClustersConf ( ) ;
197+ const location = useLocation ( ) ;
191198
192199 /** This fetches the cluster config from the backend and updates the redux store on an interval.
193200 * When stateless clusters are enabled, it also fetches the stateless cluster config from the
@@ -224,6 +231,36 @@ export default function Layout({}: LayoutProps) {
224231
225232 const panels = useUIPanelsGroupedBySide ( ) ;
226233
234+ const oidcAutoLogin = useTypedSelector ( state => state . config . oidcAutoLogin ) ;
235+
236+ useEffect ( ( ) => {
237+ if ( ! oidcAutoLogin || ! clusters ) {
238+ return ;
239+ }
240+ const urlParams = new URLSearchParams ( window . location . search ) ;
241+ const isLoggingOut = urlParams . get ( 'logout' ) === 'true' ;
242+ if ( isLoggingOut || ! ! error ) {
243+ return ;
244+ }
245+ const isCallbackPath =
246+ window . location . pathname . includes ( 'oidc-callback' ) ||
247+ urlParams . has ( 'code' ) ||
248+ urlParams . has ( 'state' ) ;
249+ if ( isCallbackPath ) {
250+ return ;
251+ }
252+ const currentClusterName = getCluster ( ) ;
253+ const currentCluster = currentClusterName ? clusters [ currentClusterName ] : null ;
254+ const isOIDC = currentCluster ?. auth_type === 'oidc' ;
255+ if ( ! isOIDC ) {
256+ return ;
257+ }
258+ if ( currentCluster . useToken === undefined ) {
259+ const oauthUrl = `${ getAppUrl ( ) } oidc?dt=${ Date . now ( ) } &cluster=${ getCluster ( ) } ` ;
260+ window . location . href = oauthUrl ;
261+ }
262+ } , [ oidcAutoLogin , clusters , error , location . pathname ] ) ;
263+
227264 if ( ! disableBackendLoader ) {
228265 if ( error && ! config ) {
229266 return < ErrorPage message = { < Trans > Failed to connect to the backend</ Trans > } error = { error } /> ;
0 commit comments