|
| 1 | +# `@asgardeo/react` Quickstart |
| 2 | + |
| 3 | +This guide will help you quickly integrate Asgardeo authentication into your React application. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- [Node.js](https://nodejs.org/en/download) (version 16 or later. LTS version recommended) |
| 8 | +- An [Asgardeo account](https://wso2.com/asgardeo/docs/get-started/create-asgardeo-account/) |
| 9 | + |
| 10 | +## Step 1: Configure an Application in Asgardeo |
| 11 | + |
| 12 | +1. **Sign in to Asgardeo Console** |
| 13 | + - Go to [Asgardeo Console](https://console.asgardeo.io/) |
| 14 | + - Sign in with your Asgardeo account |
| 15 | + |
| 16 | +2. **Create a New Application** |
| 17 | + - Click **Applications** in the left sidebar |
| 18 | + - Click **+ New Application** |
| 19 | + - Choose **Single Page Application (SPA)** |
| 20 | + - Enter your application name (e.g., "Teamspace") |
| 21 | + |
| 22 | +3. **Note Down Your Credentials from the `Quickstart` tab** |
| 23 | + - Copy the **Client ID** from the application details |
| 24 | + - Note your **Base URL** (ex: `https://api.asgardeo.io/t/<ORGANIZATION>`) |
| 25 | + |
| 26 | +4. **Configure Application Settings from the `Protocol` tab** |
| 27 | + - **Authorized redirect URLs**: Add your application URLs |
| 28 | + - `https://localhost:5173` |
| 29 | + - **Allowed origins**: Add the same URLs as above |
| 30 | + - Click **Update** to save the configuration |
| 31 | + |
| 32 | +## Step 2: Create a React Application |
| 33 | + |
| 34 | +If you don't have a React application set up yet, you can create one using Vite: |
| 35 | + |
| 36 | +```bash |
| 37 | +# Using npm |
| 38 | +npm create vite@latest teamspace --template react |
| 39 | + |
| 40 | +# Using pnpm |
| 41 | +pnpm create vite@latest teamspace --template react |
| 42 | + |
| 43 | +# Using yarn |
| 44 | +yarn create vite teamspace --template react |
| 45 | +``` |
| 46 | + |
| 47 | +## Step 3: Install the SDK |
| 48 | + |
| 49 | +Install the Asgardeo React SDK in your project: |
| 50 | + |
| 51 | +```bash |
| 52 | +# Using npm |
| 53 | +npm install @asgardeo/react |
| 54 | + |
| 55 | +# Using pnpm |
| 56 | +pnpm add @asgardeo/react |
| 57 | + |
| 58 | +# Using yarn |
| 59 | +yarn add @asgardeo/react |
| 60 | +``` |
| 61 | + |
| 62 | +## Step 4: Configure the Provider |
| 63 | + |
| 64 | +Wrap your application with the `AsgardeoProvider` in your main entry file i.e. `src/main.tsx`: |
| 65 | + |
| 66 | +```tsx |
| 67 | +import { StrictMode } from 'react' |
| 68 | +import { createRoot } from 'react-dom/client' |
| 69 | +import './index.css' |
| 70 | +import App from './App.tsx' |
| 71 | +import { AsgardeoProvider } from '@asgardeo/react' |
| 72 | + |
| 73 | +createRoot(document.getElementById('root')!).render( |
| 74 | + <StrictMode> |
| 75 | + <AsgardeoProvider |
| 76 | + baseUrl="<Base URL>" |
| 77 | + clientId="<CLIENT_ID>" |
| 78 | + > |
| 79 | + <App /> |
| 80 | + </AsgardeoProvider> |
| 81 | + </StrictMode> |
| 82 | +) |
| 83 | +``` |
| 84 | + |
| 85 | +Replace: |
| 86 | +- `<Base URL>` with the Base URL you noted in Step 1 (e.g., `https://api.asgardeo.io/t/<ORGANIZATION>`) |
| 87 | +- `<CLIENT_ID>` with the Client ID from Step 1 |
| 88 | + |
| 89 | +## Step 4: Add Sign-in & Sign-out to Your App |
| 90 | + |
| 91 | +Update your `App.tsx` to include sign-in and sign-out functionality: |
| 92 | + |
| 93 | +```tsx |
| 94 | +import { SignedIn, SignedOut, SignInButton, SignOutButton, User } from '@asgardeo/react' |
| 95 | +import './App.css' |
| 96 | + |
| 97 | +function App() { |
| 98 | + return ( |
| 99 | + <> |
| 100 | + <SignedIn> |
| 101 | + <SignOutButton /> |
| 102 | + </SignedIn> |
| 103 | + <SignedOut> |
| 104 | + <SignInButton /> |
| 105 | + </SignedOut> |
| 106 | + </> |
| 107 | + ) |
| 108 | +} |
| 109 | + |
| 110 | +export default App |
| 111 | +``` |
| 112 | + |
| 113 | +## Step 6: Try Login |
| 114 | + |
| 115 | +Run your application and test the sign-in functionality. You should see a "Sign In" button when you're not signed in, and clicking it will redirect you to the Asgardeo sign-in page. |
| 116 | + |
| 117 | +```bash |
| 118 | +# Using npm |
| 119 | +npm run dev |
| 120 | + |
| 121 | +# Using pnpm |
| 122 | +pnpm dev |
| 123 | + |
| 124 | +# Using yarn |
| 125 | +yarn dev |
| 126 | +``` |
| 127 | + |
| 128 | +## Next Steps |
| 129 | + |
| 130 | +🎉 **Congratulations!** You've successfully integrated Asgardeo authentication into your React app. |
| 131 | + |
| 132 | +### What to explore next: |
| 133 | + |
| 134 | +- **[Complete Guide](./COMPLETE%20GUIDE.md)** - Learn about advanced features and customization |
| 135 | +- **[API Documentation](./API.md)** - Explore all available components and hooks |
| 136 | +- **Custom Styling** - Customize the appearance of authentication components |
| 137 | +- **Protected Routes** - Implement route-level authentication |
| 138 | +- **User Profile Management** - Access and manage user profile data |
| 139 | + |
| 140 | +### Common Issues |
| 141 | + |
| 142 | +- **Redirect URL Mismatch**: Ensure your redirect URLs in Asgardeo match your local/production URLs exactly |
| 143 | +- **CORS Errors**: Make sure to add your domain to the "Allowed Origins" in your Asgardeo application settings |
| 144 | +- **Client ID Issues**: Double-check that you're using the correct Client ID from your Asgardeo application |
| 145 | + |
| 146 | +For more help, visit the [Asgardeo Documentation](https://wso2.com/asgardeo/docs/) or check out our [examples](../../examples/). |
0 commit comments