[英]Extract 'Roles' from a JWT in Javascript
I have created a token and sent it to my client application (ReactJS).我创建了一个令牌并将其发送到我的客户端应用程序 (ReactJS)。
{
UserId: "1",
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: "Unset",
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: Array(2),
exp: 1531116565,
iss: "http://www.example.com",
…}
The role item looks like this:角色项如下所示:
[ {0: "User"}, {1, "Admin"} ]
I'm not sure why my roles item is named as a URL.我不确定为什么我的角色项目被命名为 URL。 But what I am trying to do is work out if the JWT indicates the user has an Admin role.
但是我想要做的是确定 JWT 是否表明用户具有管理员角色。
How can I check the array of roles, for 'Admin'?如何检查“管理员”的角色数组?
you can try this你可以试试这个
const roles = [
{0: "User"}, {1: "Admin"}
];
let isAdmin = false;
roles.map( role => {
let r = Object.values( role )
if ( r[0] == 'Admin' ) {
isAdmin = true
}
})
console.log( isAdmin );
Maybe something like this:也许是这样的:
const token = localStorage.getItem("x-access-token");
const role = JSON.parse(window.atob(token.split(".")[1])).role;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.