I have an Angular 2 service that executes a few steps to authenticate and log in an app user. Any time I try to call next() on my Observer I get an error that it is undefined. The only place I can successfully call next() on it is inside contructor when the Observable is instantiated.
If I call authenticateUser() I get an error that this.isLoggedIn is undefined.
AuthService.ts
public isLoggedIn$: Observable<boolean>;
private isLoggedIn: Observer<boolean>;
constructor(...) {
this.isLoggedIn$ = new Observable<boolean>(
(observer: Observer<boolean>) => {
this.isLoggedIn = observer;
// this works fine
this.doLogin();
}).share()
}
private doLogin = ():void => {
let context:AuthContextModel = this.authContextService.getAuthContext();
if (context) {
let isAuthenticated = this.isAuthenticated(context);
if (isAuthenticated) {
this.doCreateCurrentUserContext(context)
.then((result) => {return this.doNotifyLoggedInStatus(result);});
}
}
};
private doNotifyLoggedInStatus = (result:boolean):Promise<boolean> => {
this.isLoggedIn.next(result);
return new Promise((resolve, reject) => {
return resolve(true);
});
};
public authenticateUser = (user: string, pass: string):Promise<boolean> => {
return this.doFetchToken(user, pass)
.then((fetchTokenData) => {return this.doStoreToken(fetchTokenData);})
.then((authContext) => {return this.doCreateCurrentUserContext(authContext);})
.then((result) => {return this.doNotifyLoggedInStatus(result);});
};