Skip to content
Draft
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
23 changes: 23 additions & 0 deletions judo-ui-react/src/main/resources/actor/src/auth/Auth.tsx.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@ import type { ReactNode } from 'react';
import { useEffect } from 'react';
import { useAuth, hasAuthParams } from 'react-oidc-context';
import { useTranslation } from 'react-i18next';
import { useSearchParams } from 'react-router-dom';
{{# if application.authentication }}
import { getUser, clearSecurityStorage } from '../auth';
{{/ if }}

export const Auth = ({ children }: { children?: ReactNode }) => {
const { isAuthenticated, isLoading, activeNavigator, signinRedirect, events } = useAuth();
const { t } = useTranslation();

{{# if application.authentication }}
// check current token's user name with given parameter (when defined)
useEffect(() => {
const search = window.location.search;
const params = new URLSearchParams(search);
const forcedUser = params.get('user');

if (forcedUser != null) {
// Get current user name from token
const { profile } = getUser();
const tokenUser = profile?.preferred_username;
if (tokenUser != null && tokenUser === forcedUser) {
clearSecurityStorage();
window.location.reload();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ?user search param should be removed before we reload I think since we won't need it any more.

}
}
}, [useSearchParams, getUser]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useSearchParams is not used, should not be needed in the dependencies array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getUser also never changes, so the dependency array should be empty I think.

{{/ if }}

// automatically sign-in
useEffect(() => {
if (!hasAuthParams() && !isAuthenticated && !activeNavigator && !isLoading) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{{> fragment.header.hbs }}

import { clearSecurityStorage } from '~/auth';
import { clearSecurityStorage, getUser } from '~/auth';
import type { HandleApplicationChange } from './interfaces';

export const changeApplication: HandleApplicationChange = (applicationKey: string) => {
const { profile } = getUser();
const tokenUser = profile?.preferred_username;
clearSecurityStorage();
const { origin } = window.location;
window.location.href = origin + '/' + applicationKey;
window.location.href = origin + '/' + applicationKey + "?user=" + tokenUser ;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since tokenUser could be an email address, we might need to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent it to make sure special characters are encoded in the param. Not sure, we should check.

};