Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(unauthenticated-route-mixin): Move redirection logic for thi… #1666

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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), () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not something that needs to be fixed before this can be merged (it's in the other PR as well) but that we should clean up at some point: this variable doesn't really indicate whether there was a redirect in the callback but whether the session was authenticated or not - not a big deal but this makes following what's going on unnecessarily complicated.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great point. A tuple might be appropriate for a future PR

const [didRedirect, reason] = runIfAuthenticated(...);

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
14 changes: 10 additions & 4 deletions tests/unit/mixins/unauthenticated-route-mixin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ 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 containerMock;

describe('#beforeModel', function() {
beforeEach(function() {
Expand All @@ -19,15 +21,19 @@ describe('UnauthenticatedRouteMixin', () => {
return RSVP.resolve('upstreamReturnValue');
}
});

session = InternalSession.create({ store: EphemeralStore.create() });
containerMock = {
lookup: sinon.stub()
};

containerMock.lookup.withArgs('service:session').returns(session);

route = Route.extend(MixinImplementingBeforeModel, UnauthenticatedRouteMixin, {
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