Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js jwt decode

import jwt_decode from "jwt-decode";
var token = "eyJ0eXAiO...";
var decoded = jwt_decode(token);
console.log(decoded);

/* prints: * { foo: "bar", *   exp: 1393286893, *   iat: 1393268893  } */
Comment

decode jwt tokens

let b64DecodeUnicode = str =>
  decodeURIComponent(
    Array.prototype.map.call(atob(str), c =>
      '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
    ).join(''))

let parseJwt = token =>
  JSON.parse(
    b64DecodeUnicode(
      token.split('.')[1].replace('-', '+').replace('_', '/')
    )
  )
Comment

jwt decode

jwt.decode( token, SECRET_KEY, algorithm='HS256' )
Comment

decode jwt

import { JwtHelperService } from "@auth0/angular-jwt";

constructor(private jwtHelper: JwtHelperService) {}

// DDECODIFICA TOKEN
CheckUser(): void {
  	this.role = this.GetUserRole();
	this.username = this.GetUsername();
	const token = this.tokenGetter();
	if (token && this.jwtHelper.isTokenExpired(token)) {
  		alert("Sessione scaduta!");
  		this.router.navigate(["login"]);
      	return;
	}
	if (token && !this.jwtHelper.isTokenExpired(token)) {
  		this.isLogged = true;
  		if (this.role === "User") {
    		this.adminMode = false;
  		} else {
    		this.adminMode = true;
  		}	
    }
	console.log("logged?: " + this.isLogged);
	console.log("role: " + this.role);
	console.log("username: " + this.username);
	console.log("adminMode?: " + this.adminMode);
}

tokenGetter() {
  	return localStorage.getItem("token");
}

GetUserRole() {
    const token = this.tokenGetter();
    if (!token) {
      return;
    }
    let tokenData = this.jwtHelper.decodeToken(token);
    let role =
        tokenData[
          "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
        ];
    return role;
}

GetUsername() {
    const token = this.tokenGetter();
    if (!token) {
      return;
    }
    let tokenData = this.jwtHelper.decodeToken(token);
    let username =
        tokenData[
          "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"
        ];
    return username;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: react catch error in component 
Javascript :: belongstomany sequelize 
Javascript :: array max value 
Javascript :: unexpected end of json input 
Javascript :: Promise.all() with async and await to run in console 
Javascript :: --env production 
Javascript :: material ui sidebar without hooks 
Javascript :: mongodb rename property 
Javascript :: sequelize migration limit 
Javascript :: useref initial value 
Javascript :: sort javascript 
Javascript :: Sequelize using javascript 
Javascript :: how to download array of files from aws s3 using aws sdk in nodejs 
Javascript :: javascript static 
Javascript :: javascript problems 
Javascript :: convert json data into html table 
Javascript :: vue js 
Javascript :: how to check if an element already exists in an array in javascript 
Javascript :: jq json 
Javascript :: trim function 
Javascript :: variables in javascript 
Javascript :: map function javascript 
Javascript :: counting pairs in an array, resulting in a given sum 
Javascript :: javascript function with array parameter 
Javascript :: excel json to table 
Javascript :: how to call function with only selected arguments in javascript 
Javascript :: tooltip in javasrript UI 
Javascript :: get table schema with knex 
Javascript :: document.getelementbyid( timeend ).value example 
Javascript :: js camelcase 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =