I’m doing some testing now, but I think this is going into version 2.0.0 of our @sigao/ng-auth0 library.
I abandoned the observable public properties on the service in favor of observable factory functions. Also found a pretty elegant way to ensure that the “isAuthenticated” stream is fresh both on initial subscription as well as able to accept pushed values from the handleAuthCallback response (which solves the initial problem).
the “_auth0ClientStream” variable is set up exactly like the docs with the “shareReplay” thing.
public getAuth0Client(): Observable<Auth0Client> {
return this._auth0ClientStream;
}
public getHandleRedirectCallback(): Observable<RedirectLoginResult> {
return this.getAuth0Client()
.pipe(concatMap((client: Auth0Client) => from(client.handleRedirectCallback())));
}
public getUserProfile(options?: GetUserOptions): Observable<any> {
return this.getAuth0Client().pipe(
concatMap((client: Auth0Client) => from(client.getUser(options))),
tap(user => this.userProfile = user)
);
}
public getIdTokenClaims(options?: GetIdTokenClaimsOptions): Observable<any> {
return this.getAuth0Client().pipe(
concatMap((client: Auth0Client) => from(client.getIdTokenClaims(options))),
tap(claims => this.idTokenClaims = claims)
);
}
public getTokenSilently(options?): Observable<string> {
return this.getAuth0Client().pipe(
concatMap((client: Auth0Client) => from(client.getTokenSilently(options))),
tap(token => this.token = token)
);
}
public getIsAuthenticated(): Observable<boolean> {
return this.refreshAuthentication()
.pipe(concatMap(() => this._isAuthenticatedStream));
}
private refreshAuthentication(): Observable<boolean> {
return this.getAuth0Client()
.pipe(
concatMap((client: Auth0Client) => from(client.isAuthenticated())),
tap(val => this._isAuthenticatedSubject.next(val))
);
}
Hopefully this helps any future users.