I'm having problem getting a simple http call to work.
I came from angularjs and AFAIK .then = .subscribe
So Here's my problem, Can I not use the .subscribe in my auth-service and instead use it in my login.component.ts and I want to get the Http Status also.
auth.service
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}
export class AuthService {
constructor(private http: HttpClient) { }
mainUrl = 'http://localhost:4000/api/v1';
signIn(user: User) { // this is working fine, BUT I want to use .subscribe in login component
const url = `${this.mainUrl}/sign_in`;
return this.http.post<any>(url, user, httpOptions)
.subscribe(
(data) => {
console.log(data); // I want to get the 200 status not only the jwt token
return data;
},
(error) => console.log(error)
)
}
}
login.component.ts
this.authService.signIn(userData); // how to use .subscribe here ?
UPDATE:
Here's what my angularjs code look like.
auth.service
function signIn(data) {
return $http.post(url + '/api/v1/sign_in_2', data)
.then(success)
.catch(fail);
function success(response) {
setToken(response.data.jwt);
return response;
}
function fail(e) {
return e;
}
}
login.ctrl
vm.doLogin = function (userData) {
vm.spinner = true;
elixirAuthServices.signIn(userData).then(function (data) {
if (data.status === 200 || data.status === 201) {
elixirAuthServices.getUser().then(function (data) {
vm.user_id = data.data.id;
redirect();
});
} else {
vm.authenticateErr = true;
vm.spinner = false;
logger.info('Username or Password is incorrect');
}
});
};