Reputation: 2408
I have a private method which i use for authentication. Then i have another method (public) that i call in my components called login
to do the actual login. I want to be able to subscribe to the login
method which in reality subscribe's to the private authentication
method so that i can show loading and other messages while login in being performed differently for each view. Is it possible?
Authentication method:
private userAuthenticate( email: string, password: string ) {
return this.httpPost(`${this.baseApiUrl}/auth?format=json&provider=login`, {userName: email, password: password}).subscribe(
res => this.saveJwt(res.bearerToken),
err => this.logError(err),
() => console.log("Authentication done.")
);
}
Login method:
login( email: string, password: string ) {
this.logout();
return this.userAuthenticate(email, password);
}
I want to subscribe to the login method so that i can control my loaders and error messages etc. Please help.
Upvotes: 1
Views: 5664
Reputation: 658077
You can't subscribe to Subscription
(returned by subscribe()
). You can only subscribe to Observable
.
To get an Observable
use map()
instead of subscribe()
. Then the caller of login()
can do the subscription.
private userAuthenticate( email: string, password: string ) {
return this.httpPost(
`${this.baseApiUrl}/auth?format=json&provider=login`,
{userName: email, password: password}
)
.map(res => this.saveJwt(res.bearerToken));
}
For the other callabacks, if your really need them, you can use the catch
and finally
operators.
Upvotes: 5