1

I want to store the user id in the session. I have this authenticate method in a custom authenticator :

authenticate: (credentials) ->
  _this = this
  new (Ember.RSVP.Promise) (resolve, reject) ->
    Ember.$.ajax(
      url: 'http://localhost:3000/api/v1/sessions'
      type: 'POST'
      data: session:
        email: credentials.identification
        password: credentials.password
    ).then ((response) ->
      _this.set('user_id', response.user_id)
      Ember.run ->
        resolve token: response.token
    ), (xhr, status, error) ->
      response = JSON.parse(xhr.responseText)
      Ember.run ->
        reject response.error

It's coffee script but it works like javascript. As you can this, I set 'user_id' in the session.

In app/initializers/custom-session, I have this :

import Ember from "ember";
import Session from "simple-auth/session";

export default {
  name: "current-user",
  before: "simple-auth",
  initialize: function(container) {
    Session.reopen({
      setCurrentUser: function() {
        return this.get('user_id')
      }.observes('secure.access_token')
    });
  }
};

user_id always returns undefined. I found many solutions on the Internet but nothing works.

Is there a simple solution to do that?

Dougui
  • 7,142
  • 7
  • 52
  • 87

1 Answers1

3

You are setting the user_id on the authenticator (_this.set('user_id', response.user_id)) instead of the session. The user_id should be passed to the resolve method in your authenticator. That way your user id is accessible by secure.user_id in your session.

jcbvm
  • 1,640
  • 1
  • 15
  • 22
  • I added a `then` callback to my authentication and it works. The only think I don't like is than the user id is gave by post request I made in the authenticator. I don't have access to it in my callback method. Do you know if it's possible to have information without to do a second request? – Dougui Jun 29 '15 at 11:35
  • @Dougui What do you mean by callback method? Can you give an example? – jcbvm Jun 29 '15 at 18:00