Hello,
I’m trying to authenticate my app against a REST API (Drupal 7 Service module). With a REST client it looks like this:
Host: http://example.com/api/user/login
Header:
content-type: application/json
content: {"username":"test","password":"test"}
Response:
Header:
Set-Cookie: SESSxxx=xxxxx; expires=Thu, 02-Apr-2015 14:16:35 GMT; Max-Age=2000000; path=/; domain=.example.com; HttpOnly
Content:
{"sessid":"xxxx","session_name":"SESSxxx","token":"xxxxxx","user":{"uid":"8","name":"test","mail":"test@example.com"}}
For each further request I just have to set the Cookie Header:
Cookie: session_name=sessid
I thought this must be super simple but I’m totally lost. You need to know this is my first ember app.
What works:
- Send the login request and return the session data
- Showing Login/Logout buttons depending on what simple-auth thinks my status is
What does not work:
- Setting the Cookie header for each subsequent request after successfull authentication (tested with the logout request which also just needs the cookie header)
- In Ember Inspector I see an indefnite loop of popping
<unknown Promise>
with the authentication requests response as “Fullfillment/Rejection value”
My Code:
app/authenticators/custom.js:
import Ember from 'ember';
import Base from 'simple-auth/authenticators/base';
export default Base.extend({
restore: function(data) {
return new Ember.RSVP.Promise(function (resolve, reject) {
if (!Ember.isEmpty(data.session_name)) {
resolve(data);
}
else {
reject();
}
});
},
authenticate: function(options) {
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.$.ajax({
type: "POST",
url: 'http://example.com/api/user/login',
contentType: 'application/json;charset=utf-8',
dataType: 'json',
data: JSON.stringify({
username: options.identification,
password: options.password
})
}).then(function(response) {
Ember.run(function() {
resolve(response);
});
}, function(xhr, status, error) {
Ember.run(function() {
reject(xhr.responseJSON || xhr.responseText);
});
});
});
},
invalidate: function() {
console.log('invalidate...');
Ember.$.ajax({
type: "POST",
url: 'http://example.com/api/user/logout',
contentType: 'application/json;charset=utf-8',
dataType: 'json',
});
return Ember.RSVP.resolve();
}
});
app/authorizers/custom.js
import Base from 'simple-auth/authorizers/base';
export default Base.extend({
authorize: function(jqXHR, requestOptions) {
console.log('authorize...');
jqXHR.setRequestHeader('Cookie', '1234lkj1sdfguihsidfugh');
}
});
I guess the authorizer is the point where I have to create the Cookie header. But there are two major problems:
- jqXHR.setRequestHeader does nothing
- I can’t access the stored(?) session information. I tried
import Session from 'simple-auth/session'
and thenconsole.log(Session.store);
but nothing.
Sorry if the solution might be too obvious. I alread read dozens of tutorials (each looks different) and a lot of documentation but I can’t get my head around this.
Every help is much appreciated, thank you.
Regards, haggis