Skip to content

Commit 419c10a

Browse files
docs: fix withAuthenticationRequired usage pattern in react-router example
1 parent cb56151 commit 419c10a

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

EXAMPLES.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,26 @@ import React from 'react';
170170
import { Route, BrowserRouter, Routes, useNavigate } from 'react-router-dom';
171171
import { Auth0Provider, withAuthenticationRequired } from '@auth0/auth0-react';
172172
import Profile from './Profile';
173+
import Settings from './Settings';
173174

174-
const ProtectedRoute = ({ component, ...args }) => {
175-
const Component = withAuthenticationRequired(component, args);
176-
return <Component />;
175+
// Pattern 1: Direct usage at module level
176+
const ProtectedProfile = withAuthenticationRequired(Profile, {
177+
onRedirecting: () => <div>Redirecting to login...</div>,
178+
});
179+
180+
// Pattern 2: Factory pattern (use when you need hooks in the wrapper)
181+
const createProtectedRoute = (Component, options) => {
182+
const ProtectedComponent = withAuthenticationRequired(Component, options);
183+
return (props) => {
184+
// You can use any React hooks here (useParams, useLocation, useAuth0, etc.)
185+
return <ProtectedComponent {...props} />;
186+
};
177187
};
178188

189+
const ProtectedSettings = createProtectedRoute(Settings, {
190+
onRedirecting: () => <div>Redirecting to login...</div>,
191+
});
192+
179193
const Auth0ProviderWithRedirectCallback = ({ children, ...props }) => {
180194
const navigate = useNavigate();
181195
const onRedirectCallback = (appState) => {
@@ -200,10 +214,8 @@ export default function App() {
200214
>
201215
<Routes>
202216
<Route path="/" exact />
203-
<Route
204-
path="/profile"
205-
element={<ProtectedRoute component={Profile} />}
206-
/>
217+
<Route path="/profile" element={<ProtectedProfile />} />
218+
<Route path="/settings" element={<ProtectedSettings />} />
207219
</Routes>
208220
</Auth0ProviderWithRedirectCallback>
209221
</BrowserRouter>

0 commit comments

Comments
 (0)