简体   繁体   中英

how can get claims parameter from a decoed jwt

I decode a jwt token with jwt-decode in my react app but i can not get claims parameter from that how can i get those? for example i gonna get role from claims params

this is my decode token result :

{
  aud: "SampleAudience"
​
  exp: 1564989998
​
  "http://schemas.microsoft.com/ws/2008/06/identity/claims/role": "SuperAdministrators"
​
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress": "sso_khani"
​
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "sso_khani"
​
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier": "ab57e777-91b6-4c4e-d709-08d715866555"
​
  iss: "threenine.co.uk"
}

You can ask your back-end team to provide values in a defined key names. Still you can do,

Object.keys(decodedObj).forEach(function (key) {
    let res = key.split("/");
    if (res.length > 1) {
        if (res[length - 1] === 'role') {
            // decodedObj[key] will be your role
        }
        // here you will get role, emailaddress, name, nameidentifier
    }
});

Let me know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM