Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Okta Custom Implicit Callback workaround for infinite loop if all routes are secured
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