title: Decode token JWT in Ionic
body: <p>I am trying to get the uncoded token from a ws, but I do not succeed. The token is JWT. The token that the ws returns to me is the following:</p>
  
  <pre><code>{\"token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJDb2RpZ28iOiIxMTY4IiwiVXN1YXJpbyI6IjU0MDMyIiwiUGFzc3dvcmQiOiJmZGM3OTZlNzkwMjk2NTdjZGE2YjY1OWQ3NjI0MjAyYSIsIk5vbWJyZXMiOiJEYW5pbG8gRXN0dWFyZG8iLCJBcGVsbGlkb3MiOiJJdHplcCBMdW5hIiwiTm9tYnJlQ29tcGxldG8iOiJEYW5pbG8gRXN0dWFyZG8gSXR6ZXAgTHVuYSIsIlRva2VuIjoiNzQ1QzMzNjktMUUwMy00QTJBLTk1Q0YtMzI4NTZGRUU0MEQ2Iiwic3RhdHVzIjoiT0siLCJuYmYiOjE1NDE0NDkzOTcsImV4cCI6MTU0MTQ1MTE5NywiaWF0IjoxNTQxNDQ5Mzk3LCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjQ5OTc1LyIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDk5NzUvIn0.aZA9yOosJzQqZdpMkYtzIHOj3oqYv5tU_qffVyIXOkg\"}
  </code></pre>
  
  <p>I only need the following code, without diagonals:</p>
  
  <pre><code>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJDb2RpZ28iOiIxMTY4IiwiVXN1YXJpbyI6IjU0MDMyIiwiUGFzc3dvcmQiOiJmZGM3OTZlNzkwMjk2NTdjZGE2YjY1OWQ3NjI0MjAyYSIsIk5vbWJyZXMiOiJEYW5pbG8gRXN0dWFyZG8iLCJBcGVsbGlkb3MiOiJJdHplcCBMdW5hIiwiTm9tYnJlQ29tcGxldG8iOiJEYW5pbG8gRXN0dWFyZG8gSXR6ZXAgTHVuYSIsIlRva2VuIjoiNzQ1QzMzNjktMUUwMy00QTJBLTk1Q0YtMzI4NTZGRUU0MEQ2Iiwic3RhdHVzIjoiT0siLCJuYmYiOjE1NDE0NDkzOTcsImV4cCI6MTU0MTQ1MTE5NywiaWF0IjoxNTQxNDQ5Mzk3LCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjQ5OTc1LyIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDk5NzUvIn0.aZA9yOosJzQqZdpMkYtzIHOj3oqYv5tU_qffVyIXOkg
  </code></pre>
  
  <p>This is the function that the token obtains:</p>
  
  <pre><code>this.webService.doLogin(body)
      .then((response) =&gt; {
        this.jsonToken = jwt_decode(response.data);
        this.nativeStorage.set("json", this.jsonToken );
        this.toasNative.show(JSON.parse(this.jsonToken) , "3000", "bottom").subscribe();   
        this.navCtrl.push(HomePage);
      }).catch((error) =&gt; {
        this.toasNative.show(error, "3000", "bottom").subscribe();
      });
  </code></pre>
  
  <p>I'm using the library <code>import * as jwt_decode from "jwt-decode";</code></p>
  
  <p>if the token I save it with JSON.stringify () the result is that it adds inverse diagonals to the code.</p>
  
  <pre><code>this.jsonToken = JSON.stringify(response.data);
  this.nativeStorage.set("json", this.jsonToken);
  this.toasNative.show(this.jsonToken, "3000", "bottom").subscribe();
  
  "\"{\\\"token\\\":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJDb2RpZ28iOiIxMTY4IiwiVXN1YXJpbyI6IjU0MDMyIiwiUGFzc3dvcmQiOiJmZGM3OTZlNzkwMjk2NTdjZGE2YjY1OWQ3NjI0MjAyYSIsIk5vbWJyZXMiOiJEYW5pbG8gRXN0dWFyZG8iLCJBcGVsbGlkb3MiOiJJdHplcCBMdW5hIiwiTm9tYnJlQ29tcGxldG8iOiJEYW5pbG8gRXN0dWFyZG8gSXR6ZXAgTHVuYSIsIlRva2VuIjoiNzQ1QzMzNjktMUUwMy00QTJBLTk1Q0YtMzI4NTZGRUU0MEQ2Iiwic3RhdHVzIjoiT0siLCJuYmYiOjE1NDE0NDkzOTcsImV4cCI6MTU0MTQ1MTE5NywiaWF0IjoxNTQxNDQ5Mzk3LCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjQ5OTc1LyIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDk5NzUvIn0.aZA9yOosJzQqZdpMkYtzIHOj3oqYv5tU_qffVyIXOkg\\\"}\"
  </code></pre>
  
  <p>Also try this way, but it returns the decoded token:</p>
  
  <pre><code>this.jsonToken = jwt_decode(response.data);
  this.toasNative.show(JSON.stringify(this.jsonToken) , "3000", "bottom").subscribe();
  
  {
    "Codigo": "1168",
    "Usuario": "54032",
    "Password": "fdc796e79029657cda6b659d7624202a",
    "Token": "745C3369-1E03-4A2A-95CF-32856FEE40D6",
    "status": "OK",
    "nbf": 1541449397,
    "exp": 1541451197,
    "iat": 1541449397,
    "iss": "http://localhost:49975/",
    "aud": "http://localhost:49975/"
  }
  </code></pre>
  
  <p>Also try this way, but it returns <code>SyntaxError Unexpected token o in JSON at position 1</code></p>
  
  <pre><code> this.jsonToken = jwt_decode(response.data);
        this.nativeStorage.set("json", this.jsonToken );
        this.toasNative.show(JSON.parse(this.jsonToken) , "3000", "bottom").subscribe();   
  </code></pre>
  
  <p><strong>Update</strong>. This is my component:</p>
  
  <pre><code>import { Component } from '@angular/core';
  import { IonicPage, NavController, NavParams } from 'ionic-angular';
  import { HomePage } from '../home/home';  
  import { Storage } from '@ionic/storage';
  import { ServiceProvider } from '../../providers/service/service';
  
  import { Toast } from '@ionic-native/toast';
  import * as jwt_decode from "jwt-decode";
  
  @IonicPage()
  @Component({
    selector: 'page-login',
    templateUrl: 'login.html',
  })
  export class LoginPage {
    paginaprincipal: any = HomePage;
    loading: any;
    jsonToken: any;
  
    constructor(public navCtrl: NavController,
      public navParams: NavParams,
      private webService: ServiceProvider,
      private nativeStorage: Storage,
      private toasNative: Toast) {
    }
  
    ionViewDidLoad() {
      console.log('ionViewDidLoad LoginPage');
    }
  
    doLogin() {
      const body = {
        Codigo: "1168",
        Usuario: "54032",
        Password: "01992004"
      };
  
      this.webService.doLogin(body)
      .then((response) =&gt; {
        //this.jsonToken = jwt_decode(response.data);
        var parse = JSON.parse(response.data);
        this.nativeStorage.set("json", parse);
        this.toasNative.show("Response with JSON.parse() :" + parse, "3000", "bottom").subscribe();   
        this.navCtrl.push(HomePage);
      }).catch((error) =&gt; {
        this.toasNative.show(error, "3000", "bottom").subscribe();
      });
    }
  }
  </code></pre>
  
  <p>If you can help me get the token, thank you very much.</p>