Build A BFF/Proxy
Doc type: How-to guide
Goal and scope
Create a small server-side layer that reads HttpOnly cookies and forwards the access token to downstream APIs.
Prerequisites and constraints
- The BFF runs on the same origin as the browser app.
- The BFF does not expose refresh tokens to the client.
Steps
-
Read the access token from the shared cookie.
-
Forward it in the
Authorizationheader. -
Return
401if the session is missing or expired.
export async function GET(request: Request) {
const accessToken = request.cookies.get('yoyo_auth_access_token')?.value;
if (!accessToken) {
return new Response(null, { status: 401 });
}
const response = await fetch('https://api.yoyogroup.com/resource', {
headers: { Authorization: `Bearer ${accessToken}` }
});
return new Response(await response.text(), {
status: response.status,
headers: { 'Content-Type': response.headers.get('Content-Type') || 'application/json' }
});
}
Validation
- When the session exists, downstream API calls succeed.
- When the session is missing, the BFF returns
401and the client redirects to hosted auth.