samedi 27 juin 2015

How to store the user id in session with ember-simple-auth?

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?




Aucun commentaire:

Enregistrer un commentaire