Skip to content

feat: Cookie auth improvements and cached auth state#356

Merged
jaredwray merged 14 commits intomainfrom
half-ogre/features-for-hyphen
Mar 16, 2026
Merged

feat: Cookie auth improvements and cached auth state#356
jaredwray merged 14 commits intomainfrom
half-ogre/features-for-hyphen

Conversation

@half-ogre
Copy link
Collaborator

This PR:

  • Adds authCheckUrl-based authentication that fetches a configurable URL with credentials: 'include' to determine login state, replacing the previous cookie-name approach
  • Caches auth state in localStorage and applies it in a script before the body renders, eliminating UI flash on subsequent page loads
  • Shares auth state globally via window.__doculaAuth and a docula-auth-change custom event so the API reference page can show login status without a separate auth check
  • Persists the API reference authorization type selection in localStorage across page refreshes
  • Wraps cookie auth controls and theme toggle in a header-actions container for proper right-alignment

New config options

  • authCheckUrl — URL to fetch (with credentials) to check if the user is logged in
  • authCheckMethod — HTTP method for the auth check (default: GET)
  • authCheckUserPath — dot-notation path to extract a display name from the JSON response

Test plan

  • 4 new tests added covering header-actions wrapper, cached auth state in head script, inline username script, and no inline display:none styles
  • All 344 existing tests pass
  • Manual test: first visit shows login button, auth check runs in background
  • Manual test: after login, subsequent page loads show logout button and username instantly from cache
  • Manual test: API reference persists authorization type selection across refreshes
  • Manual test: cookie auth status badge in API reference reflects login state without delay

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly upgrades Docula's cookie-based authentication system by moving from a simple cookie name check to a more robust URL-based authentication verification. The primary goal is to enhance user experience by preventing UI flashes during page loads through client-side caching of authentication states. This change also improves the flexibility of integrating with various authentication providers and streamlines the display of user information and API authorization status across the documentation site.

Highlights

  • Enhanced Cookie Authentication: Replaced the cookieName-based authentication with a more flexible authCheckUrl mechanism, allowing Docula to determine login state by fetching a configurable URL with credentials.
  • Cached Authentication State: Implemented client-side caching of authentication state in localStorage, which is applied via a <head> script to eliminate UI flash on subsequent page loads and provide an instant user experience.
  • Global Auth State Sharing: Introduced a global window.__doculaAuth object and a docula-auth-change custom event to share authentication state across the application, enabling components like the API reference page to react to login status without separate checks.
  • API Reference Improvements: Persisted the API reference authorization type selection in localStorage across page refreshes and updated the cookie auth status badge to reflect the global authentication state.
  • Header Layout Refinement: Wrapped cookie authentication controls and the theme toggle in a new header-actions container, improving their right-alignment and overall header layout.
  • New Configuration Options: Added new configuration options for cookieAuth: authCheckUrl, authCheckMethod, and authCheckUserPath, providing greater control over the authentication check process and user display name extraction.
Changelog
  • site/docs/cookie-auth.md
    • Updated documentation to reflect the new authCheckUrl-based authentication method.
    • Removed references to cookieName and added details for new configuration options: authCheckUrl, authCheckMethod, and authCheckUserPath.
    • Added explanations for cached auth state and user display name extraction.
  • src/builder.ts
    • Modified the DoculaData type to remove cookieName and include authCheckUrl, authCheckMethod, and authCheckUserPath within the cookieAuth configuration.
  • src/options.ts
    • Updated the DoculaCookieAuth type definition to remove cookieName and add authCheckUrl, authCheckMethod, and authCheckUserPath.
  • templates/modern/api.hbs
    • Added inline JavaScript to persist the selected API authorization type in localStorage.
    • Updated the API auth status badge logic to use the global window.__doculaAuth state for displaying login status.
  • templates/modern/css/styles.css
    • Introduced new CSS classes .header-actions and .cookie-auth for improved layout and styling of header elements.
    • Adjusted .theme-button styles by removing margin-left: auto.
    • Modified .cookie-auth-user:empty to use display: none !important for consistent hiding.
  • templates/modern/home.hbs
    • Removed inline style="display:none" from the logout button, deferring visibility control to CSS classes.
  • templates/modern/includes/header-bar.hbs
    • Wrapped cookie authentication elements and the theme toggle in a new div with class header-actions.
    • Added an inline script to display the username from window.__doculaAuth.
    • Removed inline style="display:none" from auth elements, relying on CSS for visibility.
  • templates/modern/includes/header.hbs
    • Injected an inline script into the <head> to load and apply cached authentication state from localStorage to window.__doculaAuth and set a docula-auth-logged-in class on the documentElement.
  • templates/modern/includes/scripts.hbs
    • Updated the cookie-auth-config element to pass new authCheckUrl, authCheckMethod, and authCheckUserPath attributes.
    • Refactored client-side JavaScript to remove cookieName logic and implement authCheckUrl fetching.
    • Introduced getNestedValue for extracting user data from JSON responses.
    • Implemented setAuthUI to manage global auth state, localStorage caching, and dispatch docula-auth-change events.
    • Modified DOMContentLoaded listener to prioritize cached auth state and then refresh via checkAuth.
  • templates/modern/js/api.js
    • Updated updateAuthUI function to retrieve authentication status from window.__doculaAuth.
    • Added event listeners to persist API authorization type selection in localStorage and react to docula-auth-change events.
  • test/builder.test.ts
    • Updated existing tests to remove cookieName from cookieAuth configurations.
    • Added new tests to verify the header-actions wrapper, cached auth state in the head script, inline username script rendering, absence of inline display:none styles, and correct rendering of authCheckUrl configuration.
  • test/options.test.ts
    • Adjusted tests for DoculaOptions to reflect the removal of cookieName and the addition of authCheckUrl, authCheckMethod, and authCheckUserPath.
Activity
  • Four new tests were added covering the header-actions wrapper, cached auth state in the head script, inline username script, and the removal of inline display:none styles.
  • All 344 existing tests continue to pass, ensuring no regressions.
  • Manual testing confirmed that the login button appears on the first visit, with the auth check running in the background.
  • Manual testing verified that subsequent page loads instantly display the logout button and username from cache after login.
  • Manual testing confirmed that the API reference persists authorization type selection across refreshes.
  • Manual testing validated that the cookie auth status badge in the API reference accurately reflects the login state without delay.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the cookie-based authentication system to use a configurable authCheckUrl for determining login status, replacing the previous JWT cookie name check. The changes include updating documentation, configuration types, and various Handlebars templates to support the new fetch-based authentication, introduce cached auth state in localStorage to prevent UI flickering, and allow for extracting a user display name from the authCheckUrl response. Review comments suggest improving code readability and maintainability by combining and formatting inline JavaScript scripts in api.hbs, header-bar.hbs, and header.hbs, and addressing the use of !important in styles.css by refactoring JavaScript to toggle classes instead of inline styles.

Comment on lines 150 to 152
.cookie-auth-user:empty {
display: none;
display: none !important;
}

Choose a reason for hiding this comment

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

medium

The use of !important is generally discouraged as it can lead to maintenance issues and difficulty in overriding styles. This rule is likely necessary because the accompanying JavaScript is setting inline style attributes, which have higher specificity than class-based styles. By refactoring the JavaScript to only toggle classes on a parent element (as suggested in another comment for templates/modern/includes/scripts.hbs), this !important rule can be removed.

Suggested change
.cookie-auth-user:empty {
display: none;
display: none !important;
}
.cookie-auth-user:empty {
display: none;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

From Claude:

The !important is intentional here. .docula-auth-logged-in .cookie-auth-user (line 127) sets display: inline with specificity 0,2,0. .cookie-auth-user:empty also has specificity 0,2,0, but it appears after the logged-in rule, so it only wins by source order. That's fragile — any future reordering of the CSS would break it. The !important makes the intent explicit: an empty username element should never be visible regardless of auth state.

There are no inline styles being set on this element — the JS sets textContent, not style. So the premise of the suggestion (that !important is fighting inline styles) doesn't apply here.

@codecov
Copy link

codecov bot commented Mar 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (d16d35b) to head (a82bfbc).
⚠️ Report is 15 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #356   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         7           
  Lines         1818      1818           
  Branches       514       520    +6     
=========================================
  Hits          1818      1818           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jaredwray jaredwray merged commit 9ca2d62 into main Mar 16, 2026
9 checks passed
@jaredwray jaredwray deleted the half-ogre/features-for-hyphen branch March 16, 2026 15:01
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.

2 participants