1
votes

I'm trying to check if an user is authenticated, and I'm using a method that returns an observable of either true or false if the user is authenticated or not. What I'm trying to do is the wait until authorized has a value, then check if it's true or false in the callback function and depending on it's value perform an action. So basically I don't want my filter function to check if the value is true or false, but to check if there is a value before continuing with the code inside the subscription callback.

This is my function:

this.oidcSecurityService.getIsAuthorized().pipe(filter(authorized => authorized !== undefined)).subscribe(authorized => {
  console.log(authorized);
  this.isAuthorized = authorized;
  if (!authorized) {
    debugger
    this.login()
  }
  else {
    debugger
    this.getBoSpar()
  }
});

What am I doing wrong, and how can I make the filter function check if authorized is fetched, and not if it's either true or false? I'm not getting any errors, it's just that authorized is always evaluated to false, even though I know that the user is authenticated (I've separating the logic to happen on click events, and that works).

1
I don't see anything immediately wrong with your code or your logic. Can you edit your question to include the code for getIsAuthorized? - Vlad274

1 Answers

0
votes

authorized !== undefined looks wrong

authorized != null checks for null and undefined only

this.oidcSecurityService.getIsAuthorized()
  .pipe(
    tap(authorized => console.log('BEFORE', authorized)),
    filter(authorized => authorized != null),
    tap(authorized => console.log('AFTER', authorized))
  )
  .subscribe(authorized => {
    console.log(authorized);
    this.isAuthorized = authorized;
    if (!authorized) {
      debugger
      this.login()
    } else {
      debugger
      this.getBoSpar()
    }
});

Worth using tap also to check the values coming through the pipe, before and after the filter.