Search code examples
spartacus-storefront

SAP Spartacus - How to find wheter the user is logged in or not


I just want to show one div if the user is logged in already or else I want to show another div with different content. Can anyone guide this to achieve this?


Solution

  • You'd use the authService.isUserLoggedIn() method, that exposes an observable.

    You could use it like this:

    <div *ngIf="isLoggedIn;else #differentContent">
      User is logged in
    </div>
    
    <ng-template #differentContent>
      something else
    </ng-template>
    

    In your component controller you'd addd the isLoggedIn property:

    @Component({...})
    export class MyComponent {
      isLoggedIn: Observable<boolean> = this.authService.isUserLoggedIn();
      constructor(private authService: AuthService) {}
    }