Last active
August 28, 2020 06:12
-
-
Save Kannndev/807e23812a1580b395248fa5d5d1b543 to your computer and use it in GitHub Desktop.
Okta Custom Implicit Callback workaround for infinite loop if all routes are secured
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
import { Redirect } from 'react-router-dom'; | |
import { withOktaAuth } from '@okta/okta-react'; | |
import PropTypes from 'prop-types'; | |
const CustomImplicitCallback = (props) => { | |
const { authService, authState } = props; | |
const [authInfo, setAuthInfo] = useState({}); | |
useEffect(() => { | |
authService.handleAuthentication().then(() => { | |
setAuthInfo({ | |
authenticated: authState.isAuthenticated, | |
error: authState.error && authState.error.message, | |
errorCode: authState.error && authState.error.errorCode, | |
}); | |
}); | |
}, [authService, authState]); | |
let location = '/'; | |
if (authInfo.authenticated !== null) { | |
const referrerKey = 'secureRouterReferrerPath'; | |
location = localStorage.getItem(referrerKey) || '/'; | |
localStorage.removeItem(referrerKey); | |
} | |
return ( | |
<> | |
{authInfo.errorCode === 'access_denied' && ( | |
<Redirect to="/unauthorized" /> | |
)} | |
{authInfo.authenticated ? ( | |
<Redirect to={location} /> | |
) : ( | |
<p>{authInfo.error}</p> | |
)} | |
</> | |
); | |
}; | |
CustomImplicitCallback.propTypes = { | |
authService: PropTypes.oneOfType([PropTypes.object]), | |
authState: PropTypes.oneOfType([PropTypes.object]), | |
}; | |
export default withOktaAuth(CustomImplicitCallback); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment