Skip to content

Commit c581671

Browse files
authored
[7.x] Improve login UI error message. (#75655)
1 parent 65293aa commit c581671

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

x-pack/plugins/security/public/authentication/login/components/login_form/login_form.test.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,58 @@ describe('LoginForm', () => {
421421
expect(window.location.href).toBe(currentURL);
422422
expect(coreStartMock.notifications.toasts.addError).toHaveBeenCalledWith(failureReason, {
423423
title: 'Could not perform login.',
424+
toastMessage: 'Oh no!',
424425
});
425426
});
426427

428+
it('shows error with message in the `body`', async () => {
429+
const currentURL = `https://some-host/login?next=${encodeURIComponent(
430+
'/some-base-path/app/kibana#/home?_g=()'
431+
)}`;
432+
433+
const coreStartMock = coreMock.createStart({ basePath: '/some-base-path' });
434+
coreStartMock.http.post.mockRejectedValue({
435+
body: { message: 'Oh no! But with much more details!' },
436+
message: 'Oh no!',
437+
});
438+
439+
window.location.href = currentURL;
440+
const wrapper = mountWithIntl(
441+
<LoginForm
442+
http={coreStartMock.http}
443+
notifications={coreStartMock.notifications}
444+
loginAssistanceMessage=""
445+
selector={{
446+
enabled: true,
447+
providers: [
448+
{ type: 'basic', name: 'basic', usesLoginForm: true },
449+
{ type: 'saml', name: 'saml1', usesLoginForm: false },
450+
],
451+
}}
452+
/>
453+
);
454+
455+
expectPageMode(wrapper, PageMode.Selector);
456+
457+
wrapper.findWhere((node) => node.key() === 'saml1').simulate('click');
458+
459+
await act(async () => {
460+
await nextTick();
461+
wrapper.update();
462+
});
463+
464+
expect(coreStartMock.http.post).toHaveBeenCalledTimes(1);
465+
expect(coreStartMock.http.post).toHaveBeenCalledWith('/internal/security/login', {
466+
body: JSON.stringify({ providerType: 'saml', providerName: 'saml1', currentURL }),
467+
});
468+
469+
expect(window.location.href).toBe(currentURL);
470+
expect(coreStartMock.notifications.toasts.addError).toHaveBeenCalledWith(
471+
new Error('Oh no! But with much more details!'),
472+
{ title: 'Could not perform login.', toastMessage: 'Oh no!' }
473+
);
474+
});
475+
427476
it('properly switches to login form', async () => {
428477
const currentURL = `https://some-host/login?next=${encodeURIComponent(
429478
'/some-base-path/app/kibana#/home?_g=()'

x-pack/plugins/security/public/authentication/login/components/login_form/login_form.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,15 @@ export class LoginForm extends Component<Props, State> {
451451

452452
window.location.href = location;
453453
} catch (err) {
454-
this.props.notifications.toasts.addError(err, {
455-
title: i18n.translate('xpack.security.loginPage.loginSelectorErrorMessage', {
456-
defaultMessage: 'Could not perform login.',
457-
}),
458-
});
454+
this.props.notifications.toasts.addError(
455+
err?.body?.message ? new Error(err?.body?.message) : err,
456+
{
457+
title: i18n.translate('xpack.security.loginPage.loginSelectorErrorMessage', {
458+
defaultMessage: 'Could not perform login.',
459+
}),
460+
toastMessage: err?.message,
461+
}
462+
);
459463

460464
this.setState({ loadingState: { type: LoadingStateType.None } });
461465
}

0 commit comments

Comments
 (0)