How to Configure Social Login (OAuth2)¶
P8s supports OAuth2 authentication out of the box for Google, GitHub, and Microsoft.
Step 1: Obtain Provider Credentials¶
- Google: Go to Google Cloud Console, create credentials for an OAuth2 Client ID.
- GitHub: Go to Developer Settings > OAuth Apps > New OAuth App.
- Microsoft: Register an app in the Azure Portal.
Set the callback URL to: http://localhost:8000/auth/social/callback/{provider_name} (e.g., google, github).
Step 2: Configure Settings¶
Add the provider configuration to your backend/settings.py (or environment variables):
# backend/settings.py
from p8s.core.settings import Settings
class ProjectSettings(Settings):
# ... other settings ...
OAUTH2_PROVIDERS = {
"google": {
"client_id": "YOUR_GOOGLE_CLIENT_ID",
"client_secret": "YOUR_GOOGLE_CLIENT_SECRET",
},
"github": {
"client_id": "YOUR_GITHUB_CLIENT_ID",
"client_secret": "YOUR_GITHUB_CLIENT_SECRET",
"scope": "user:email",
},
}
Security Tip: Never commit secrets! Use environment variables like
GOOGLE_CLIENT_IDand load them in your settings.
Step 3: Frontend Integration¶
The backend exposes endpoints at:
- Login: /auth/social/login/{provider}
- Callback: /auth/social/callback/{provider}
In your frontend login page, add buttons linking to these endpoints:
<a href="http://localhost:8000/auth/social/login/google">Login with Google</a>
<a href="http://localhost:8000/auth/social/login/github">Login with GitHub</a>
Step 4: Verify¶
- Click the login link.
- Authenticate with the provider.
- You will be redirected back to your app with a session cookie or JWT token.
- A
SocialAccountrecord will be created and linked to theUser.