|
| 1 | +--- |
| 2 | +title: "Full Integration Example" |
| 3 | +description: "See how all the pieces of the React SDK fit together in a complete application." |
| 4 | +--- |
| 5 | + |
| 6 | +Here is a complete, single-file example showing how to build an application with urBackend using `create-react-app` or `Vite`. |
| 7 | + |
| 8 | +This example includes: |
| 9 | +1. The global `<UrProvider>`. |
| 10 | +2. A client-side router (using simple state for demonstration). |
| 11 | +3. A public Login page (`<GuestRoute>`) with the pre-built `<UrAuth>` UI. |
| 12 | +4. A secure Dashboard page (`<ProtectedRoute>`). |
| 13 | +5. A `<UrUserButton>` for the profile dropdown and logout. |
| 14 | + |
| 15 | +## `App.tsx` |
| 16 | + |
| 17 | +```tsx |
| 18 | +import { useState, useEffect } from 'react'; |
| 19 | +import { |
| 20 | + UrProvider, |
| 21 | + UrAuth, |
| 22 | + ProtectedRoute, |
| 23 | + GuestRoute, |
| 24 | + UrUserButton, |
| 25 | + useUser |
| 26 | +} from '@urbackend/react'; |
| 27 | + |
| 28 | +// --- Components --- |
| 29 | + |
| 30 | +function LoginPage({ navigate }: { navigate: (path: string) => void }) { |
| 31 | + return ( |
| 32 | + // If they are already logged in, send them straight to the dashboard |
| 33 | + <GuestRoute fallback={<div>Loading...</div>} onRedirect={() => navigate('/dashboard')}> |
| 34 | + <div style={{ display: 'flex', height: '100vh', alignItems: 'center', justifyContent: 'center' }}> |
| 35 | + <UrAuth providers={['google', 'github']} /> |
| 36 | + </div> |
| 37 | + </GuestRoute> |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +function Dashboard({ navigate }: { navigate: (path: string) => void }) { |
| 42 | + const { user } = useUser(); |
| 43 | + |
| 44 | + return ( |
| 45 | + // If they are NOT logged in, kick them back to login |
| 46 | + <ProtectedRoute fallback={<div>Loading...</div>} onRedirect={() => navigate('/')}> |
| 47 | + |
| 48 | + {/* Top right profile dropdown */} |
| 49 | + <UrUserButton |
| 50 | + onSettingsClick={() => alert('Opening Settings')} |
| 51 | + onProfileClick={() => alert('Opening Profile')} |
| 52 | + /> |
| 53 | + |
| 54 | + <div style={{ padding: '40px', fontFamily: 'sans-serif' }}> |
| 55 | + <h1>Dashboard</h1> |
| 56 | + <p>Welcome back, {user?.name || user?.email}!</p> |
| 57 | + <p>Your user ID is: <code>{user?._id}</code></p> |
| 58 | + </div> |
| 59 | + |
| 60 | + </ProtectedRoute> |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +// --- Main Router --- |
| 65 | + |
| 66 | +function Router() { |
| 67 | + const [route, setRoute] = useState(window.location.pathname); |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + const handlePop = () => setRoute(window.location.pathname); |
| 71 | + window.addEventListener('popstate', handlePop); |
| 72 | + return () => window.removeEventListener('popstate', handlePop); |
| 73 | + }, []); |
| 74 | + |
| 75 | + const navigate = (path: string) => { |
| 76 | + window.history.pushState({}, '', path); |
| 77 | + setRoute(path); |
| 78 | + }; |
| 79 | + |
| 80 | + if (route === '/dashboard') { |
| 81 | + return <Dashboard navigate={navigate} />; |
| 82 | + } |
| 83 | + |
| 84 | + // Default route (Login) |
| 85 | + return <LoginPage navigate={navigate} />; |
| 86 | +} |
| 87 | + |
| 88 | +// --- Root Entry Point --- |
| 89 | + |
| 90 | +export default function App() { |
| 91 | + return ( |
| 92 | + <UrProvider apiKey="pk_live_your_publishable_api_key"> |
| 93 | + <Router /> |
| 94 | + </UrProvider> |
| 95 | + ); |
| 96 | +} |
| 97 | +``` |
0 commit comments