0

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?

auth.service.ts

auth.gaurd.ts

login.component

Anony
  • 23
  • 1
  • 3
  • 1
    you 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
  • 1
    Please 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 Answers1

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