Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 :: delete file firebase angular 
Javascript :: javascript$.4908BEAMpacidE 
Javascript :: how to get specific property name with numbers from object in javascript 
Javascript :: Using conditional tailwind classes for twin.macro 
Javascript :: suscribe messagechannel lwc 
Javascript :: random jwt secret key generator 
Javascript :: how to convert javascript to typescript angular 
Javascript :: node fs get size 
Javascript :: TypeError: Invalid schema configuration: `True` is not a valid type at path `id.required`. See https://bit.ly/mongoose-schematypes for a list of valid schema types.] 
Javascript :: how to compile javascript class to function 
Javascript :: traversing 2d array javascript 
Javascript :: submit form on ctrl enter 
Javascript :: Plumasil - new item button text 
Javascript :: createfileinput javascript 
Javascript :: create index with multiple fields mongo 
Javascript :: Fetch data changing on reload from array to undefined 
Javascript :: Passing JSON to Javascript in Laravel – but JS is converting the JSON to HTML Entities 
Javascript :: Make a card dynamic with Angular JS 
Javascript :: angularjs Split date and time from api response 
Javascript :: Chrome DevTools - why does variable display in Watch but not under Scope 
Javascript :: React Native : Add a band of color in the background 
Javascript :: What is the best way to download mulitple images using jquery 
Javascript :: reduce dot notations to javascript array 
Javascript :: send data from a file to frontend nodejs 
Javascript :: Another Example In JavaScript Event Delegation 
Javascript :: react state based router 
Javascript :: phaser set mass 
Javascript :: javascript code to decide even or odd number in html using visual studio 
Javascript :: map sord elo 
Javascript :: push replacement getx 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =