Overview
The social authentication flow:- User clicks “Sign in with Google” (or another provider)
- User is redirected to the provider’s OAuth page
- User authorizes your application
- Provider redirects back to your app with user data
- User is automatically created or logged in
Setting Up Google Authentication
Create Google OAuth credentials
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Navigate to APIs & Services → Credentials
- Click Create Credentials → OAuth client ID
- Select Web application as the application type
- Add authorized redirect URIs:
http://localhost:3333/google/callback(for development)https://yourdomain.com/google/callback(for production)
- Copy the Client ID and Client Secret
How Social Authentication Works
The social authentication is handled by theSocialController:
app/auth/controllers/social_controller.ts
Routes Configuration
Social authentication routes are defined inapp/auth/routes.ts:
app/auth/routes.ts
Adding More Providers
Install provider support (if needed)
AdonisJS Ally supports many providers out of the box:
- GitHub
- Discord
- And more…
Security Considerations
CSRF Protection
The Ally package includes built-in CSRF protection through state verification. Never disable this in production.
Email Verification
Social providers verify email addresses, so users authenticated via OAuth can be considered verified.
Profile Updates
Consider syncing user profile data (name, avatar) on each login to keep it up to date.
Troubleshooting
Redirect URI mismatch error
Redirect URI mismatch error
Ensure the callback URL in your OAuth provider settings exactly matches the one in your config:
State mismatch error
State mismatch error
This usually happens when:
- Session cookies are not being saved
- You’re testing across different domains (localhost vs 127.0.0.1)
- Session expires between redirect and callback
config/session.ts.Access denied error
Access denied error
The user clicked “Cancel” or “Deny” on the OAuth consent screen. This is normal behavior and should be handled gracefully with a helpful error message.
Invalid client error
Invalid client error
Verify that:
- Client ID and Secret are correct
- OAuth app is enabled in provider settings
- Environment variables are loaded correctly
Environment Variables Reference
.env.example
Resources
AdonisJS Ally Docs
Official social authentication guide
Google OAuth Setup
Google Cloud Console
GitHub OAuth Apps
Create GitHub OAuth apps
Supported Providers
View all supported providers