Skip to content

feat: user based feature flag integration#173

Open
Alessandro100 wants to merge 20 commits into
mainfrom
feat/140-user-based-feature-flag-integration
Open

feat: user based feature flag integration#173
Alessandro100 wants to merge 20 commits into
mainfrom
feat/140-user-based-feature-flag-integration

Conversation

@Alessandro100

@Alessandro100 Alessandro100 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary:

closes #140

Enables user based feature flags for use in server and client components.

It was done in a way where its easy to add and consume feature flags. It's also done in a way where it should be easy to replace the firebase remote configs gradually

For a more detailed explanation on how it works, how to use it, and the options considered check the doc that was added in docs/user-feature-flags.md

Expected behavior:

Scenarios

On login

  • fetch the user feature flags GET /v1/user
  • create a (signed httpOnly) cookie with featureFlags - server access
  • broadcast featureFlags to Context (and tabs) - client access

On logout

  • Context is cleared - client
  • Cookie is deleted - server

On token renewal (~60 mins)

  • fetch the user feature flags GET /v1/user
  • replace the (signed httpOnly) cookie with featureFlags - server access
  • broadcast featureFlags to replace Context (and tabs) - client access

Testing tips:

For the review of this PR I added an element to the Header to display the value of a user based feature flag
Go to Retool and set isNotificationsEnabled to true for the account you plan on using, then in the preview url check that the value changes based on your login

NOTE: If you are already logged, it's possible that the feature flags will be delayed up to 1 hour (tokenRenewal), for best testing, logout then login. Or you can wait 1 hour

Please make sure these boxes are checked before submitting your pull request - thanks!

  • Run the unit tests with yarn test to make sure you didn't break anything
  • Add or update any needed documentation to the repo
  • Format the title like "feat: [new feature short description]". Title must follow the Conventional Commit Specification(https://www.conventionalcommits.org/en/v1.0.0/).
  • Linked all relevant issues
  • Include screenshot(s) showing how this pull request works and fixes the issue(s)

@Alessandro100
Alessandro100 requested a review from Copilot July 22, 2026 19:46
@Alessandro100 Alessandro100 self-assigned this Jul 22, 2026
@vercel

vercel Bot commented Jul 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
mobilitydatabase-web Ready Ready Preview, Comment Jul 23, 2026 1:09pm

Request Review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Integrates user-based feature flags into the Next.js app so flags are available to both Server Components (via a signed httpOnly cookie read in a server action) and Client Components (via a React provider kept in sync across tabs with BroadcastChannel).
custom

Changes:

  • Add a new user feature flag model (UserFeatureFlags + defaults) and SSR hydration via getServerFlags() passed into <Providers>.
  • On login / provider login / signup, fetch GET /v1/user and persist/broadcast flags via POST /api/feature-flags; on logout clear flags across tabs and delete cookies.
  • Add API type/schema updates, documentation, and Cypress E2E coverage for the feature-flag cookie + provider state transitions.

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/app/types.ts Extends UserData to include features returned by the user service.
src/app/store/saga/profile-saga.ts Adjusts token refresh saga typing (feature flags handled elsewhere).
src/app/store/saga/auth-saga.ts Applies feature flags after login/signup/provider login and resets defaults on logout (cross-tab).
src/app/services/user-service-api-types.ts Updates generated OpenAPI types to include features and new subscription endpoints/docs.
src/app/services/session-service.ts Adds feature-flag refresh/apply helpers and returns renewal signal from session cookie establishment.
src/app/services/profile-service.ts Maps features from the user profile API into UserData.
src/app/services/channel-service.ts Adds typed channel support + a helper that also delivers to the sender tab.
src/app/providers.tsx Wires UserFeatureFlagProvider into the provider tree with SSR initial flags.
src/app/interface/UserFeatureFlags.ts Defines typed flag map, defaults, and conversion from API array form.
src/app/context/UserFeatureFlagProvider.tsx New client provider + hook, sync via BroadcastChannel, and Cypress exposure hook.
src/app/components/AuthSessionProvider.tsx Refreshes feature flags on session renewal cadence.
src/app/api/session/route.ts Clears the feature-flag cookie along with the session cookie on logout.
src/app/api/feature-flags/route.ts New route to HMAC-sign and set the md_features cookie.
src/app/actions/feature-flags.ts New server action to read + verify the signed feature-flag cookie for SSR/server usage.
src/app/[locale]/layout.tsx Hydrates user feature flags server-side and passes them into <Providers>.
package.json Updates OpenAPI type generation scripts for user service + GBFS validator types.
external_types/UserServiceAPI.yaml Updates the source OpenAPI spec to include user features + subscription endpoints.
docs/user-feature-flags.md Adds architecture and usage documentation for user feature flags.
cypress/e2e/user-feature-flags.cy.ts Adds E2E tests for cookie set/clear and provider state sync on login/logout/renewal.

Comment thread src/app/interface/UserFeatureFlags.ts
Comment on lines +58 to +61
const encoded = cookieValue.split('.')[0];
// base64url → standard base64 before atob()
const base64 = encoded.replace(/-/g, '+').replace(/_/g, '/');
return JSON.parse(atob(base64));
Comment thread docs/user-feature-flags.md Outdated
@github-actions

Copy link
Copy Markdown

*Lighthouse ran on https://mobilitydatabase-o63u5eo7c-mobility-data.vercel.app/ * (Desktop)
⚡️ HTML Report Lighthouse report for the changes in this PR:

Performance Accessibility Best Practices SEO
🟢 90 🟢 94 🟢 96 🟢 100

*Lighthouse ran on https://mobilitydatabase-o63u5eo7c-mobility-data.vercel.app/feeds * (Desktop)
⚡️ HTML Report Lighthouse report for the changes in this PR:

Performance Accessibility Best Practices SEO
🟠 80 🟠 87 🟢 96 🟢 100

*Lighthouse ran on https://mobilitydatabase-o63u5eo7c-mobility-data.vercel.app/feeds/gtfs/mdb-2126 * (Desktop)
⚡️ HTML Report Lighthouse report for the changes in this PR:

Performance Accessibility Best Practices SEO
🔴 34 🟢 94 🟢 96 🟢 100

*Lighthouse ran on https://mobilitydatabase-o63u5eo7c-mobility-data.vercel.app/feeds/gtfs_rt/mdb-2585 * (Desktop)
⚡️ HTML Report Lighthouse report for the changes in this PR:

Performance Accessibility Best Practices SEO
🟠 87 🟠 84 🟢 96 🟢 100

*Lighthouse ran on https://mobilitydatabase-o63u5eo7c-mobility-data.vercel.app/feeds/gbfs/gbfs-flamingo_porirua * (Desktop)
⚡️ HTML Report Lighthouse report for the changes in this PR:

Performance Accessibility Best Practices SEO
🟢 95 🟢 94 🟢 96 🟢 100

@davidgamez davidgamez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

displayName: userDisplayName,
} = useAuthSession();
// TO REMOVE: for PR testing
const { isNotificationsEnabled } = useUserFeatureFlags(); // Ensure feature flags are loaded for SSR hydration

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove before merging

Comment on lines +275 to +276
TO REMOVE: is notification enabled:{' '}
{isNotificationsEnabled.toString()}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove before merging

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.

[user-service: web] API-based feature flag UI migration

3 participants