Skip to content

Commit 000c7a7

Browse files
fix: catch / display error on login callback
OKTA-361608 <<<Jenkins Check-In of Tested SHA: abdaaf9 for eng_productivity_ci_bot_okta@okta.com>>> Artifact: okta-react
1 parent 67626d1 commit 000c7a7

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

src/LoginCallback.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ const LoginCallback: React.FC<{
1919
onAuthResume?: OnAuthResumeFunction,
2020
}> = ({ errorComponent, onAuthResume }) => {
2121
const { oktaAuth, authState } = useOktaAuth();
22+
const [callbackError, setCallbackError] = React.useState(null);
23+
2224
const authStateReady = !authState.isPending;
2325
const ErrorReporter = errorComponent || OktaError;
24-
2526
React.useEffect(() => {
26-
2727
if (onAuthResume && oktaAuth.isInteractionRequired?.() ) {
2828
onAuthResume();
2929
return;
3030
}
31-
oktaAuth.handleLoginRedirect()
32-
.catch( err => {
33-
console.log(err); //TODO: handle these errors OKTA-361608
34-
});
35-
31+
oktaAuth.handleLoginRedirect().catch(e => {
32+
setCallbackError(e);
33+
});
3634
}, [oktaAuth]);
3735

38-
if(authStateReady && authState.error) {
39-
return <ErrorReporter error={authState.error}/>;
36+
const authError = authStateReady ? authState.error : null;
37+
const displayError = callbackError || authError;
38+
if (displayError) {
39+
return <ErrorReporter error={displayError}/>;
4040
}
4141

4242
return null;

test/jest/loginCallback.test.tsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import * as React from 'react';
1414
import { mount } from 'enzyme';
15+
import { act } from 'react-dom/test-utils';
1516
import LoginCallback from '../../src/LoginCallback';
1617
import Security from '../../src/Security';
1718

@@ -138,16 +139,44 @@ describe('<LoginCallback />', () => {
138139
expect(wrapper.text()).toBe(''); // no output since we expect to be redirected
139140
});
140141

141-
it('will treat isInteractionRequired like a normal error if not onAuthResume is passed', () => {
142-
oktaAuth.isInteractionRequired = jest.fn().mockImplementation( () => true );
142+
it('renders errors caught from handleLoginRedirect', async () => {
143+
const errorMsg = 'error on callback';
144+
authState.isPending = true;
145+
authState.isAuthenticated = false;
146+
oktaAuth.handleLoginRedirect.mockImplementation(() => {
147+
return Promise.reject(new Error(errorMsg));
148+
});
143149
const wrapper = mount(
144150
<Security {...mockProps}>
145151
<LoginCallback />
146152
</Security>
147153
);
148-
expect(wrapper.text()).toBe(''); // TODO: should be noticed as an error OKTA-361608
154+
return await act(async () => {
155+
await wrapper.update(); // set state
156+
await wrapper.update(); // render error
157+
expect(wrapper.text()).toBe('Error: ' + errorMsg);
158+
});
149159
});
150160

161+
it('will treat isInteractionRequired like a normal error if not onAuthResume is passed', async () => {
162+
oktaAuth.isInteractionRequired = jest.fn().mockImplementation( () => true );
163+
const errorMsg = 'error on callback';
164+
authState.isPending = true;
165+
authState.isAuthenticated = false;
166+
oktaAuth.handleLoginRedirect.mockImplementation(() => {
167+
return Promise.reject(new Error(errorMsg));
168+
});
169+
const wrapper = mount(
170+
<Security {...mockProps}>
171+
<LoginCallback />
172+
</Security>
173+
);
174+
return await act(async () => {
175+
await wrapper.update(); // set state
176+
await wrapper.update(); // render error
177+
expect(wrapper.text()).toBe('Error: ' + errorMsg);
178+
});
179+
});
151180
});
152181

153182
});

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,9 +4186,9 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7:
41864186
sha.js "^2.4.8"
41874187

41884188
cross-fetch@^3.0.6:
4189-
version "3.1.3"
4190-
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.3.tgz#b8e7d5f19161c4a0ca916f707978848786043afb"
4191-
integrity sha512-2i6v88DTqVBNODyjD9U6Ycn/uSZNvyHe25cIbo2fFnAACAsaLTJsd23miRWiR5NuiGXR9wpJ9d40/9WAhjDIrw==
4189+
version "3.1.4"
4190+
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.4.tgz#9723f3a3a247bf8b89039f3a380a9244e8fa2f39"
4191+
integrity sha512-1eAtFWdIubi6T4XPy6ei9iUFoKpUkIF971QLN8lIvvvwueI65+Nw5haMNKUwfJxabqlIIDODJKGrQ66gxC0PbQ==
41924192
dependencies:
41934193
node-fetch "2.6.1"
41944194

0 commit comments

Comments
 (0)