import React from 'react';import { withAuthenticationRequired } from '@auth0/auth0-react';const PrivateRoute = () => (<div>Private</div>);export default withAuthenticationRequired(PrivateRoute, { // Show a message while the user waits to be redirected to the login page. onRedirecting: () => (<div>Redirecting you to the login page...</div>)});
import React, { useEffect, useState } from 'react';import { useAuth0 } from '@auth0/auth0-react';const Posts = () => { const { getAccessTokenSilently } = useAuth0(); const [posts, setPosts] = useState(null); useEffect(() => { (async () => { try { const token = await getAccessTokenSilently({ authorizationParams: { audience: 'https://api.example.com/', // Value in Identifier field for the API being called. scope: 'read:posts', // Scope that exists for the API being called. You can create these through the Auth0 Management API or through the Auth0 Dashboard in the Permissions view of your API. } }); const response = await fetch('https://api.example.com/posts', { headers: { Authorization: `Bearer ${token}`, }, }); setPosts(await response.json()); } catch (e) { console.error(e); } })(); }, [getAccessTokenSilently]); if (!posts) { return <div>Loading...</div>; } return ( <ul> {posts.map((post, index) => { return <li key={index}>{post}</li>; })} </ul> );};export default Posts;
Was this page helpful?
⌘I
Assistant
Responses are generated using AI and may contain mistakes.