Open Closed

Clearing Session Storage on logout in Angular #5841


User avatar
0
SamirBoutazzout created

Hey guys, i'm trying to fix an issue I have on the frontend. I would like to clear the sessionStorage from an item I set during the session of a user. I've been looking around on the documentation and google but I couldn't find an answer. Is there a hook or a service exposed in Angular where I can clear the session on logout? Basically, I want to clear the sessionStorage when the user logs out. The issue i'm having is when I logout with an account, and log in with another, the items from the previous session are persisted and this is causing an issue.

Thank you

  • ABP Framework version: v6.0.2 Commercial
  • UI Type: Angular
  • Database System: EF Core (PostgreSQL)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

2 Answer(s)
  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Hello SamirBoutazzout, Can you please try this. Try to add this code in app.component.ts file.

    import { AuthService } from '@abp/ng.core';
    import { Router } from '@angular/router';
    import { OAuthService } from 'angular-oauth2-oidc';
    import { filter, tap } from 'rxjs';
    constructor(private oAuthService: OAuthService , private router: Router, private authService: AuthService) {
    this.oAuthService.events
        .pipe(filter(event => event?.type === 'logout'),
          tap(() => {
            sessionStorage.clear();
          })).subscribe();
      }
    }
    
  • User Avatar
    0
    SamirBoutazzout created

    Hi Anjali_Musmade,

    Thank you very much. That worked like a charm. I appreciate it.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35