I am getting one JWT encoded access token from my API in response. But I am not able to decode it and get it in JSON format. I tried using the angular2-jwt library for it, but it did not worked. I am writing my code below:
setXAuthorizationToken(client){
let requestHeader = new Headers();
requestHeader.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post(client.clientURL + "oauth/token", 'grant_type=password&client_id=toto&client_secret=sec&' + 'username=' + client.username
+ '&password=' + client.password, {
headers: requestHeader
}).map(res=>res.json())
.subscribe((token) =>{
if(!token.access_token){
return;
}
else{
var decompressToken = LZString.decompressFromEncodedURIComponent(token.access_token);
console.log(decompressToken);
}
});
}
Can anybody please help me solve this issue?
Each JWT contains a payload. The payload is a base64 encoded JSON object that sits between the two periods in the token. We can decode this payload by using atob() to decode the payload to a JSON string and use JSON. parse() to parse the string into an object.
A JWT is not encrypted. It is based64 encoded and signed. So anyone can decode the token and use its data.
and a VERIFY SIGNATURE which guarantee your token is valid or not. JWT decode only look for public part so it is totally safe to do that in your front-end code.
I use Auth0's jwt-decode package for decoding JWT token in Angular; this package works for me fine.
After installing the package through this command:
npm install jwt-decode
Import this package into your TypeScript class using this syntax:
import * as jwt_decode from "jwt-decode";
Or for newer versions (3 and above):
import jwt_decode from 'jwt-decode';
Then use this library method for decoding your access token like this:
getDecodedAccessToken(token: string): any {
try {
return jwt_decode(token);
} catch(Error) {
return null;
}
}
The token
parameter defines your access token which comes from your API.
The jwt_decode
method returns the decoded token info as an object; you can access any info from your token.
const tokenInfo = this.getDecodedAccessToken(token); // decode token
const expireDate = tokenInfo.exp; // get token expiration dateTime
console.log(tokenInfo); // show decoded token object in console
jwt-decode is a small browser library that helps to decode JWTs token which is Base64Url encoded.
IMPORTANT: This library doesn't validate the token, any well formed JWT can be decoded. You should validate the token in your server-side logic by using something like express-jwt, koa-jwt, Owin Bearer JWT, etc.
Try and use the JavaScript build in function atob()
. Kind of like this:
atob(token.split('.')[1])
Notes:
The token is actually a string.
If you want to know why i split the token you should check this website jwt.io.
Use @auth0/angular-jwt
Step - 1 : Install using npm
npm install @auth0/angular-jwt
Step - 2 : Import the package
import { JwtHelperService } from '@auth0/angular-jwt';
Step - 3 : Create an instance and use
const helper = new JwtHelperService();
const decodedToken = helper.decodeToken(myRawToken);
// Other functions
const expirationDate = helper.getTokenExpirationDate(myRawToken);
const isExpired = helper.isTokenExpired(myRawToken);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With