Using angular8
, I encountered an issue where logging in with USER1 credentials, closing the browser, and then attempting to log in with USER2 credentials still logged me in as USER1. While adding code to the app component resolved this problem, I faced an issue with local storage being cleared upon refreshing the browser, which was not ideal. How can I address this situation?
import { Component, HostListener } from "@angular/core";
@Component({ selector: 'app-root', templateUrl:"./app/app.component.html" })
export class AppComponent {
@HostListener("window:onbeforeunload",["$event"])
clearLocalStorage(event) {
localStorage.clear();
}
}
The solution involves storing user details and jwt token in local storage in order to maintain user login status between page refreshes.
login(email, password) {
return this.http.post<any>(`${Constant.apiUrl}account/login`, { email, password })
.pipe(map(user => {
localStorage.setItem('currentUser', JSON.stringify(user));
this.currentUserSubject.next(user);
return user;
}));
}