Search code examples
ember.jsember-simple-auth

make custom API call with authentication


I am trying to develop an application that should eventually replace an existing (non-Ember) one and provide additional functionality.

For a start, for anything not yet implemented in the new app, I want to redirect users to the existing one, using the latter's single-sign-on capability. The workflow I imagined is this:

  1. User logged in to new (Ember) app clicks link or button there
  2. New app makes an API call to an endpoint that returns an SSO token
  3. New app generated link including SSO token, opens it (in new or same window)

I use ember-simple-auth to authenticate the user for API calls that return user-specific information, using a JSON web token that contains the user id.

For step 2 above I would need to include that token in the API call, but I am at loss how, and even where to implement the call. Do I need an Ember.Route for this (where I could throw in the AuthenticatedRouteMixin)? I would not consider the SSO token to be part of my model, so that does not seem right. Can I get the session's token somehow and include it in a direct ajax call? Should I?


Solution

  • ember-simple-auth provides the SessionService where you can access that information.

    My recommendation is to use ember-ajax to make the actual request, and override the ajax service to call the session services authorize method.

    Then you need to implement your authorizer to authorize that request.

    The detail implementation depends on your authorizer and how you want to include the token in your request. As header, query param, or in the body.