Documentation Index
Fetch the complete documentation index at: https://mintlify.com/MatthewSabia1/SubPirate-Pro/llms.txt
Use this file to discover all available pages before exploring further.
Overview
SubPirate uses Reddit’s OAuth 2.0 web app flow to securely connect user accounts. This allows users to analyze subreddits and run campaigns using their authenticated Reddit accounts.Architecture
The OAuth flow is split between browser and server:- Browser initiates OAuth with Reddit’s authorize endpoint
- Reddit redirects back to your callback URL with an authorization code
- Server exchanges the code for access and refresh tokens via
POST /api/reddit/oauth/exchange - Tokens are encrypted with AES-256-GCM and stored in the database
api/_lib/redditOAuthExchangeFlow.js.
Reddit App Registration
Create a Reddit App
Go to https://www.reddit.com/prefs/apps and click “create another app…”
Configure App Type
Select web app as the application type. This is required for the authorization code flow.
Environment Variables
Client-Side (Public)
These are bundled into the browser and must use theVITE_ prefix:
.env
VITE_REDDIT_REDIRECT_URI is optional. Set it when you need strict host/port consistency across environments.
Server-Side (Secret)
These remain private and are never exposed to the browser:.env
Token Encryption
Reddit refresh tokens are encrypted at rest using AES-256-GCM encryption. This protects tokens even if your database is compromised.Generate Encryption Key
Run this command to generate a secure 32-byte encryption key:.env
OAuth Flow Implementation
The exchange flow inapi/_lib/redditOAuthExchangeFlow.js handles:
1. Code Exchange
Exchanges the authorization code for access and refresh tokens:2. User Identity Verification
Fetches the authenticated user’s Reddit profile:3. Subscription Check
Verifies the user has an active subscription before allowing account connections:4. Token Encryption
Encrypts tokens before storing in the database:5. Account Limit Enforcement
Enforces per-plan limits on connected Reddit accounts:Reconnecting Accounts
Users can reconnect existing Reddit accounts without counting against their quota. The flow verifies:- The
reconnectAccountIdmatches an existing account owned by the user - The Reddit user ID from OAuth matches the stored account’s Reddit user ID
Security Considerations
Troubleshooting
”Invalid redirect_uri” Error
Cause: Mismatch between registered Reddit app redirect URI and environment variables. Solution: EnsureVITE_REDDIT_REDIRECT_URI and REDDIT_REDIRECT_URI exactly match the URI registered in Reddit’s app settings.
”Invalid token response from Reddit”
Cause: Authorization code already used, expired, or client credentials mismatch. Solution:- Verify
REDDIT_CLIENT_IDandREDDIT_CLIENT_SECRETmatch your Reddit app - Check that authorization codes are only used once
- Ensure system time is accurate (OAuth uses timestamps)
“Failed to encrypt Reddit tokens”
Cause: Missing or invalidTOKEN_ENCRYPTION_KEY.
Solution: Generate a new encryption key using the command above and add it to your .env file.
Database Schema
reddit_accounts Table
Stores connected Reddit account metadata:reddit_account_tokens Table
Stores encrypted OAuth tokens:Next Steps
- OpenRouter AI Integration - Enable subreddit analysis
- Stripe Billing - Set up subscription plans