Overview
The Macondo Link Manager API uses a combination of Google OAuth 2.0 for login and JWT (JSON Web Tokens) for session management. Authentication is cookie-based, providing secure, stateless authentication.Authentication Flow
1. Google OAuth Login
Users authenticate via Google OAuth 2.0:- Redirect user to
/auth/google - User authorizes with Google
- Google redirects to
/auth/google/callback - API validates user and sets JWT cookie
- User is redirected to frontend
Authentication Methods
Google OAuth 2.0
Only users with email addresses from the allowed domain can authenticate. Domain restrictions are enforced server-side.
Initiate Login
Redirect users to:OAuth Callback
- Exchanges authorization code for access token
- Fetches user profile from Google
- Validates user email domain
- Creates or updates user in database
- Generates JWT token
- Sets secure cookie
- Redirects to frontend
email: Access to user’s email addressprofile: Access to basic profile information (name, picture)
JWT Token
After successful Google authentication, a JWT is generated and stored in a secure cookie. Cookie Name:macondo.token
Token Payload:
- Expiration: 7 days
- Algorithm: HS256 (default for @fastify/jwt)
- Storage: HTTP-only cookie
- Path:
/ - SameSite:
none(cross-origin support) - Secure:
true(HTTPS only)
Cookie Configuration
Protected Routes
Most API endpoints require authentication via theauthHook middleware.
Authentication Hook
TheauthHook validates the JWT token on every protected request:
Location: /home/daytona/workspace/source/api/src/hooks/auth.ts:5
Process:
- Extract
macondo.tokencookie from request - Verify JWT signature and expiration
- Decode token payload
- Attach user data to
request.user - Allow request to proceed
- All
/linksendpoints - All
/clientsendpoints - All
/campaignsendpoints - All
/dashboardendpoints /meendpoint
Making Authenticated Requests
Include the JWT cookie in all requests to protected endpoints:Authentication Errors
Missing Token
Status:401 Unauthorized
macondo.token cookie is present in the request.
Invalid or Expired Token
Status:401 Unauthorized
- Token signature is invalid
- Token has expired (> 7 days old)
- Token format is malformed
Domain Not Allowed
During login, if the user’s email domain is not allowed: Redirect: Frontend with error parameterGet Current User
Retrieve information about the authenticated user:Response (200)
Example
Logout
Log out by clearing the authentication cookie:Response (200)
Example
Security Best Practices
Token Security
- HTTP-Only: Tokens are not accessible via JavaScript
- Secure Flag: Tokens only transmitted over HTTPS
- SameSite: Cross-origin protection
- Short Expiration: Tokens expire after 7 days
- Server-Side Validation: All tokens verified on every request
Domain Restrictions
The API enforces email domain restrictions during OAuth login. If a user with an unauthorized domain attempts to log in, they will be redirected with an error. Error handling in frontend:CORS and Credentials
- Origin: Restricted to configured frontend URL
- Credentials: Must be included in all cross-origin requests
- Methods: Limited to
GET,POST,PUT,DELETE,OPTIONS
Environment Configuration
Required environment variables:The
JWT_SECRET should be a strong, randomly generated string. Never commit this value to version control.Troubleshooting
Token Not Sent with Request
Problem: Requests to protected endpoints fail with 401 error Solutions:- Ensure
credentials: 'include'(fetch) orwithCredentials: true(axios) - Check that cookie is set after successful login
- Verify CORS configuration allows credentials
- Confirm frontend and API URLs match CORS settings
Token Expires Too Quickly
Problem: Users are logged out unexpectedly Solution: Tokens expire after 7 days. Implement a “keep me logged in” flow or prompt users to re-authenticate when tokens expire.Cross-Origin Cookie Issues
Problem: Cookie not set in cross-origin scenarios Solutions:- Ensure API uses
sameSite: 'none'andsecure: true - Verify both frontend and API use HTTPS
- Check browser settings allow third-party cookies
- Confirm
FRONTEND_URLenvironment variable matches actual frontend URL
Next Steps
Create Links
Start creating shortened links
Manage Clients
Organize your agency clients
View Analytics
Track link performance
User Profile
Access user information
