Reputation: 617
It's logged out after refresh page , how can i keep the user logging in after reloading ?
This is header.component.html:
<li class="nav-item dropdown-menu-left" ngbDropdown dir="rtl">
<a href="javascript:void(0)" class="nav-link" ngbDropdownToggle> John Smith </a>
<div class="dropdown-menu dropdown-menu-left">
<a class="dropdown-item" (click)="onLoggedout()">logout</a>
</div>
</li>
this is logout function :
onLoggedout() {
this.authService.logout();
this.router.navigate(['/login'])
}
This is authService:
logout(){
firebase.auth().signOut();
this.token = null;
}
This is auth-guard.ts:
constructor(private router: Router, private authService:AuthService) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.authService.isAuthenticated();
}
Upvotes: 2
Views: 6527
Reputation: 86800
I understand your problem, you need to follow these steps:
Firstyl when you login you need to store your token
in the localstorage.
(i am assuming token
keyword as flag that user is login)
once you logout clear your localstorage using
localStorage.clear();
Than as your case once you press back button
of browser user will not allowed to access page before login, for this you need to make changes in your authGuard file as follow
constructor(private router: Router, private authService:AuthService) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
if(localStorage.get('token'))
return true;
else
return false;
}
if still need any help let me know.
Upvotes: 4
Reputation: 316
Save your session in cookies and check each time if the cookie of the session exist. This library may help you to manage cookies easily Angular2-cookie.
Upvotes: 0
Reputation: 49
I don't know,it's you expect answer.But you can try to save token to localStorage.
Upvotes: 0