Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic HTTP Authentication in Ember Data REST Adapter

Is there a way in Ember.js (and Ember-data) to send credentials to an api that requires Basic HTTP Authentication? I can see how it's done in JQuery here, but don't see any straightforward way to do it in Ember. I thought maybe adding something to the header would work (see below in coffeescript), but no success:

App.AuthAdapter =  DS.RESTAdapter.extend(
    host: 'https://my-api.example.com'
    namespace: 'v1'
    headers:
        "Authorization Basic fooUsername:barPassword"
    ...
like image 587
bk11425 Avatar asked Nov 15 '13 00:11

bk11425


People also ask

What is adapter in Ember?

In Ember Data, an Adapter determines how data is persisted to a backend data store. Things such as the backend host, URL format and headers used to talk to a REST API can all be configured in an adapter. Ember Data's default Adapter has some built-in assumptions about how a REST API should look.

How do I use Ajax in Ember JS?

Basic Usage In fact, ember-ajax is a wrapper around jQuery's method, and can be configured in much the same way. In general, you will use the request(url, options) method, where url is the destination of the request and options is a configuration hash for jQuery. ajax . import Ember from 'ember'; export default Ember.

What is Ember mirage?

Ember CLI Mirage is a client side mock server to develop and prototype applications. It uses a library called. Pretender in the background to make this possible. At some point while learning Ember. js you probably have needed to mock some data from your server.

What is service in Ember?

A Service is an Ember object that lives for the duration of the application, and can be made available in different parts of your application. Services are useful for features that require shared state or persistent connections. Example uses of services might include: User/session authentication. Geolocation.


2 Answers

You can extend the default Rest adapter and add a headers hash which will be included in the ajax that's sent.

App.ApplicationAdapter = DS.RESTAdapter.extend(
   headers:
     withCredentials: true
     Authorization: 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='
)

Or you could take it a step farther and override the ajax method

App.ApplicationAdapter = DS.RESTAdapter.extend(
  ajax: (url, type, hash) ->
    adapter = this
    new Ember.RSVP.Promise((resolve, reject) ->
      hash = hash or {}
      hash.url = url
      hash.type = type
      hash.dataType = "json"
      hash.context = adapter

      if hash.data and type isnt "GET"
        hash.contentType = "application/json; charset=utf-8"
        hash.data = JSON.stringify(hash.data)

      if adapter.headers isnt `undefined`
        headers = adapter.headers
        hash.beforeSend = (xhr) ->
          forEach.call Ember.keys(headers), (key) ->
            xhr.setRequestHeader key, headers[key]

    hash.success = (json) ->
      Ember.run null, resolve, json

    hash.error = (jqXHR, textStatus, errorThrown) ->
      Ember.run null, reject, adapter.ajaxError(jqXHR)

    Ember.$.ajax hash
  )
)
like image 152
Kingpin2k Avatar answered Sep 20 '22 16:09

Kingpin2k


Can you use $.ajaxPrefilter? e.g.

Ember.$.ajaxPrefilter (options) ->
  options.xhrFields = { withCredentials: true }
  options.username = 'fooUsername'
  options.password = 'barPassword'
  true # need to return non-falsy here
like image 24
gerry3 Avatar answered Sep 19 '22 16:09

gerry3