在React前端中查看用户角色的方式 – How to see users role in react frontend

huangapple go评论55阅读模式
英文:

Auth0 How to see users role in react frontend

问题

我想查看用户拥有哪个角色,然后只在为该用户启用某个特定角色时在React中显示特定内容。

像这样:

if (user.role1) {
  return (
    <React.Fragment>
      <h1>启用了角色 1</h1>
    </React.Fragment>
  );
}

我尝试使用管理 API 获取角色,但这不起作用。从单页应用程序调用管理 API 也不是一个好方法。我尝试将角色添加到元数据中,但我不知道如何访问这些角色。如何在元数据中访问这些角色?

英文:

I would like to see which role a user has, to then show things in react only if a certain role is enabled for that user.

Like this

if (user.role1) {
    return (
      &lt;React.Fragment&gt;
        &lt;h1&gt;The role 1 is enabled&lt;/h1&gt;
      &lt;/React.Fragment&gt;
    )
};

I tried with the management API to get roles, but that doesn’t work. It is not good to call API management from single page applications either. I tried to add roles to the metadata, but I don’t know how to access those roles. How can I access the roles within the meta data?

答案1

得分: 2

以下是代码的翻译部分:

已解决
我在auth0库中添加了一个规则如下所示

exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://myroles.com';
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
  }
}

然后在React中您可以在useAuth0()钩子中查看它

import './App.css';
import './Loader.css';
import React from 'react';
import LoginButton from './Components/Login/Login';
import LogoutButton from './Components/Login/Logout';
import { useAuth0 } from "@auth0/auth0-react";
import MainHeader from './Components/MainHeader/MainHeader';

function App() {

  const { user, isAuthenticated, isLoading } = useAuth0();

  if (isLoading) {
    return (
      <React.Fragment>
        <br/>
        <div className="lds-roller"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>
      </React.Fragment>
    );
  } 
  else if (!isAuthenticated) {
    return (
      <React.Fragment>
        <MainHeader />
        <h1>Please login!</h1>
        <LoginButton />
      </React.Fragment>
    )
  } 
  else if (isAuthenticated) {
    // <GetRole id={user.sub}/>;
    // GetRoleP(user.sub);
    const roles = user['https://myroles.com/roles'];
    let nsIsEnabled = roles.includes('nos-security');

    if (nsIsEnabled){
      return (
        <React.Fragment>
          <MainHeader />
          <h1>Welcome {user.name}</h1>
          <h1>Nos security enabled</h1>
          <LogoutButton />
        </React.Fragment>
      )
    } else {
      return (
        <React.Fragment>
          <MainHeader />
          <h1>Welcome {user.name}</h1>
          <LogoutButton />
        </React.Fragment>
      )
    };
  };
}

export default App;

希望这能帮助您理解代码的翻译部分。如果您有任何其他问题,请随时提出。

英文:

SOLVED
i added a rule in the auth0 library like this

exports.onExecutePostLogin = async (event, api) =&gt; {
const namespace = &#39;https://myroles.com&#39;;
if (event.authorization) {
api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
}
}

then in react you can view it in the user from useAuth0() hook:

import &#39;./App.css&#39;;
import &#39;./Loader.css&#39;;
import React, {  } from &#39;react&#39;;
import LoginButton from &#39;./Components/Login/Login&#39;;
import LogoutButton from &#39;./Components/Login/Logout&#39;;
import { useAuth0 } from &quot;@auth0/auth0-react&quot;;
import MainHeader from &#39;./Components/MainHeader/MainHeader&#39;;
function App() {
const { user, isAuthenticated, isLoading } = useAuth0();
if (isLoading) {
return (
&lt;React.Fragment&gt;
&lt;br/&gt;
&lt;div className=&quot;lds-roller&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;/React.Fragment&gt;
);
} 
else if (!isAuthenticated) {
return (
&lt;React.Fragment&gt;
&lt;MainHeader /&gt;
&lt;h1&gt;Please login!&lt;/h1&gt;
&lt;LoginButton /&gt;
&lt;/React.Fragment&gt;
)
} 
else if (isAuthenticated) {
// &lt;GetRole id={user.sub}/&gt;
// GetRoleP(user.sub);
const roles = user[&#39;https://myroles.com/roles&#39;];
let nsIsEnabled = roles.includes(&#39;nos-security&#39;);
if (nsIsEnabled){
return (
&lt;React.Fragment&gt;
&lt;MainHeader /&gt;
&lt;h1&gt;Welcome {user.name}&lt;/h1&gt;
&lt;h1&gt;Nos security enabled&lt;/h1&gt;
&lt;LogoutButton /&gt;
&lt;/React.Fragment&gt;
)
} else {
return (
&lt;React.Fragment&gt;
&lt;MainHeader /&gt;
&lt;h1&gt;Welcome {user.name}&lt;/h1&gt;
&lt;LogoutButton /&gt;
&lt;/React.Fragment&gt;
)
};
};
}
export default App;

huangapple
  • 本文由 发表于 2023年2月8日 15:48:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定