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

Idea for ESA tips blog post #190

Open
geekygrappler opened this issue Apr 16, 2018 · 2 comments
Open

Idea for ESA tips blog post #190

geekygrappler opened this issue Apr 16, 2018 · 2 comments

Comments

@geekygrappler
Copy link
Contributor

geekygrappler commented Apr 16, 2018

This is from my experience of setting up ESA at the weekend. It could be that my solutions are not the best, or that the problems are covered in the docs and I just didn't read thoroughly enough.

Setting up log in redirects

import Route from '@ember/routing/route';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

export default Route.extend(ApplicationRouteMixin, {
  routeAfterAuthentication: 'dashboard'
});

OR

// app/instance-initializers/session-events.js
export function initialize(instance) {
  const applicationRoute = instance.container.lookup('route:application');
  const session          = instance.container.lookup('service:session');
  session.on('authenticationSucceeded', function() {
    applicationRoute.transitionTo('index');
  });
  session.on('invalidationSucceeded', function() {
    applicationRoute.transitionTo('bye');
  });
};

export default {
  initialize,
  name:  'session-events',
  after: 'ember-simple-auth'
};

But the latter causes issues in the test, it’s not up to date. Far simpler to use the mixin and override that property.

The already authenticated redirect

import Route from '@ember/routing/route';
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';

export default Route.extend(UnauthenticatedRouteMixin, {
  routeIfAlreadyAuthenticated: 'dashboard'
});

Testing - mock already logged in for your protected routes

this.owner.lookup('service:session').set('isAuthenticated', true);

Easy when you know it.

Testing - respond to login correctly in test

     import { mock } from 'ember-data-factory-guy';

    test('after successful login the user is redirected to the dashboard', async function(assert) {
      mock({
        type: 'POST',
        url: '/token',
        responseText: { access_token: 'rubbish' }
      });

      await visit('/login');

      await click('[data-test-login-button]');

      assert.equal(currentURL(), '/dashboard', 'The user is redirected to dashboard after login');
    });
@geekygrappler
Copy link
Contributor Author

I feel like there may be more as I play with it some more.

@marcoow
Copy link
Member

marcoow commented Apr 16, 2018

I guess we could have an "ESA best practices" series or so. Some of these things probably indicate a lack of documentation though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants