title: Get user info from a token that stored in Local storage angular + nodejs
body: <p>I using <code>bcrypt</code> and <code>jwt</code> for authentication in my angular app in the front and NodeJs in the back. I storing the token in the user local storage, Now i manage to store and receive the token from the local storage and authenticate the user after refresh, But i want also to get is info from the server, I tried several ways but  i am not sure what is the best way to achieve that </p>
  
  <p>My node route</p>
  
  <pre><code>router.post("/login", (req, res, next) =&gt; {
      let fetchedUser;
      User.findOne({ email: req.body.email })
        .then(user =&gt; {
          console.log(user)
          if (!user) {
            return res.status(401).json({
              message: "Auth failed"
            });
          }
          fetchedUser = user;
          return bcrypt.compare(req.body.password, user.password);
        })
        .then(result =&gt; {
          if (!result) {
            return res.status(401).json({
              message: "Auth failed"
            });
          }
          const token = jwt.sign(
            { email: fetchedUser.email, userId: fetchedUser._id },
            "this_is_the_secret",
              { expiresIn: "1h" }
          );
          res.status(200).json({
            token: token,
             expiresIn: 3600,
             isAdmin:fetchedUser.admin,
             user: fetchedUser
          });
        })
        .catch(err =&gt; {
          return res.status(401).json({
            message: "Auth failed"
          });
        });
    });
  </code></pre>
  
  <p>My service:</p>
  
  <pre><code>import { Injectable } from "@angular/core";
  import { HttpClient } from "@angular/common/http";
  import { Subject } from "rxjs";
  import { Router } from "@angular/router";
  import { SignUpData } from "../models/sign-up.model";
  import { LogInData } from "../models/log-in.model";
  
  
  @Injectable({ providedIn: "root"})
  export class AuthService {
      private isAuthenticated = false
      private token: string
      private authStatusListner = new Subject&lt;boolean&gt;()
      private tokenTimer: any
      private isAdmin = false
      private userInfo: any
  
      signupUrl = "http://localhost:3000/signup";
      loginUrl  = "http://localhost:3000/login"
  
      constructor(private http: HttpClient,
                  private router:Router){}
  
      getToken(){
          return this.token
      }
  
      checkIfAdmin(){
          return this.isAdmin
      }
  
      getUserInfo(){
          if(this.token){
              return this.userInfo
          }
      }
  
  
      getAuthStatus(){
          return this.isAuthenticated
  
      }
  
    getAuthStatusListner(){
        return this.authStatusListner.asObservable()
    }
  
  
      createUser(name: string, email: string, password:string){
          const authData: SignUpData = {name:name, email: email, password: password}
          this.http.post(this.signupUrl, authData)
           .subscribe(response =&gt; {
           });
      }
  
  
      login(email: string, password){
         const authData: LogInData = {email: email, password: password}
          this.http.post&lt;{token: string, expiresIn: number, isAdmin: boolean, user: any}&gt;(this.loginUrl,authData)
          .subscribe(response =&gt; {
              const token = response.token
              this.token = token;
              if(token){
                  if(response.isAdmin){
                      this.isAdmin = response.isAdmin
                  }
  
                  this.userInfo = response.user
                  const expiersInDuration = response.expiresIn
                  this.setAuthTimer(expiersInDuration)
                  this.isAuthenticated = true
                  this.authStatusListner.next(true); 
                  const now = new Date;
                  const expirationDate = new Date (now.getTime() + expiersInDuration * 1000)
                  this.saveAuthData(token,expirationDate,this.userInfo.email)  
                  this.router.navigate(['/user'])
  
  
              }
          });
      }
  
  
      // Cheacking if the user have the login token in the local storage for autoLogin and the exparation date 
      autoAuthUser(){
       const AuthInformation =  this.getAuthData()
       if (!AuthInformation){
           return
       }
       const now = new Date();
       const expairesIn = AuthInformation.expirationDate.getTime() - now.getTime();
       if (expairesIn &gt; 0){
           this.token = AuthInformation.token
           this.isAuthenticated = true
           this.setAuthTimer(expairesIn / 1000)
           this.authStatusListner.next(true)
           this.login(AuthInformation.email, AuthInformation.token)
       }
      }
  
      // User logout
      logout(){
          this.token = null
          this.isAuthenticated = false
          this.isAdmin = false
          this.authStatusListner.next(false) 
          clearTimeout(this.tokenTimer)
          this.clearAuthData()  
          this.router.navigate(['/user'])   
  
      }
  
  
      // Setting the timer for the token expiration
      private setAuthTimer(duration: number){
          this.tokenTimer =  setTimeout(() =&gt; {
              this.logout();
          }, duration * 1000)
      }
  
      // Setting the Auth token in the local storage
      private saveAuthData(token: string, expirationDate: Date, email:string){
          console.log(token)
           localStorage.setItem("email", email )
           localStorage.setItem("token", token)
           localStorage.setItem("expiration", expirationDate.toISOString())
      }
  
      // Clearing the token from local storage in logout
      private clearAuthData(){
          localStorage.removeItem('email')
          localStorage.removeItem('token')
          localStorage.removeItem('expiration')
      }
  
      // Geting the login token if it exist in the user local storage
      private getAuthData(){
          const email = localStorage.getItem("email")
          const token = localStorage.getItem("token");
          const expirationDate = localStorage.getItem("expiration")
          if (!token || !expirationDate){
              return
          }
          return {
              email: email,
              token: token,
              expirationDate: new Date(expirationDate)
          }
      }
  
  }
  </code></pre>