title: After php validation of user login, how do I record and use that information in Angular?
body: <p>After watching and reading tons of conflicting tutorials about login and registration, I have a working system that creates users or logs them in after verifying their details. It returns a userID to the front end which I store in my api service (the one that makes all the calls to php backend).</p>
  
  <p>I'm storing the logged in user in PHP session variables to make sure only the right user is performing actions, but I would also like to keep track of the logged in user on the frontend. The problem is that every time the page reloads, the api service loses the data about the logged in user.</p>
  
  <p>Here's what my login call looks like from a login component ts file:</p>
  
  <pre><code>    var response: any = this.api.loginUser(email, password).subscribe(res =&gt; {
    this.spinner.spinnerOff();
    console.log(res);
    if (res.success) {
      this.api.setUser(res.success);
      this.router.navigate(["/home"]);
    } else this.formError = true;
  });
  </code></pre>
  
  <p>This works fine and I can use the value of api.userID (which is returned in res.success) to alter the "login/register" menu to say "welcome!" instead, but on page refresh, it's gone. Not only that, but I would think I need to do a new check on each route to make sure whether they're logged in anyway.</p>
  
  <p>Do I need to send back logged in user data on every component load or is there some other way to manage user sessions? More importantly, how can that be done in a secure way? I would have to be backend-driven wouldn't it?</p>