chore: setup auth redirection logic

This commit is contained in:
Randall Morey committed 2020-07-24 18:08:37 -04:00
1 parent 659a494092
commit 6c1375fe16
6 files changed
+73 -2

No files matched your search

@@ -1,8 +1,19 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { hash } from 'rsvp';
export default class ScopesScopeAuthenticateRoute extends Route {
// =services
@service session;
// =attributes
routeIfAlreadyAuthenticated = 'scopes.scope.projects';
// =methods
/**
* Returns all auth methods for the current scope, along with the current
* scope and all scopes (for org navigation).
@@ -23,4 +34,10 @@ export default class ScopesScopeAuthenticateRoute extends Route {
});
}
redirect() {
if (this.session.isAuthenticated) {
this.transitionTo(this.routeIfAlreadyAuthenticated);
}
}
}
@@ -0,0 +1,20 @@
import Route from '@ember/routing/route';
export default class ScopesScopeAuthenticateIndexRoute extends Route {
// =methods
/**
* This index route exists solely to redirect the user on to the
* first auth method.
* @param {object} model
* @param {[AuthMethodModel]} model.authMethods
*/
redirect({ authMethods }) {
const firstAuthMethod = authMethods.firstObject;
if (firstAuthMethod) {
this.transitionTo('scopes.scope.authenticate.method', firstAuthMethod);
}
}
}
@@ -10,6 +10,10 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route {
@service notify;
@service intl;
// =attributes
routeAfterAuthentication = 'scopes.scope.projects';
// =methods
/**
@@ -52,7 +56,7 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route {
requestCookies,
{ scope, authMethod }
);
this.transitionTo('scopes.scope.projects');
this.transitionTo(this.routeAfterAuthentication);
} catch (e) {
const errorMessage = this.intl.t('errors.titles.authentication-failed');
this.notify.error(errorMessage, { closeAfter: null });
@@ -0,0 +1 @@
{{outlet}}
@@ -5,7 +5,7 @@ import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import a11yAudit from 'ember-a11y-testing/test-support/audit';
import {
currentSession,
//authenticateSession,
authenticateSession,
//invalidateSession,
} from 'ember-simple-auth/test-support';
@@ -18,6 +18,7 @@ module('Acceptance | authentication', function(hooks) {
let scope;
let authMethod;
let authMethodID;
let authenticateURL;
let authMethodAuthenticateURL;
let projectsURL;
@@ -27,10 +28,18 @@ module('Acceptance | authentication', function(hooks) {
authMethod = this.server.create('auth-method', { scope });
orgScopeID = orgScope.id;
authMethodID = authMethod.id;
authenticateURL = `/scopes/${orgScopeID}/authenticate`;
authMethodAuthenticateURL = `/scopes/${orgScopeID}/authenticate/${authMethodID}`;
projectsURL = `/scopes/${orgScopeID}/projects`;
});
test('visiting auth methods authenticate route redirects to first auth method', async function(assert) {
assert.expect(1);
await visit(authenticateURL);
await a11yAudit();
assert.equal(currentURL(), authMethodAuthenticateURL);
});
test('visiting auth method authenticate route', async function(assert) {
assert.expect(1);
await visit(authMethodAuthenticateURL);
@@ -38,6 +47,15 @@ module('Acceptance | authentication', function(hooks) {
assert.equal(currentURL(), authMethodAuthenticateURL);
});
test('visiting any authenticate route while already authenticated redirects to projects', async function(assert) {
assert.expect(2);
authenticateSession();
await visit(authenticateURL);
assert.equal(currentURL(), projectsURL);
await visit(authMethodAuthenticateURL);
assert.equal(currentURL(), projectsURL);
});
test('failed authentication shows a notification message', async function(assert) {
assert.expect(3);
await visit(authMethodAuthenticateURL);
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/authenticate/index', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:scopes/scope/authenticate/index');
assert.ok(route);
});
});