I'm new to angular and made an app with login logout feature. After login when I refresh the browser, application automatically logs out. I can't understand how to use session. Also is there anyway my app automatically logs out after particular time if there's no activity?
Asked
Active
Viewed 3,650 times
0
-
1you have to save your session token in either `cookies` or `localStorage`. and use it to determine if your session has expired or not – John Velasquez Mar 05 '19 at 17:01
-
1Please share a minimal reproducible code snippet for others to check. A screenshot or link to external code repo is not acceptable. – nircraft Mar 05 '19 at 17:34
1 Answers
2
You need to save the session somewhere, cookie or localStorage for example
I can't understand how to use session
here you're
// auth.service.ts
private loggedIn = new BehaviorSubject<boolean>(localStorage.getItem("isLoggedIn") === "true");
login (user: User) {
if (user.userName === "admin" && user.password === "admin") {
localStorage.setItem("isLoggedIn", "true");
localStorage.setItem("token", "add some unique token here");
this.loggedIn.next(true);
this.router.navigate(["/home"]);
}
}
Also is there anyway my app automatically logs out after particular time if there's no activity?
yes there's a way, check this for example
Sidenote the code above is just an example please don't use it in production.
update here's a working example

Ahmed Kamal
- 2,660
- 3
- 21
- 36
-
-
-
-
@nipun You can use `*ngIf` attribute for example `
`, also I recommend you to take a look at the documentation [here](https://angular.io/docs) – Ahmed Kamal Mar 06 '19 at 13:55 -
I know it might be getting frustrated for you but still I'm getting the issue while refreshing the page, my toolbar disappears. Any help? – Anony Mar 07 '19 at 06:22
-