Skip to content

Commit

Permalink
refactor(unauthenticated-route-mixin): Move redirection logic for thi…
Browse files Browse the repository at this point in the history
…s mixin into a function

- simple the same thing as mainmatter#1655 does for authenticated-route-mixin
  • Loading branch information
Andy Brown committed Aug 30, 2018
1 parent a275c12 commit 8b8d494
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
20 changes: 17 additions & 3 deletions addon/mixins/unauthenticated-route-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import { assert } from '@ember/debug';
import { computed } from '@ember/object';
import Configuration from './../configuration';
import isFastBoot from 'ember-simple-auth/utils/is-fastboot';
import { getOwner } from '@ember/application';

/**
*
* @param {ApplicationInstance} owner The ApplicationInstance that owns the session service
* @param {(...args: [any]) => any} callback Callback that will be invoked if the user is authenticated
*/
function runIfAuthenticated(owner, callback) {
const sessionSvc = owner.lookup('service:session');
if (sessionSvc.get('isAuthenticated')) {
callback();
return true;
}
}

/**
__This mixin is used to make routes accessible only if the session is
Expand Down Expand Up @@ -62,16 +76,16 @@ export default Mixin.create({
`beforeModel` method is actually executed.
@method beforeModel
@param {Transition} transition The transition that lead to this route
@public
*/
beforeModel() {
if (this.get('session').get('isAuthenticated')) {
const didRedirect = runIfAuthenticated(getOwner(this), () => {
let routeIfAlreadyAuthenticated = this.get('routeIfAlreadyAuthenticated');
assert('The route configured as Configuration.routeIfAlreadyAuthenticated cannot implement the UnauthenticatedRouteMixin mixin as that leads to an infinite transitioning loop!', this.get('routeName') !== routeIfAlreadyAuthenticated);

this.transitionTo(routeIfAlreadyAuthenticated);
} else {
});
if (!didRedirect) {
return this._super(...arguments);
}
}
Expand Down
15 changes: 11 additions & 4 deletions tests/unit/mixins/unauthenticated-route-mixin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import sinon from 'sinon';
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';
import InternalSession from 'ember-simple-auth/internal-session';
import EphemeralStore from 'ember-simple-auth/session-stores/ephemeral';
import createWithContainer from '../../helpers/create-with-container';

describe('UnauthenticatedRouteMixin', () => {
let route;
let session;
let transition;
let containerMock;

describe('#beforeModel', function() {
beforeEach(function() {
Expand All @@ -20,18 +22,23 @@ describe('UnauthenticatedRouteMixin', () => {
return RSVP.resolve('upstreamReturnValue');
}
});

session = InternalSession.create({ store: EphemeralStore.create() });

transition = {
send() {}
};
containerMock = {
lookup: sinon.stub()
};

route = Route.extend(MixinImplementingBeforeModel, UnauthenticatedRouteMixin, {
containerMock.lookup.withArgs('service:session').returns(session);

route = createWithContainer(Route.extend(MixinImplementingBeforeModel, UnauthenticatedRouteMixin, {
// pretend this is never FastBoot
_isFastBoot: false,
// replace actual transitionTo as the router isn't set up etc.
transitionTo() {}
}).create({ session });
}), { session }, containerMock);

sinon.spy(route, 'transitionTo');
});

Expand Down

0 comments on commit 8b8d494

Please sign in to comment.