Discourse Integration Guide
This guide explains how to integrate Discourse (community forum) with Silvia, allowing users to log in to the forum using their Silvia account.Overview
Silvia implements Discourse Connect (formerly DiscourseConnect SSO) to enable single sign-on between the Silvia app and the Discourse forum. When users click “Login” on the forum, they’re redirected to Silvia for authentication, then automatically logged in to Discourse.Architecture
Setup Instructions
1. Generate SSO Secret
Generate a strong random secret (32+ characters):2. Configure Environment Variables
Add these to your.env file:
3. Configure Discourse Settings
In your Discourse admin panel (Settings → Login), configure: Required Settings:- ✅
enable discourse connect- Enable this to activate SSO discourse connect url- Set to:https://www.cfosilvia.com/api/discourse/ssodiscourse connect secret- Use the same secret from step 1
- ✅
discourse connect overrides email- Silvia email takes precedence - ✅
discourse connect overrides username- Silvia username takes precedence - ✅
discourse connect overrides name- Silvia name takes precedence - ✅
discourse connect overrides avatar- Silvia avatar takes precedence
- ✅
invite only- Require users to have a Silvia account - ✅
verbose discourse connect logging- Enable during testing for detailed SSO logs discourse connect allows all return paths- Allow redirects to any Discourse pathlogout redirect- Set tohttps://www.cfosilvia.com/api/discourse/logoutfor logout sync
4. Deploy to Production
Add the same environment variables to your Vercel project:- Go to Vercel Dashboard → Your Project → Settings → Environment Variables
- Add:
DISCOURSE_SSO_SECRET= Your generated secretDISCOURSE_URL=https://community.cfosilvia.com
- Redeploy your application
5. Test the Integration
- Log out of both Silvia and Discourse
- Navigate to your Discourse forum: https://community.cfosilvia.com
- Click “Login” - you’ll be redirected to the custom community sign-in page at
/community-signin - Sign in with your credentials (or create an account)
- Verify you’re automatically redirected back to Discourse and logged in
/community-signin with:
- Custom welcome message and community branding
- List of community benefits
- Clerk authentication component
- Seamless redirect back to the forum after login
6. Verify User Data Sync
Check that the following data syncs correctly:- ✅ Email address
- ✅ Username
- ✅ Full name
- ✅ Avatar/profile picture
- ✅ User is created on first login
- ✅ User data updates on subsequent logins
User Data Mapping
Silvia sends the following user data to Discourse:Security Considerations
HTTPS Required
Both Silvia and Discourse must use HTTPS in production. The SSO flow involves redirects with sensitive signed payloads.Secret Management
- Store the SSO secret in environment variables (never commit to git)
- Use the same secret in both Silvia and Discourse
- Rotate the secret periodically (requires updating both systems)
Signature Verification
- Silvia verifies Discourse’s signature before processing requests
- Discourse verifies Silvia’s signature before logging in users
- Invalid signatures are rejected with a 403 error
Return URL Validation
Thereturn_sso_url from Discourse should be validated to prevent open redirects. Currently, we trust Discourse to only send valid return URLs.
Troubleshooting
”Invalid signature” Error
Cause: The SSO secret doesn’t match between Silvia and Discourse. Solution:- Verify
DISCOURSE_SSO_SECRETin your.envmatches thediscourse connect secretin Discourse settings - Ensure there are no extra spaces or newlines in either secret
- Restart your Silvia server after changing the secret
”Missing SSO parameters” Error
Cause: The request from Discourse is missing thesso or sig parameters.
Solution:
- Verify the
discourse connect urlin Discourse settings is correct - Check that Discourse is actually sending the SSO request (check Discourse logs)
Users Not Being Created in Discourse
Cause: The response payload might be invalid or missing required fields. Solution:- Check the Silvia server logs for the SSO response payload
- Verify the user has a valid email address in Clerk
- Check Discourse logs for any errors processing the SSO response
Redirect Loop
Cause: The SSO endpoint might be redirecting back to itself. Solution:- Ensure the
/api/discourse/ssoroute checks for Clerk authentication correctly - Verify that the
redirect_urlparameter doesn’t create a loop - Check browser cookies are enabled (required for Clerk authentication)
Avatar Not Syncing
Cause: Discourse might not be fetching external avatar URLs. Solution:- Enable
download remote images to localin Discourse settings - Verify
discourse connect overrides avataris enabled - Check that the avatar URL is publicly accessible
Advanced Configuration
Custom User Fields
To sync custom user data to Discourse:- Create custom user fields in Discourse (Admin → Customize → User Fields)
- Add the field mappings to the SSO response in
/app/api/discourse/sso/route.ts:
Group Membership
To automatically add users to Discourse groups based on their Silvia subscription:Logout Sync
To log users out of Silvia when they log out of Discourse:- Enable
logout redirectin Discourse settings - Set it to:
https://www.cfosilvia.com/api/discourse/logout - Modify
/app/api/discourse/logout/route.tsto redirect to/sign-out
Monitoring
Success Metrics to Track
- Number of successful SSO logins per day
- Average time to complete SSO flow
- Failed SSO attempts (invalid signatures, missing users, etc.)
- New vs. returning users via SSO
Logging
The SSO endpoints log important events:- ✅ Successful SSO completions
- ❌ Invalid signatures
- ❌ Missing parameters
- ❌ User not found errors
API Endpoints
GET /api/discourse/sso
Main SSO endpoint that handles authentication requests from Discourse. Parameters:sso- Base64-encoded payload from Discoursesig- HMAC-SHA256 signature of the sso parameter
- Redirect to Discourse with signed user data
- Or redirect to /sign-in if user not authenticated
https://www.cfosilvia.com/api/discourse/sso
GET /api/discourse/logout
Optional endpoint for handling logout requests from Discourse. Parameters:sso- Base64-encoded payload from Discoursesig- HMAC-SHA256 signature
- JSON success response
- Or redirect to /sign-out to log out of Silvia
https://www.cfosilvia.com/api/discourse/logout