2

So I have successfully implemented Ember-Auth using a pure token based approach. I would like to redirect my user to the root of my app once they sign in.

I know I can use actionRedirectable (http://ember-auth.herokuapp.com/docs in the docs) but since I am using a pure token approach and not storing anything in cookies I am effectively signing my user in again every time the page refreshes using a remember_token (which seems unideal but I'll work it out shortly). This means that using actionRedireactable would mean that I would be redirecting every time the user refreshes the page. Perhaps there is an anti-pattern in there somewhere?

Anyway here is my SignInView:

App.SignInView = Ember.View.extend({

  templateName: 'auth/sign_in',

  email:    null,
  password: null,

  submit: function(event, view) {
    event.preventDefault();
    event.stopPropagation();

    App.Auth.signIn({
      data: {
        email:    this.get('email'),
        password: this.get('password')
      }
    });
  }
});

If I call this.get("controller").transitionToRoute('...') directly after the signIn call then my user invariably isn't signed in by this point so they get redirected to the login page again. And if I try:

App.Auth.on('signInSuccess', function() {
  // ...
});

then I don't have any sensible way to access the router to do a transition. Any bright ideas would be greatly appreciated. Thanks!

2 Answers 2

2

As a best practice you should not have logic in your view, logic is much better suited to live in controllers, so for your use case, create a App.SignInController an instrument there your authentication process:

View

App.SignInView = Ember.View.extend({
  templateName: 'auth/sign_in',
  email:    null,
  password: null,

  submit: function(event, view) {
    event.preventDefault();
    event.stopPropagation();

    var data = {
        email:    this.get('email'),
        password: this.get('password')
    }
    // forward the action to your controller passing along the
    // data object your sign in process needs
    this.get("controller").send("signIn", data);
  }
});

Furthermore, you should not transitionTo from elsewhere other than inside the router. By doing so, you could run into serious issues because you don't know in which state your router actually is. So the best thing to do is to get a reference to your router and call the transitionTo on the router instead:

Controller

App.SignInController = Ember.ObjectController.extend({
  signIn: function(data) {

    // grab your passed data object and issues you sign in
    App.Auth.signIn({
      data: data
    });
    
    // subscribe to the `signInSuccess` event and 
    // then transition to your route but using 
    // the router itself
    App.Auth.one('signInSuccess', function() {
      var router = this.get('target.router');
      router.transitionTo('route_name');
    });

  }
});

Hope this helps.

0

I don't have tested, but I think that this works:

App.SignInView = Ember.View.extend({

  templateName: 'auth/sign_in',

  email:    null,
  password: null,

  submit: function(event, view) {
    event.preventDefault();
    event.stopPropagation();
    var controller = this.get('controller');

    App.Auth.signIn({
      data: {
        email:    this.get('email'),
        password: this.get('password')
      }
    });

    App.Auth.one('signInSuccess', function() {
      controller.transitionToRoute('...');
    });

  }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.