Overview
Study Sync uses Firebase Authentication for securing API endpoints. All authenticated requests must include a valid Firebase ID token in theAuthorization header.
Authentication Methods
Firebase ID Token
The API uses Firebase ID tokens for authentication. These tokens are obtained from Firebase Authentication after a user signs in.Header Format
Getting a Firebase ID Token
Using Firebase SDK (Client-side)
Token Refresh
Firebase ID tokens expire after 1 hour. The Firebase SDK automatically refreshes tokens, but you can manually refresh:Making Authenticated Requests
cURL Example
JavaScript Fetch Example
Axios Example
Authentication Flow
- User signs in via Firebase Authentication (email/password, Google, etc.)
- Firebase returns an ID token
- Client includes token in
Authorizationheader for API requests - API verifies token using Firebase Admin SDK
- If token is valid, API processes the request
- If user doesn’t exist in database, API auto-creates user profile
User Auto-Creation
When a valid Firebase token is provided for a new user, the API automatically creates a user profile with:Firebase user ID from the token
User’s email address
Display name (from Firebase or extracted from email)
Profile photo URL (if available from Firebase)
User role, defaults to
"user"Optional Authentication
Some endpoints use optional authentication, where:- Public content is accessible without a token
- Additional features/data are available with authentication
- No error is returned for missing/invalid tokens
GET /api/study-plans(public plans viewable without auth)GET /api/study-plans/:id(public plans accessible without auth)
Authentication Errors
401 Unauthorized
Returned when authentication is required but missing or invalid. Missing Token:403 Forbidden
Returned when authenticated but lacking permission to access the resource.Security Best Practices
- Never expose tokens - Don’t log or store tokens in plain text
- Use HTTPS - Always use secure connections in production
- Refresh tokens - Implement automatic token refresh before expiration
- Handle errors - Properly handle 401/403 errors and redirect to login
- Validate on client - Check authentication state before making requests