'my token sends user_id How I can get it by localStorage in Angular?

I need help trying to figure it out, i am using django as backend and Angular as frontend. My django backend pass a token so it can be acessed in frontend by login.

login.ts

 onLogin() {
this.service.loginUser(this.input).subscribe(
  response => {
    console.log(response)
    this.jwttoken.setToken(response['token']);
    this.jwttoken.getItem(response);
    this.router.navigate(['dashboard']);
  },
  error => {
    console.log('error', error);
  }

i save it in a localstorage that can be acessed on my service

jwttoken service

      jwtToken : string
  decodedToken: {[key: string]: string}
  public storage: Storage;

  constructor() {
    this.storage = window.localStorage;
  }


  setToken(token: string){
    if (token) {
      this.jwtToken = token;
    }
  }

  
  getItem(key: string) {
    if(this.storage){
      localStorage.setItem('token',(key));
    }
    
    return null;
    
  }
  

i need to have my user id that i can see on my web browser console.

{"token":"[object Object]","d34330659cba7cf64e8414f83aa6522f55b0f978":"d34330659cba7cf64e8414f83aa6522f55b0f978","[object Object]":"{"token":"d34330659cba7cf64e8414f83aa6522f55b0f978","user_id":1,"email":"[email protected]"}"}

this is where i need to access my user id, so i can send it to my html file

    export class RegularComponent implements OnInit {
  
  patient_code: any;
  number_tumor: any;
  tumor_size: any;
  tumor_volume: any;
  biopsy_date: any;
  hp_lote_number: any;
  closeResult: string;
  userlists: any;

  user_id: any;

  constructor(private service: SharedService, private modalService: NgbModal, private jwtstorage: JWTTokenServiceService) { }

  localstorage = JSON.stringify(this.jwtstorage.storage).replace(/\\/g, "");
  


  ngOnInit() {
    // this.getUserlist();  
    console.log(this.localstorage);
  
  }



      // getUserlist() {
      //   let observable = this.service.getUsersList(); 
      //   observable.subscribe((data) => { this.userlists = data; console.log(data); return data; }); }
      

 open(content) {
   this.modalService.open(content,{ariaLabelledBy: 'modal-basic-title', size: 'lg'}).result.then((result) => {
     this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
      }

  private getDismissReason(reason: any): string {
    if (reason === ModalDismissReasons.ESC) {
      return 'by pressing ESC';
    } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
      return 'by clicking on a backdrop';
    } else {
      return `with: ${reason}`;
    }
  }       

}

I searched all around, and I can't figure it out. Could you explain to me how I can do this? I just need the user_id of that string that cames from the localstorage. Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source