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 :: javascript clear an array 
Javascript :: javascript array last element 
Javascript :: js get folder of current script 
Javascript :: closures 
Javascript :: methods of object js 
Javascript :: .index of javascript 
Javascript :: make image onclick in vuejs 
Javascript :: prototype javascript 
Javascript :: code to convert rgb to hsl color 
Javascript :: trigger mouseover on element devtools 
Javascript :: password generator 
Javascript :: how can I send form data with image in angular 
Javascript :: use of length property 
Javascript :: react native geocoding 
Javascript :: Use jsx extension react-native 
Javascript :: get props from methods in vue 
Javascript :: Node Sass does not yet support your current environment: OS X 64-bit with Unsupported runtime in cypress tests 
Javascript :: jquery check valid link 
Javascript :: capitalize text js 
Javascript :: javascript constructor 
Javascript :: .has javascript 
Javascript :: row auto textarea 
Javascript :: limit number in javascript 
Javascript :: desestructuración javascript 
Javascript :: create random password javascript 
Javascript :: jQuery - Chaining 
Javascript :: nested function 
Javascript :: create an express application 
Javascript :: fill in javascript 
Javascript :: how to install react js 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =