Reputation: 600
I have the following code to authenticate a user using Ionic 2, angularjs and devise_token_auth, but if successful login, I receive the access-token in the header of the response and not in json.
authenticate(user) {
var creds = "email=" + user.name + "&password=" + user.password;
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
return new Promise(resolve => {
this.http.post('http://localhost:3000/auth/sign_in', creds, {headers: headers}).subscribe(data => {
if(data.json().data){
this.storeUserCredentials("MY TOKEN IN THE HEADER");
resolve(true);
}
else
resolve(false);
});
});
}
But I'm beginning to study AngularJS and ionic. I do not know to access the access-token in the header
Upvotes: 1
Views: 3082
Reputation: 93
$scope.token=respone.headers('Authorization')
this line will return the token from the response.
Upvotes: 1
Reputation: 3964
This is how , we can access a response header from $http response.
$http.post('/api').then(function(response) {
//Accessing header
console.log(response.headers()['access-token']);
});
Your code is a bit different. You can try this.
this.http.post('http://localhost:3000/auth/sign_in', creds, {headers: headers}).subscribe(data => {
if(data.json().data){
this.storeUserCredentials(data.json().headers()['access-token']);
resolve(true);
}
else
resolve(false);
});
Upvotes: 1