Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 11,607 Bytes
61d29fc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | ---
sidebar_position: 6
---
# Authentication Setup Guide
Complete guide for setting up OAuth authentication with HuggingFace, Google, Facebook, and GitHub, plus Neon serverless PostgreSQL.
## π₯ Database Setup: Neon (Serverless PostgreSQL)
**Recommended Choice** - Free tier, zero-config, perfect for production.
### Why Neon?
β
**Free tier**: 0.5 GB storage with scale-to-zero
β
**Managed backups**: Point-in-time recovery included
β
**Encrypted**: At rest + in transit
β
**Public internet**: Perfect for HuggingFace Spaces
β
**Standard PostgreSQL**: No vendor lock-in
β
**Enterprise backing**: Acquired by Databricks (2025)
### Setup Steps
1. **Sign up at [neon.tech](https://neon.tech)**
- Click "Sign up" (free, no credit card required)
- Sign in with GitHub or email
2. **Create a new project**
- Click "New Project"
- Name: `open-navigator-engagement`
- Region: Choose closest to your users (e.g., `US East`)
- PostgreSQL version: 16 (latest)
3. **Copy connection string**
- Go to Dashboard β Connection Details
- Copy the **"Connection string"**
- Format: `postgresql://user:password@ep-xxx.us-east-2.aws.neon.tech/neondb?sslmode=require`
4. **Add to `.env` file**
```bash
DATABASE_URL=postgresql://user:password@ep-xxx.us-east-2.aws.neon.tech/neondb?sslmode=require
```
5. **Database auto-initialization**
- Tables are created automatically on first API startup
- No migration scripts needed!
### Verify Connection
```bash
# Start the API server
source .venv/bin/activate
python main.py serve
# You should see:
# β
Database initialized at: postgresql://...
```
---
## π OAuth Provider Setup
### 1. HuggingFace OAuth
**Get credentials:** [huggingface.co/settings/applications](https://huggingface.co/settings/applications)
#### Steps:
1. Go to HuggingFace Settings β Applications
2. Click **"Create an OAuth app"**
3. Fill in:
- **Application name**: `Open Navigator`
- **Homepage URL**: `https://www.communityone.com`
- **Redirect URI**:
- Development: `http://localhost:8000/auth/callback/huggingface`
- Production: `https://www.communityone.com/api/auth/callback/huggingface`
- **Scopes**: `openid profile email`
4. Click **"Create"**
5. Copy **Client ID** and **Client Secret**
#### Add to `.env`:
```bash
HUGGINGFACE_CLIENT_ID=hf_oauth_xxx
HUGGINGFACE_CLIENT_SECRET=hf_oauth_secret_xxx
```
---
### 2. Google OAuth
**Get credentials:** [console.cloud.google.com/apis/credentials](https://console.cloud.google.com/apis/credentials)
#### Steps:
1. **Create a project** (or select existing)
- Go to Google Cloud Console
- Select project or click "New Project"
- Name: `Open Navigator`
2. **Enable Google+ API**
- Go to APIs & Services β Library
- Search "Google+ API"
- Click Enable
3. **Configure OAuth consent screen**
- Go to APIs & Services β OAuth consent screen
- User Type: **External**
- App name: `Open Navigator`
- User support email: Your email
- Developer contact: Your email
- Scopes: `email`, `profile`, `openid`
4. **Create OAuth 2.0 Client ID**
- Go to APIs & Services β Credentials
- Click **"Create Credentials"** β OAuth client ID
- Application type: **Web application**
- Name: `Open Navigator Web Client`
- Authorized redirect URIs:
- `http://localhost:8000/auth/callback/google` (development)
- `https://www.communityone.com/api/auth/callback/google` (production)
- Click **"Create"**
5. Copy **Client ID** and **Client Secret**
#### Add to `.env`:
```bash
GOOGLE_CLIENT_ID=123456789-xxxxx.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=GOCSPX-xxxxx
```
---
### 3. Facebook OAuth
**Get credentials:** [developers.facebook.com/apps](https://developers.facebook.com/apps)
#### Steps:
1. **Create a new app**
- Click **"Create App"**
- Type: **Consumer**
- App Name: `Open Navigator`
2. **Add Facebook Login**
- Dashboard β Add Product
- Select **"Facebook Login"** β Set up
3. **Configure OAuth settings**
- Go to Facebook Login β Settings
- Valid OAuth Redirect URIs:
- `http://localhost:8000/auth/callback/facebook`
- `https://www.communityone.com/api/auth/callback/facebook`
- Client OAuth Login: **Yes**
- Web OAuth Login: **Yes**
4. **Get App ID and Secret**
- Go to Settings β Basic
- Copy **App ID** and **App Secret**
#### Add to `.env`:
```bash
FACEBOOK_APP_ID=1234567890123456
FACEBOOK_APP_SECRET=xxxxxxxxxxxxx
```
---
### 4. GitHub OAuth
**Get credentials:** [github.com/settings/developers](https://github.com/settings/developers)
#### Steps:
1. **Register new OAuth application**
- Go to Settings β Developer settings β OAuth Apps
- Click **"New OAuth App"**
2. **Fill in details**
- **Application name**: `Open Navigator`
- **Homepage URL**: `https://www.communityone.com`
- **Authorization callback URL**:
- Development: `http://localhost:8000/auth/callback/github`
- Production: `https://www.communityone.com/api/auth/callback/github`
3. **Create application**
- Click **"Register application"**
- Copy **Client ID**
- Generate **Client Secret** and copy it
#### Add to `.env`:
```bash
GITHUB_CLIENT_ID=Iv1.xxxxxxxxxxxxx
GITHUB_CLIENT_SECRET=xxxxxxxxxxxxx
```
---
## π Generate JWT Secret
Create a secure random secret for JWT tokens:
```bash
# Generate 32-byte random secret
openssl rand -hex 32
# Or use Python
python -c "import secrets; print(secrets.token_urlsafe(32))"
```
Add to `.env`:
```bash
JWT_SECRET_KEY=your_random_32_char_secret_here
```
---
## π Environment Configuration
### Development `.env`:
```bash
# Database
DATABASE_URL=postgresql://user:password@ep-xxx.us-east-2.aws.neon.tech/neondb?sslmode=require
# JWT
JWT_SECRET_KEY=your_random_secret_key
# Frontend URL
FRONTEND_URL=http://localhost:5173
# OAuth (all providers)
HUGGINGFACE_CLIENT_ID=hf_oauth_xxx
HUGGINGFACE_CLIENT_SECRET=hf_oauth_secret_xxx
GOOGLE_CLIENT_ID=123456789-xxx.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=GOCSPX-xxx
FACEBOOK_APP_ID=123456789
FACEBOOK_APP_SECRET=xxx
GITHUB_CLIENT_ID=Iv1.xxx
GITHUB_CLIENT_SECRET=xxx
```
### Production (HuggingFace Spaces)
Add secrets in **Space Settings β Repository secrets**:
| Secret Name | Value |
|-------------|-------|
| `DATABASE_URL` | `postgresql://...neon.tech/...` |
| `JWT_SECRET_KEY` | Your random secret |
| `FRONTEND_URL` | `https://www.communityone.com` |
| `HUGGINGFACE_CLIENT_ID` | From HF OAuth app |
| `HUGGINGFACE_CLIENT_SECRET` | From HF OAuth app |
| `GOOGLE_CLIENT_ID` | From Google Cloud |
| `GOOGLE_CLIENT_SECRET` | From Google Cloud |
| `FACEBOOK_APP_ID` | From Facebook app |
| `FACEBOOK_APP_SECRET` | From Facebook app |
| `GITHUB_CLIENT_ID` | From GitHub OAuth app |
| `GITHUB_CLIENT_SECRET` | From GitHub OAuth app |
---
## π§ͺ Testing Authentication
### 1. Start the API server
```bash
source .venv/bin/activate
python main.py serve
```
### 2. Start the frontend
```bash
cd frontend
npm run dev
```
### 3. Test OAuth flows
1. Visit `http://localhost:5173`
2. Click **"Login"** in top-right
3. Select a provider (HuggingFace, Google, Facebook, or GitHub)
4. Complete OAuth flow
5. You should be redirected back with your profile visible
### 4. Verify database
Check that user was created in Neon:
```bash
# Option 1: Neon SQL Editor (in dashboard)
SELECT * FROM users;
# Option 2: psql client
psql "postgresql://user:password@ep-xxx.neon.tech/neondb?sslmode=require"
\dt # List tables
SELECT * FROM users;
```
---
## π Database Schema
The authentication system creates these tables automatically:
### `users` table
| Column | Type | Description |
|--------|------|-------------|
| `id` | Integer | Primary key |
| `email` | String(255) | User email (unique) |
| `username` | String(100) | Optional username |
| `full_name` | String(255) | Display name |
| `avatar_url` | String(500) | Profile picture URL |
| `oauth_provider` | String(50) | `huggingface`, `google`, `facebook`, `github` |
| `oauth_id` | String(255) | Provider's user ID |
| `hashed_password` | String(255) | For email/password (optional) |
| `is_active` | Boolean | Account status |
| `is_verified` | Boolean | Email verification status |
| `created_at` | DateTime | Account creation |
| `updated_at` | DateTime | Last update |
| `last_login` | DateTime | Last login timestamp |
| `preferences` | Text | User settings (JSON) |
### `oauth_states` table
| Column | Type | Description |
|--------|------|-------------|
| `id` | Integer | Primary key |
| `state_token` | String(255) | CSRF protection token |
| `provider` | String(50) | OAuth provider name |
| `redirect_uri` | String(500) | Callback URL |
| `created_at` | DateTime | Creation timestamp |
| `expires_at` | DateTime | Expiration (10 minutes) |
---
## π Security Best Practices
### β
DO:
- Use HTTPS in production
- Rotate JWT secrets regularly
- Keep OAuth secrets in environment variables (never commit to git)
- Use Neon's connection pooling
- Enable Neon's IP allowlist for production
### β DON'T:
- Commit `.env` file to git (it's in `.gitignore`)
- Share OAuth secrets publicly
- Use weak JWT secrets
- Disable SSL mode on Neon connections
---
## π Production Deployment
### HuggingFace Spaces
All environment variables are automatically loaded from **Repository secrets**.
### Update OAuth redirect URIs
After deploying, update all OAuth apps with production callback URLs:
- HuggingFace: `https://www.communityone.com/api/auth/callback/huggingface`
- Google: `https://www.communityone.com/api/auth/callback/google`
- Facebook: `https://www.communityone.com/api/auth/callback/facebook`
- GitHub: `https://www.communityone.com/api/auth/callback/github`
---
## π Troubleshooting
### Database connection fails
```
β could not connect to server
```
**Fix:**
- Verify `DATABASE_URL` is correct
- Check Neon project is not suspended (free tier auto-suspends after inactivity)
- Ensure `?sslmode=require` is in connection string
### OAuth redirect mismatch
```
β redirect_uri_mismatch
```
**Fix:**
- Check redirect URI in OAuth app settings matches your server
- Ensure `http://` vs `https://` matches
- Verify port number (`:8000` for API)
### JWT token invalid
```
β Could not validate credentials
```
**Fix:**
- Ensure `JWT_SECRET_KEY` is set and matches between sessions
- Check token hasn't expired (7-day default)
- Clear browser localStorage and re-login
### Tables not created
```
β relation "users" does not exist
```
**Fix:**
- Restart API server to trigger `init_db()`
- Check database connection is successful
- Manually run: `from api.database import init_db; init_db()`
---
## π Additional Resources
- [Neon Documentation](https://neon.tech/docs)
- [OAuth 2.0 RFC](https://datatracker.ietf.org/doc/html/rfc6749)
- [JWT Best Practices](https://datatracker.ietf.org/doc/html/rfc8725)
- [FastAPI Security](https://fastapi.tiangolo.com/tutorial/security/)
---
## β
Checklist
Before going live, ensure:
- [ ] Neon database created and connected
- [ ] All 4 OAuth apps configured with production redirect URIs
- [ ] JWT secret generated (32+ characters)
- [ ] Environment variables added to HuggingFace Spaces secrets
- [ ] Database tables created (`users`, `oauth_states`)
- [ ] OAuth flows tested with all 4 providers
- [ ] HTTPS enabled on custom domain
- [ ] Neon IP allowlist configured (optional, for extra security)
---
**Need help?** Open an issue on [GitHub](https://github.com/getcommunityone/open-navigator-for-engagement/issues)
|