1function generate_token(length){
2 //edit the token allowed characters
3 var a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".split("");
4 var b = [];
5 for (var i=0; i<length; i++) {
6 var j = (Math.random() * (a.length-1)).toFixed(0);
7 b[i] = a[j];
8 }
9 return b.join("");
10}
11generate_token(32); //returns "qweQj4giRJSdMNzB8g1XIa6t3YtRIHPH"
12
1import jwt_decode from "jwt-decode";
2var token = "eyJ0eXAiO...";
3var decoded = jwt_decode(token);
4console.log(decoded);
5
6/* prints: * { foo: "bar", * exp: 1393286893, * iat: 1393268893 } */
1jwt.encode( { 'client_id':'value', 'expires_in':'datetime'}, SECRET_KEY, algorithm='HS256' )
2
3OBS:
4Convert datetime to string because in the backend is a json encode system
5and it will generate a TypeError
6ex: TypeError: Object of type datetime is not JSON serializable
7
1{
2 "alg": "CfDJ8OW5OI0CPGJBgSNlGwO0x4YF7qbYKVv7KOO-N0eFtDUzXOrL7F9Xd9W1otVi4ueJOkAmAhuoHFWNkqRaFD7zvAMHMSKncl6Vo5QXKmpvy6vqxOKxSURdIey8aZPRi3Nnhp2p9la-Al5xrVKz0lignRdcCHf3O7pF9zv_sNx_c_T7pUe3WsxaJEPX3t_9FO2Wjw"
3}
4
1let b64DecodeUnicode = str =>
2 decodeURIComponent(
3 Array.prototype.map.call(atob(str), c =>
4 '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
5 ).join(''))
6
7let parseJwt = token =>
8 JSON.parse(
9 b64DecodeUnicode(
10 token.split('.')[1].replace('-', '+').replace('_', '/')
11 )
12 )
1HMACSHA256(
2 base64UrlEncode(header) + "." +
3 base64UrlEncode(payload),
4
5) secret base64 encoded