relai/
├── main.py # Main FastAPI application
├── auth/ # Authentication module
│ ├── __init__.py
│ ├── models.py # Pydantic models for auth
│ ├── jwt_handler.py # JWT token handling
│ ├── google_oauth.py # Google OAuth integration
│ └── routes.py # Auth API routes
├── .env.example # Environment variables template
├── requirements.txt # Python dependencies
└── README.md # This file
- Create a Google Cloud Project and enable the Google+ API
- Create OAuth 2.0 credentials (Client ID and Client Secret)
- Set up authorized redirect URIs
-
Copy the example environment file:
cp .env.example .env
-
Edit
.envand fill in your Google OAuth credentials:GOOGLE_CLIENT_ID: Your Google OAuth Client IDGOOGLE_CLIENT_SECRET: Your Google OAuth Client SecretJWT_SECRET_KEY: A secure random string for JWT signingFRONTEND_URL: Your frontend application URL
-
Install dependencies:
pip install -r requirements.txt
-
Run the application:
python main.py
- GET /auth/google/url - Get Google OAuth authorization URL
- POST /auth/google/token - Exchange OAuth code for JWT token
- GET /auth/me - Get current user information (requires JWT token)
- GET /protected - Example protected route (requires JWT token)
-
Get the Google OAuth URL:
curl http://localhost:8000/auth/google/url
-
Redirect user to the returned
auth_url -
After user authorization, Google redirects back with a
codeparameter -
Exchange the code for a token:
curl -X POST http://localhost:8000/auth/google/token \ -H "Content-Type: application/json" \ -d '{"code": "YOUR_CODE", "redirect_uri": "YOUR_REDIRECT_URI"}'
-
Use the returned JWT token in Authorization header:
curl http://localhost:8000/auth/me \ -H "Authorization: Bearer YOUR_JWT_TOKEN"
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Enable the Google+ API or Google Identity API
- Go to "Credentials" and create OAuth 2.0 Client IDs
- Add your redirect URIs (e.g.,
http://localhost:3000/auth/callbackfor development) - Copy the Client ID and Client Secret to your
.envfile
- Use HTTPS in production
- Configure CORS properly for your domain
- Keep your JWT secret key secure
- Set appropriate token expiration times
- Validate redirect URIs properly