Skip to content

feat: implement runtime authentication handling#1517

Open
AnishSarkar22 wants to merge 1 commit into
MODSetter:mainfrom
AnishSarkar22:hotpatch/docker-auth-type
Open

feat: implement runtime authentication handling#1517
AnishSarkar22 wants to merge 1 commit into
MODSetter:mainfrom
AnishSarkar22:hotpatch/docker-auth-type

Conversation

@AnishSarkar22

@AnishSarkar22 AnishSarkar22 commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator
  • Added a new proxy function to manage runtime authentication types and set cookies accordingly.
  • Introduced runtime authentication configuration to dynamically adjust UI based on the selected auth type.
  • Updated global styles to hide specific authentication buttons based on the current auth type.
  • Refactored sign-in button and hero section components to utilize the new runtime authentication logic.
  • Created a new utility file for runtime authentication configuration and initialization script.

Description

Motivation and Context

FIX #

Screenshots

API Changes

  • This PR includes API changes

Change Type

  • Bug fix
  • New feature
  • Performance improvement
  • Refactoring
  • Documentation
  • Dependency/Build system
  • Breaking change
  • Other (specify):

Testing Performed

  • Tested locally
  • Manual/QA verification

Checklist

  • Follows project coding standards and conventions
  • Documentation updated as needed
  • Dependencies updated as needed
  • No lint/build errors or new warnings
  • All relevant tests are passing

High-level PR Summary

This PR implements a dynamic runtime authentication system that allows switching between Google OAuth and local authentication modes without rebuilding the application. It introduces a cookie-based mechanism (surfsense_auth_type) to store the authentication preference, uses a client-side initialization script to apply the correct UI mode before hydration, and leverages CSS data attributes with runtime-specific classes (runtime-auth-google and runtime-auth-local) to conditionally show/hide authentication buttons throughout the application. The changes refactor authentication-related components to render both button types simultaneously while CSS handles the visibility logic.

⏱️ Estimated Review Time: 5-15 minutes

💡 Review Order Suggestion
Order File Path
1 surfsense_web/lib/runtime-auth-config.ts
2 surfsense_web/proxy.ts
3 surfsense_web/app/layout.tsx
4 surfsense_web/app/globals.css
5 surfsense_web/components/auth/sign-in-button.tsx
6 surfsense_web/components/homepage/hero-section.tsx

Need help? Join our Discord

Summary by CodeRabbit

  • New Features

    • Authentication method can now be configured at runtime through cookies instead of at build time, providing greater flexibility.
    • Sign-in interface now displays both Google and local authentication options simultaneously for user choice.
  • Refactor

    • Restructured authentication initialization to support dynamic auth configuration.

- Added a new proxy function to manage runtime authentication types and set cookies accordingly.
- Introduced runtime authentication configuration to dynamically adjust UI based on the selected auth type.
- Updated global styles to hide specific authentication buttons based on the current auth type.
- Refactored sign-in button and hero section components to utilize the new runtime authentication logic.
- Created a new utility file for runtime authentication configuration and initialization script.
@vercel

vercel Bot commented Jun 18, 2026

Copy link
Copy Markdown

@AnishSarkar22 is attempting to deploy a commit to the Rohan Verma's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

A new runtime auth configuration system is introduced. A runtime-auth-config.ts module defines a RuntimeAuthUiMode type and helpers to resolve auth mode and generate a cookie-reading init script. A Next.js edge middleware (proxy.ts) writes the auth-type cookie on responses. The root layout injects a beforeInteractive script and sets data-surfsense-auth-type on <html>. CSS rules and refactored UI components use this attribute to show or hide Google vs. local auth elements at runtime.

Changes

Runtime Auth Mode Detection and UI Toggling

Layer / File(s) Summary
Runtime auth config module
surfsense_web/lib/runtime-auth-config.ts
Defines RuntimeAuthUiMode ("GOOGLE" | "LOCAL"), RUNTIME_AUTH_TYPE_COOKIE_NAME, resolveRuntimeAuthUiMode normalizer, getRuntimeAuthInitScript IIFE generator, and Window.__SURFSENSE_AUTH_TYPE__ type augmentation.
Edge middleware proxy sets auth cookie
surfsense_web/proxy.ts
Exports a Next.js edge handler that resolves the auth UI mode from process.env.AUTH_TYPE and build-time fallback, then sets the surfsense_auth_type cookie (1-year, sameSite: lax, HTTPS-conditional secure) on every matched response.
Layout script injection and CSS visibility rules
surfsense_web/app/layout.tsx, surfsense_web/app/globals.css
layout.tsx adds a beforeInteractive Script that runs getRuntimeAuthInitScript and sets data-surfsense-auth-type on <html>. globals.css adds attribute selectors to display: none .runtime-auth-local when GOOGLE is active and .runtime-auth-google when LOCAL is active.
Auth UI components render both options unconditionally
surfsense_web/components/auth/sign-in-button.tsx, surfsense_web/components/homepage/hero-section.tsx
Removes isGoogleAuth build-time branching from both components. Both now always render the Google sign-in button and the local login link, applying runtime-auth-google / runtime-auth-local class names for CSS-driven visibility.

Sequence Diagram(s)

sequenceDiagram
  actor User
  participant Browser
  participant EdgeMiddleware as proxy (Edge Middleware)
  participant Cookie as surfsense_auth_type cookie
  participant InitScript as beforeInteractive Script
  participant HTML as document.documentElement
  participant CSS as globals.css rules

  User->>Browser: navigate to page
  Browser->>EdgeMiddleware: request (matched route)
  EdgeMiddleware->>EdgeMiddleware: resolveRuntimeAuthUiMode(env.AUTH_TYPE, BUILD_TIME_AUTH_TYPE)
  EdgeMiddleware->>Cookie: set surfsense_auth_type = GOOGLE | LOCAL
  EdgeMiddleware-->>Browser: response with Set-Cookie
  Browser->>InitScript: execute before interactive
  InitScript->>Cookie: read surfsense_auth_type
  InitScript->>HTML: setAttribute data-surfsense-auth-type = GOOGLE | LOCAL
  HTML->>CSS: selector html[data-surfsense-auth-type] matches
  CSS->>Browser: hide .runtime-auth-local or .runtime-auth-google
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • MODSetter/SurfSense#441: Modifies the same hero-section.tsx component's auth call-to-action rendering, which this PR also refactors to unconditionally render both Google and local auth buttons.

Poem

🐇 Hop, hop, cookies set at the gate,
A script wakes up before the page is late,
Google or local? The HTML knows!
CSS hides what the runtime won't show,
No build-time branch, just a cookie's say,
Both buttons rendered, in a runtime way! 🍪

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 'feat: implement runtime authentication handling' directly and clearly summarizes the main change: introducing runtime authentication handling across the codebase.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

@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: 1

🤖 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 `@surfsense_web/proxy.ts`:
- Around line 8-24: Rename the file from proxy.ts to middleware.ts and move it
to the project root directory (or src/ if using a src directory structure).
Next.js middleware requires this specific file name and location to be
automatically recognized and invoked. Additionally, rename the exported function
from proxy to middleware, as Next.js expects the middleware function to be named
exactly middleware. Keep the config export with the matcher as is, since it will
now be properly recognized once the file is in the correct location with the
correct name.
🪄 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

Run ID: 5d458947-79b5-44a2-a179-e9a581ada5d6

📥 Commits

Reviewing files that changed from the base of the PR and between a4bb0a5 and b54eff6.

📒 Files selected for processing (6)
  • surfsense_web/app/globals.css
  • surfsense_web/app/layout.tsx
  • surfsense_web/components/auth/sign-in-button.tsx
  • surfsense_web/components/homepage/hero-section.tsx
  • surfsense_web/lib/runtime-auth-config.ts
  • surfsense_web/proxy.ts

Comment thread surfsense_web/proxy.ts
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