Skip to content

feat(example-fastify-web-call-api): add web app calling an API example#88

Open
frederikprijck wants to merge 3 commits into
mainfrom
feat/example-fastify-web-call-api
Open

feat(example-fastify-web-call-api): add web app calling an API example#88
frederikprijck wants to merge 3 commits into
mainfrom
feat/example-fastify-web-call-api

Conversation

@frederikprijck

@frederikprijck frederikprijck commented Jul 10, 2026

Copy link
Copy Markdown
Member

Description

Adds a runnable example-fastify-web-call-api example. It demonstrates a Fastify web app that:

  1. Logs the user in via @auth0/auth0-fastify.
  2. Requests an access token for the configured audience (obtained at login).
  3. Calls a separate resource server (example-fastify-api) on the user's behalf using fastify.auth0Client.getAccessToken({ request, reply }), then renders the response at /call-api.

Supporting changes

  • example-fastify-api: added PORT env support (was hardcoded to 3000) so it can run on 3001 alongside this web app on 3000 — matching what example-express-api already does.
  • Root README.md and llms.txt: linked the new example.

Summary by CodeRabbit

  • New Features

    • Added a Fastify web app example demonstrating Auth0 login, session-protected pages, access-token retrieval, and calls to a protected API.
    • Added public/private/call-API views and support for a configurable listening port (default: 3000).
  • Documentation

    • Added example documentation, environment variable guidance, and an .env.example.
    • Linked the new example from the main project docs.
  • Tests

    • Added automated integration tests covering login redirects, authenticated API calls, and unauthenticated access behavior.
  • CI

    • Added a GitHub Actions workflow to build and run the example’s CI tests.

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: d43987d2-5c96-4b72-a2ca-16c24af079c6

📥 Commits

Reviewing files that changed from the base of the PR and between bb69fd1 and 6ce7b34.

📒 Files selected for processing (2)
  • examples/example-fastify-web-call-api/src/index.ts
  • examples/example-fastify-web-call-api/views/layout.ejs
🚧 Files skipped from review as they are similar to previous changes (2)
  • examples/example-fastify-web-call-api/views/layout.ejs
  • examples/example-fastify-web-call-api/src/index.ts

📝 Walkthrough

Walkthrough

Changes

A new Fastify web example authenticates users with Auth0, obtains access tokens, calls a protected API, renders the result, and includes integration tests, environment configuration, documentation, and CI coverage.

Fastify web API example

Layer / File(s) Summary
Application foundation
examples/example-fastify-web-call-api/package.json, tsconfig.json, .env.example, src/index.ts
Adds the example package, TypeScript configuration, environment template, Auth0 integration, view setup, and exported buildApp().
Authenticated routes and views
examples/example-fastify-web-call-api/src/index.ts, views/*
Adds public, private, login-protected, and API-calling routes with corresponding EJS templates.
Integration validation
examples/example-fastify-web-call-api/src/index.spec.ts
Mocks Auth0 and the resource server, preserves session cookies, and tests redirects, token forwarding, and rendered API responses.
Example integration and documentation
examples/example-fastify-api/src/index.ts, .github/workflows/examples.yml, README.md, llms.txt, examples/example-fastify-web-call-api/README.md
Adds configurable API-server ports, CI execution, and links and instructions for running the new example.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Browser
  participant FastifyApp
  participant Auth0
  participant ResourceServer
  Browser->>FastifyApp: Request /call-api
  FastifyApp->>Auth0: Check session and request access token
  Auth0-->>FastifyApp: Return access token
  FastifyApp->>ResourceServer: GET /api/private with Bearer token
  ResourceServer-->>FastifyApp: Return protected response
  FastifyApp-->>Browser: Render API response
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding a Fastify web app example that calls an API.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/example-fastify-web-call-api

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@frederikprijck frederikprijck force-pushed the feat/example-fastify-web-call-api branch from b136121 to bf3aa54 Compare July 10, 2026 09:54
@frederikprijck frederikprijck changed the base branch from main to ci/build-examples July 10, 2026 10:20
@frederikprijck frederikprijck force-pushed the feat/example-fastify-web-call-api branch from 19da61c to 75f5422 Compare July 10, 2026 10:20
@frederikprijck frederikprijck changed the base branch from ci/build-examples to main July 10, 2026 15:20
Add a runnable example mirroring the express example-express-web-call-api:
a Fastify web app that logs the user in, requests an access token for the
configured audience, and calls the example-fastify-api resource server on
the user's behalf via getAccessToken().

Also add PORT env support to example-fastify-api so it can run on 3001
alongside this web app on 3000, and link the new example from the root
README and llms.txt.
Add a vitest suite for the web-call-api example that mocks Auth0
(discovery, JWKS, token) and the downstream API with msw, then drives the
full login and /call-api flow via fastify.inject() — no live services.

Refactor the example into an exported buildApp() factory so the app can be
driven by the test without binding a port, starting the server only when
run directly.

Add a 'Build and Test Examples' workflow that builds each example (and runs
the web-call-api tests), mirroring the auth0-express examples CI. Also add a
build script to example-fastify-api so CI can verify it compiles.
@frederikprijck frederikprijck force-pushed the feat/example-fastify-web-call-api branch from 75f5422 to bb69fd1 Compare July 10, 2026 15:21

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@examples/example-fastify-web-call-api/src/index.ts`:
- Around line 61-67: Update hasSessionPreHandler so the unauthenticated branch
returns the result of reply.redirect, and wrap request.url with
encodeURIComponent before interpolating it into the returnTo query parameter.

In `@examples/example-fastify-web-call-api/views/layout.ejs`:
- Around line 24-47: Update the layout navigation links to determine the current
route and conditionally apply the active class and aria-current="page" only to
the matching item. Pass the current route into the EJS template from the
relevant render handlers and use it for the links in the layout template.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: d35a5099-899c-4027-9d91-4bfe0cce6836

📥 Commits

Reviewing files that changed from the base of the PR and between c6a09f5 and bb69fd1.

⛔ Files ignored due to path filters (1)
  • examples/example-fastify-web-call-api/public/img/auth0.png is excluded by !**/*.png
📒 Files selected for processing (15)
  • .github/workflows/examples.yml
  • README.md
  • examples/example-fastify-api/src/index.ts
  • examples/example-fastify-web-call-api/.env.example
  • examples/example-fastify-web-call-api/README.md
  • examples/example-fastify-web-call-api/package.json
  • examples/example-fastify-web-call-api/src/index.spec.ts
  • examples/example-fastify-web-call-api/src/index.ts
  • examples/example-fastify-web-call-api/tsconfig.json
  • examples/example-fastify-web-call-api/views/api.ejs
  • examples/example-fastify-web-call-api/views/index.ejs
  • examples/example-fastify-web-call-api/views/layout.ejs
  • examples/example-fastify-web-call-api/views/private.ejs
  • examples/example-fastify-web-call-api/views/public.ejs
  • llms.txt

Comment thread examples/example-fastify-web-call-api/src/index.ts
Comment thread examples/example-fastify-web-call-api/views/layout.ejs
- return reply.redirect and encodeURIComponent(request.url) in hasSessionPreHandler
- drop hardcoded active/aria-current from nav links
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant