From 2af8d7efb8e12ddcd395d01a3d6f97a7e1e52d59 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Wed, 6 Jan 2021 12:53:57 -0500 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20lookup=20authenticat?= =?UTF-8?q?or=20by=20auth=20method=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/core/addon/decorators/notify.js | 23 +++++++-------- .../scopes/scope/authenticate/method.js | 29 +++++++++---------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/addons/core/addon/decorators/notify.js b/addons/core/addon/decorators/notify.js index 42204023f..1ce13d91e 100644 --- a/addons/core/addon/decorators/notify.js +++ b/addons/core/addon/decorators/notify.js @@ -50,25 +50,22 @@ export function notifyError(notification, options = { catch: false }) { desc.value = async function () { const owner = getOwner(this); const notifyService = owner.lookup('service:notify'); - //const intlService = owner.lookup('service:intl'); + const intlService = owner.lookup('service:intl'); + try { return await method.apply(this, arguments); } catch (error) { - // As of now all error notifications pass a function, but this code - // could support either a function or a string. - // const candidateKey = (typeof notification === 'function') - // ? notification.apply(this, [error]) - // : notification; - const candidateKey = notification.apply(this, [error]); - // As of now, we only use untranslated strings for error notifications, - // but this code would support both translated and arbitrary strings. - // const text = intlService.exists(candidateKey) - // ? intlService.t(candidateKey) - // : candidateKey; - const text = candidateKey; + const candidateKey = (typeof notification === 'function') + ? notification.apply(this, [error]) + : notification; + + const text = intlService.exists(candidateKey) + ? intlService.t(candidateKey) + : candidateKey; notifyService.error(text, { closeAfter: null }); + if (options.catch) { // squelch the error } else { diff --git a/ui/admin/app/routes/scopes/scope/authenticate/method.js b/ui/admin/app/routes/scopes/scope/authenticate/method.js index 7f9f87856..1caa35d95 100644 --- a/ui/admin/app/routes/scopes/scope/authenticate/method.js +++ b/ui/admin/app/routes/scopes/scope/authenticate/method.js @@ -2,6 +2,7 @@ import Route from '@ember/routing/route'; import { inject as service } from '@ember/service'; import { action } from '@ember/object'; import loading from 'ember-loading/decorator'; +import { notifyError } from 'core/decorators/notify'; export default class ScopesScopeAuthenticateMethodRoute extends Route { // =services @@ -35,28 +36,24 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route { // =actions /** - * Delegates authentication to the `password` authenticator. - * If authentication fails, notifies with an alert. + * Delegates authentication to an authenticator of type matching the current + * auth method. If authentication fails, notifies with an alert. */ @action @loading + @notifyError(() => 'errors.authentication-failed.title', { catch: true }) async authenticate(creds) { this.resetCredentials(); - const authenticatorName = 'authenticator:password'; const scope = this.modelFor('scopes.scope'); - const authMethod = this.currentModel; + const authMethod = this.modelFor('scopes.scope.authenticate.method'); + const authenticatorName = `authenticator:${authMethod.type}`; const requestCookies = false; - try { - await this.session.authenticate( - authenticatorName, - creds, - requestCookies, - { scope, authMethod } - ); - this.transitionTo('index'); - } catch (e) { - const errorMessage = this.intl.t('errors.authentication-failed.title'); - this.notify.error(errorMessage, { closeAfter: null }); - } + await this.session.authenticate( + authenticatorName, + creds, + requestCookies, + { scope, authMethod } + ); + this.transitionTo('index'); } }