refactor: 💡 lookup authenticator by auth method type

This commit is contained in:
Randall Morey committed 2021-01-06 12:53:57 -05:00
1 parent ce9a4ae9b2
commit 2af8d7efb8
2 files changed
+23 -29

No files matched your search

+10 -13
View File
@@ -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 {
@@ -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');
}
}