3
votes

I am new to ember. I currently use Ember 1.5 and Ember Data 1.0.0-beta.7+canary.b45e23ba and ember-simple-auth 0.2.1.

I am struggling with a scenario where I want to do two custom calls to an API for login. Here is what the code I have:

App.ApplicationAdapter = DS.RESTAdapter.reopen
    host: '//localhost:8443'
    namespace: 'api/v1'

App.store = DS.Store.create
    adapter: App.ApplicationAdapter

App.User = DS.Model.extend()

Ember.Application.initializer
    name: 'authentication'
    initialize: (container, application) ->
        container.register('authenticators:custom', App.Authenticator);
        Ember.SimpleAuth.setup(container, application)


App.Authenticator = Ember.SimpleAuth.Authenticators.OAuth2.extend
    authenticate: (credentials) ->
        return new Ember.RSVP.Promise (resolve, reject) ->
            Ember.$.ajax
                url:  '//localhost:8443/api/v1/login'
                type: 'POST'
                data: { email: credentials.identification, password: credentials.password }
            .then (response) ->
                token = response.token
                userId = response.userId

                # Add authorization header
                App.ApplicationAdapter.reopen
                    headers: 
                        Authorization: 'Bearer '+response.access_token

                # Load the user data
                App.store
                .find('user', response.userId)
                .then (response) ->
                    Ember.run ->
                        resolve({ access_token: token, account_id: userId })
                , (xhr, status, error) ->
                    Ember.run ->
                        reject(xhr.responseText)

            , (xhr, status, error) ->
                Ember.run ->
                    reject(xhr.responseText)

1/ The code above will fail when it reaches the App.store.find('user', response.userId). Throwing:

Uncaught TypeError: Cannot read property 'normalize' of undefined ember-data.js:9806
Ember.Object.extend.modelFor ember-data.js:9806
Ember.Object.extend.findById ember-data.js:9098
Ember.Object.extend.find ember-data.js:9085
(anonymous function) initializers.coffee:30
(anonymous function) jquery.js:3206
fire jquery.js:3049
self.fireWith jquery.js:3161
done jquery.js:8185
(anonymous function) jquery.js:8532

2/ I am a little confused with what is required to get a Store with a RESTAdapter with Ember Data 1.0. Apparently the API changed and I don't know if my App.store is the proper way to instantiate a store.

Thanks for your help!

1
Not sure about the Ember Data stuff but you don't need to add the Bearer Token header manually to the adapter - Ember.SimpleAuth does that automatically. - marcoow
Having this exact issue, without the authenticator. OP, did you ever find a solution? - Ruben Martinez Jr.
Unfortunately not. I was giving Ember a spin for fun and didn't dig this deeper. - karellm

1 Answers

0
votes

replace your find with this:

container.lookup('store:main').find('user', response.userId)