This is an example FastAPI application that demonstrates how to use the Kinde FastAPI integration.
- Install the required dependencies:
pip install fastapi uvicorn python-multipart python-dotenv-
Configure your Kinde application:
- Create a new application in your Kinde dashboard
- Set the redirect URI to
http://localhost:8000/callback - Copy your client ID and client secret
-
Create a
.envfile in the examples directory with the following variables:
KINDE_CLIENT_ID=your_client_id
KINDE_CLIENT_SECRET=your_client_secret
KINDE_REDIRECT_URI=http://localhost:8000/callback
KINDE_HOST=https://your-domain.kinde.comRun the example application from the SDK root directory:
python -m uvicorn kinde_fastapi.examples.example_app:app --reload --port 8000The application will be available at http://localhost:8000.
-
Authentication Flow
- Login with Kinde
- OAuth callback handling
- Session management
- Logout
-
Automatic Route Registration
- The OAuth class automatically registers these routes:
/login- Redirects to Kinde login/callback- Handles OAuth callback from Kinde/logout- Logs out the user/register- Redirects to Kinde registration/user- Returns user information (JSON)
- The OAuth class automatically registers these routes:
-
Protected Routes
- Example of a protected route that requires authentication
- Automatic redirection to login for unauthenticated users
-
User Information
- Retrieving and displaying user information
- Session-based user state management
/- Home page (shows different content based on authentication status)/login- Redirects to Kinde login (auto-registered)/callback- Handles OAuth callback from Kinde (auto-registered)/logout- Logs out the user (auto-registered)/register- Redirects to Kinde registration (auto-registered)/user- Returns user information as JSON (auto-registered)/protected- Example protected route
- Always use HTTPS in production
- Use a secure session secret key
- Implement proper state parameter validation
- Handle OAuth errors appropriately
- Implement proper session management
- Consider implementing CSRF protection
- Add proper error handling
- Implement state parameter validation
- Add more security features
- Use proper templates instead of inline HTML
- Add user profile management
- Implement role-based access control