Use auth0 jwt in api
This commit is contained in:
@@ -7,19 +7,19 @@ import ProfileFromApi from "./ProfileFromApi";
|
||||
|
||||
const AuthTest = () => {
|
||||
return (
|
||||
<Auth0Provider
|
||||
domain="dev-b-bgocbi.us.auth0.com"
|
||||
clientId="c5A08v815qx3ySjffHbaUc5b5MPWhRad"
|
||||
redirectUri={window.location.origin}
|
||||
audience="https://dev-b-bgocbi.us.auth0.com/api/v2/"
|
||||
scope="read:current_user update:current_user_metadata"
|
||||
>
|
||||
<LoginButton />
|
||||
<LogoutButton />
|
||||
<Profile />
|
||||
<ProfileFromApi />
|
||||
</Auth0Provider>
|
||||
<Auth0Provider
|
||||
domain="dev-b-bgocbi.us.auth0.com"
|
||||
clientId="c5A08v815qx3ySjffHbaUc5b5MPWhRad"
|
||||
redirectUri={window.location.origin}
|
||||
audience="https://1t8da6s66e.execute-api.us-east-1.amazonaws.com/"
|
||||
scope="read:stuff"
|
||||
>
|
||||
<LoginButton />
|
||||
<LogoutButton />
|
||||
<Profile />
|
||||
<ProfileFromApi />
|
||||
</Auth0Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export default AuthTest;
|
||||
export default AuthTest;
|
||||
|
||||
@@ -7,4 +7,4 @@ const LoginButton = () => {
|
||||
return <button onClick={() => loginWithRedirect()}>Log In</button>;
|
||||
};
|
||||
|
||||
export default LoginButton;
|
||||
export default LoginButton;
|
||||
|
||||
@@ -11,4 +11,4 @@ const LogoutButton = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default LogoutButton;
|
||||
export default LogoutButton;
|
||||
|
||||
@@ -19,4 +19,4 @@ const Profile = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default Profile;
|
||||
export default Profile;
|
||||
|
||||
@@ -3,35 +3,34 @@ import { useAuth0 } from "@auth0/auth0-react";
|
||||
|
||||
const ProfileFromApi = () => {
|
||||
const { user, isAuthenticated, getAccessTokenSilently } = useAuth0();
|
||||
const [userMetadata, setUserMetadata] = useState(null);
|
||||
|
||||
const [message, setMessage] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
const getUserMetadata = async () => {
|
||||
const domain = "dev-b-bgocbi.us.auth0.com";
|
||||
|
||||
const getMessage = async () => {
|
||||
|
||||
try {
|
||||
const accessToken = await getAccessTokenSilently({
|
||||
audience: `https://${domain}/api/v2/`,
|
||||
scope: "read:current_user",
|
||||
audience: "https://1t8da6s66e.execute-api.us-east-1.amazonaws.com/",
|
||||
scope: "read:stuff",
|
||||
});
|
||||
|
||||
const userDetailsByIdUrl = `https://${domain}/api/v2/users/${user.sub}`;
|
||||
|
||||
const metadataResponse = await fetch(userDetailsByIdUrl, {
|
||||
|
||||
const authorizedUrl = "https://1t8da6s66e.execute-api.us-east-1.amazonaws.com/authorized";
|
||||
|
||||
const authorizedResponse = await fetch(authorizedUrl, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
const { user_metadata } = await metadataResponse.json();
|
||||
|
||||
setUserMetadata(user_metadata);
|
||||
|
||||
const message = await authorizedResponse.text();
|
||||
|
||||
setMessage(message);
|
||||
} catch (e) {
|
||||
console.log(e.message);
|
||||
}
|
||||
};
|
||||
|
||||
getUserMetadata();
|
||||
|
||||
getMessage();
|
||||
}, [getAccessTokenSilently, user?.sub]);
|
||||
|
||||
return (
|
||||
@@ -40,15 +39,15 @@ const ProfileFromApi = () => {
|
||||
<img src={user.picture} alt={user.name} />
|
||||
<h2>{user.name}</h2>
|
||||
<p>{user.email}</p>
|
||||
<h3>User Metadata</h3>
|
||||
{userMetadata ? (
|
||||
<pre>{JSON.stringify(userMetadata, null, 2)}</pre>
|
||||
<h3>API Message</h3>
|
||||
{message ? (
|
||||
<pre>{message}</pre>
|
||||
) : (
|
||||
"No user metadata defined"
|
||||
"No message found"
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
export default ProfileFromApi;
|
||||
export default ProfileFromApi;
|
||||
|
||||
Reference in New Issue
Block a user