1

I am trying to have a user authenticated from code. I have a login form where users can login, and it works like charm.

However, when a new user sign up, and saves the form I would like to login them in in background when the form is valid.

How can I do that?

I found the following in the API at the authenticate() method:

For an application that works without an authentication route (e.g. because it opens a new window to handle authentication there), this is the method to override, e.g.:

App.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin, {
  actions: {
    authenticateSession: function() {
      this.get('session').authenticate('app:authenticators:custom', {});
    }
  }
});

Do anyone know how to implement this. When I call this.get('session').authenticate() where to put the identication and password data?

Any hints or suggestions are highly appreciated!

Edit: maybe it is possible to use the same authenticator used for logging in instead of app:authenticators:custom as in the example?

1 Answer 1

5
+50

To authenticate the session either via a regular login (probably with EmberSimpleAuth's default authenticator) or automatically after successful signup, you'd use a different authenticator for each case. For the login you'd use the default authenticator with the LoginControllerMixin etc. and for the automatic case you'd use a custom authenticator. For docs on using custom authenticators see the examples in EmberSimpleAuth's github repo an the API docs: http://ember-simple-auth.simplabs.com/api.html.

Basically what you'd do is:

App.AutoAuthenticator = Ember.SimpleAuth.Authenticators.OAuth2.extend({
  authenticate: function(credentials) {
    if (!Ember.isEmpty(credentials.access_token)) {
      return Ember.RSVP.resolve(credentials);
    } else {
      return this._super(credentials);
    }
  }
});
Ember.Application.initializer({
  name: 'authentication',
  initialize: function(container, application) {
    container.register('app:authenticators:custom', App.AutoAuthenticator);
    Ember.SimpleAuth.setup(container, application);
  }
});
this.get('session').authenticate('app:auto-authenticator', { access_token: 'secret token!' })

I did not actually test that - please regard this as pseudo code ;)

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the comment! This is easy to follow. One question left, do you also know where I put the App.AutoAuthenticator method in Ember App Kit? What folder do I need to put the that code?
Hm, not sure which folders Ember App Kit defines - I'd say a good place would be sth. like "lib" or so.
Again thanks for your comment! However can you please test this code and edit it to a working version? The community will be very happy! Bounty 's coming! ;-)
maybe someone else can do that for you - I'm busy working towards the 1.0 release of Ember.SimpleAuth...
Haha, okay we should do it with this answer. You need to know that we are so exited about SimpleAuth! Bounty granted!
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.