Starter Templates
Jumpstart your API gateway development process with our pre-built starter templates
Backend for Frontend (BFF) Auth
Optimize UX and security in web applications with this approach.
npx create-zuplo-api --example bff-authBackend for Frontend (BFF) Authentication
This example implements the Backend for Frontend (BFF) authentication pattern, which optimizes security and user experience for web applications by keeping tokens server-side and using secure HTTP-only cookies for session management.
Instead of exposing access tokens to the browser, the BFF pattern stores tokens securely on the server and uses session cookies to identify users. This protects against XSS attacks and token theft.
Prerequisites
- A Zuplo account. You can sign up for free.
- An Auth0 account (or another OAuth identity provider)
- An Upstash Redis account for session storage
Working with this Example
Locally
Working locally is the best way to explore and understand the code for this example. You can get a local version by using the Zuplo CLI:
Then, in the project directory run the following commands:
Deploy this example to Zuplo
It is also possible to deploy this example directly to your Zuplo account and work with it via the Zuplo Portal. You can do this by clicking the Deploy to Zuplo button anywhere on this page.
Environment Variables
Set these in your .env file locally or in the Zuplo Portal under Settings > Environment Variables:
How It Works
Authentication Flow
- Login: User visits
/auth/login, gets redirected to Auth0 - Callback: Auth0 redirects to
/auth/callbackwith authorization code - Token Exchange: BFF exchanges code for tokens with Auth0
- Session Storage: Tokens stored in Upstash Redis with session ID
- Cookie: Session ID returned to browser as HTTP-only cookie
- API Calls: Frontend calls
/bff/tokento get access token for API requests
Endpoints
| Endpoint | Description |
|---|---|
/auth/login | Redirects user to Auth0 login page |
/auth/callback | OAuth callback - exchanges code for tokens |
/auth/logout | Clears session and cookie |
/bff/token | Returns access token for authenticated requests |
/bff/sessioninfo | Returns user profile information |
/app | Sample protected application page |
Security Features
- HTTP-only cookies: Session cookies cannot be accessed by JavaScript
- Secure flag: Cookies only sent over HTTPS
- SameSite=Strict: Prevents CSRF attacks
- Server-side token storage: Access tokens never exposed to browser
- Token refresh: Automatic refresh using refresh tokens
Project Structure
Testing the Flow
- Start the dev server:
-
Open your browser to
http://localhost:9000/auth/login -
Log in with Auth0
-
After redirect, check the session info:
- Get an access token for API calls:
Frontend Integration
Your frontend can use the BFF endpoints to manage authentication:
Common Customizations
- Different identity provider: Update the OAuth URLs and token exchange logic
- Different session storage: Replace Upstash with Redis, DynamoDB, or another store
- Add claims to session: Store additional user data from the identity provider
- Customize cookie settings: Adjust expiration, domain, or path as needed
Learn More
Other Examples
Jumpstart your API gateway development process with our pre-built starter templates