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 :: usereducer react 
Javascript :: use of slot in vue 
Javascript :: == vs === javascript 
Javascript :: open source code 
Javascript :: how to usestate in react 
Javascript :: rimraf node.js 
Javascript :: angular get firebase firestore 
Javascript :: replace all swear words using bookmarklet 
Javascript :: Everything Be True 
Javascript :: ubicar escrol en el final 
Javascript :: tailwind only dropdown 
Javascript :: how to disable autonumeric js 
Javascript :: js match emoticon 
Javascript :: discord js get specific user from users 
Javascript :: working with binary and base64 data 
Javascript :: mongoose query same field with different values 
Javascript :: for (var i = 0; i < 10; i++) { setTimeout(function () { console.log(i) }, 10) } What 
Javascript :: firestore save a score as a number not a string in js 
Javascript :: check presense of nmber in a string javascript 
Javascript :: onclick how to post card data to api 
Javascript :: org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject 
Javascript :: save action hide element in jquery 
Javascript :: javascript short syntax get element 
Javascript :: elasticsearch transport client example 
Javascript :: datatables pass headers on request 
Javascript :: one-page web app that requires simple style work. using html, css,javascript and jquery 
Javascript :: how to print more than 20 documents mongo shell 
Javascript :: javascript troubleshooting with jest 
Javascript :: get the first value when mapping through the array 
Javascript :: json url data is not showing in console using jquery 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =