diff --git a/packages/okta-react/src/SecureRoute.js b/packages/okta-react/src/SecureRoute.js index 9606fa957..d41fff48e 100644 --- a/packages/okta-react/src/SecureRoute.js +++ b/packages/okta-react/src/SecureRoute.js @@ -12,17 +12,22 @@ import React, { useEffect } from 'react'; import { useOktaAuth } from './OktaContext'; -import { Route } from 'react-router-dom'; +import { Route, useRouteMatch } from 'react-router-dom'; const SecureRoute = ( props ) => { const { authService, authState } = useOktaAuth(); + const match = useRouteMatch(props); useEffect(() => { + // Only process logic if the route matches + if (!match) { + return; + } // Start login if and only if app has decided it is not logged inn if(!authState.isAuthenticated && !authState.isPending) { authService.login(); } - }, [authState.isPending, authState.isAuthenticated, authService]); + }, [authState.isPending, authState.isAuthenticated, authService, match]); if (!authState.isAuthenticated) { return null; diff --git a/packages/okta-react/test/jest/secureRoute.test.js b/packages/okta-react/test/jest/secureRoute.test.js index e96ffbf82..e052cd521 100644 --- a/packages/okta-react/test/jest/secureRoute.test.js +++ b/packages/okta-react/test/jest/secureRoute.test.js @@ -132,16 +132,27 @@ describe('', () => { authState.isPending = false; }); - it('calls login()', () => { + it('calls login() if route matches', () => { mount( - + ); expect(authService.login).toHaveBeenCalled(); }); + + it('does not call login() if route does not match', () => { + mount( + + + + + + ); + expect(authService.login).not.toHaveBeenCalled(); + }); }); describe('isPending: true', () => {