Rtype
Rtype

Reputation: 915

Ember-auth, ember and ajax authentication: accessing ember auth-token

Im using Ember-Auth and have login to my rails application working.

Once logged in the auth.auth-token is set and i can view it in my templates with:

{{auth.authToken}}

I no need to pass this token along with my ember-data requests but for the life of me cant figure out how to access it.

App.Store = DS.Store.extend
  adapter: DS.RESTAdapter.reopen
    namespace: 'api/v1'
    setHeaders: (->
      this.set('headers', { "auth_token": <what goes here?>  });
      return
    ).on('init');

some of the things I've tried:

App.Session.get("authToken")
App.auth_token
App.auth.auth_token
App.Auth.get('auth_token')
App.Auth.get('authToken')

There seem to be lots of people doing it different ways, due to embers rapidly changing API. But everything I've tried ends up being undefined.

DEBUG: -------------------------------
DEBUG: Ember      : 1.5.0-beta.3
DEBUG: Ember Data : 1.0.0-beta.7.f87cba88
DEBUG: Handlebars : 1.3.0
DEBUG: jQuery     : 1.11.0
DEBUG: ------------------------------- 

Auth configuration:

App.Auth = Ember.Auth.extend
  request: 'jquery'
  response: 'json'
  strategy: 'token'
  session: 'cookie'
  signInEndPoint: '/api/v1/user_session' # api url for sign-in
  signOutEndPoint: '/api/v1/user_session' # api url for sign-out

  tokenKey: 'auth_token' # param returned from api containing auth token
  tokenIdKey: 'user_id' # param returned from api for id of authenticated user
  tokenLocation: 'param' # auth token will be sent to api as a param

Upvotes: 0

Views: 369

Answers (1)

Rtype
Rtype

Reputation: 915

Well the answer to this I believe was that my authentication server wasn't returning the token with the correct name in my json response.

I switched to ember-simple-auth since having this issue, but had a similar issue until I discovered that the name of my auth token in the json my server was returning was: "auth_token" rather that what ember-simple-auth expected: "access_token"

So my guess is that the auth was seeing that the request succeeded, which explains why I could login. but then the session was empty because ember-simple-auth was looking for a token of a different name and failing that didn't bother to create the session.

Perhaps a suggestion to the ember-auth and ember-simple-auth people is to add a warning or error when the returned token is missing?

Hope this helps someone else :)

Upvotes: 1

Related Questions