Skip to content

Changing Security's oktaAuth parameter means restoreOriginalUri is not called. #304

@Olimarrex

Description

@Olimarrex

Describe the bug

I have a Security component whose oktaAuth can change.
In the repro below, if the component starts with the dummyAuth oktaAuth, and is changed to realInstance. When I change it. If I was to swap to the realInstance and login, I would expect it to trigger restoreOriginalUri as it does if you start the application with the dummyInstance defaulting to false.

Reproduction Steps?

//REPRODUCTION
import { Security, useOktaAuth } from '@okta/okta-react';
import OktaAuth from '@okta/okta-auth-js';
import OktaSignIn from '@okta/okta-signin-widget';
import { useEffect, useRef, useState } from 'react';

const dummyAuthInstance = new OktaAuth({
	issuer: 'https://lorem.okta.com/oauth2/default',
	clientId: 'lorem',
	redirectUri: 'https://lorem.com'
});

const realInstance = new OktaAuth({
	issuer: 'https://{youroktaissuerhere}.okta.com/oauth2/default',
	clientId: 'yourclientIdHere',
	redirectUri: 'https://localhost:44431/auth/oktaLogin'
});

export default function ConditionalSecurity({ children }) {
	const [useDummy, setUseDummy] = useState(true);
	const instance = useDummy ? dummyAuthInstance : realInstance;
	return <>
		<button onClick={() => setUseDummy(!useDummy)}>Using Dummy? {String(useDummy)}</button>
		<Security oktaAuth={instance} restoreOriginalUri={(auth, originalUri) => {
			alert('REDIRECTING HAPPENS');
		}} >
			<OktaSignInWidget instance={instance} />
		</Security>
	</>;
}


function OktaSignInWidget({ instance }) {
	const oktaAuth = useOktaAuth();
	const widgetRef = useRef<any>();

	useEffect(() => {
		let widget: any = null;
		(async () => {
			if (!widgetRef.current) {
				return;
			}

			if (oktaAuth) {
				widget = new OktaSignIn(oktaAuth.oktaAuth.options);
				widget.showSignInToGetTokens({
					el: widgetRef.current,
				}).then((tokens) => instance.handleLoginRedirect(tokens)).catch();
			}
		})();
		return () => {
			widget?.remove();
		};
	}, [instance, oktaAuth]);

	return <div ref={widgetRef} />;
}
  1. Setup an SPA using the above.
  2. Run it.
  3. Click the button to swap useDummy to false.
  4. Log in.
  5. Note the lack of "REDIRECTING HAPPENS" alert.
  6. Change const [useDummy, setUseDummy] = useState(true); to const [useDummy, setUseDummy] = useState(false);
  7. Log in again. This time you should now see a "REDIRECTING HAPPENS" alert.

SDK Versions

System:
OS: Windows 11 10.0.26200
CPU: (22) x64 Intel(R) Core(TM) Ultra 7 155H
Memory: 5.64 GB / 31.47 GB
Binaries:
Node: 22.16.0 - C:\Program Files\nodejs\node.EXE
npm: 10.9.2 - C:\Program Files\nodejs\npm.CMD
pnpm: 10.12.1 - C:\Users\max\AppData\Local\pnpm\pnpm.EXE
Browsers:
Chrome: 148.0.7778.97
Edge: Chromium (140.0.3485.54)
Firefox: 150.0.1 - C:\Program Files\Mozilla Firefox\firefox.exe
npmPackages:
@okta/okta-auth-js: ^8.0.0 => 8.0.0
@okta/okta-react: ^6.11.0 => 6.11.0
@okta/okta-signin-widget: ^7.44.2 => 7.44.2
react: ^18.2.0 => 18.3.1
react-apexcharts: ^1.4.1 => 1.7.0
react-big-calendar: ^1.8.4 => 1.17.0
react-checkbox-tree: ^1.8.0 => 1.8.0
react-dnd: ^16.0.1 => 16.0.1
react-dom: ^18.2.0 => 18.3.1
react-grid-layout: ^1.4.1 => 1.5.0
react-redux: ^8.1.2 => 8.1.3
react-resizable: ^3.0.5 => 3.0.5
react-router: ^7.8.0 => 7.8.0
react-signature-canvas: 1.1.0-alpha.2 => 1.1.0-alpha.2
react-to-print: ^2.15.1 => 2.15.1
react-virtualized-auto-sizer: ^1.0.20 => 1.0.25
react-window: ^1.8.9 => 1.8.11

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions