Skip to content

Instantly share code, notes, and snippets.

@Kannndev
Last active August 28, 2020 06:12
Show Gist options
  • Save Kannndev/807e23812a1580b395248fa5d5d1b543 to your computer and use it in GitHub Desktop.
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
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