fix: auto-detect port in auth client baseURL#11
Conversation
- Replace hardcoded localhost:3000 with window.location.origin - Enables auth to work on any port (3000, 3001, etc.) - Fixes issue when port 3000 is already in use - Maintains SSR compatibility with fallback env var
WalkthroughThe auth client's baseURL determination logic was refactored to use browser detection instead of NODE_ENV checks. When in a browser, it uses Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Client Code
participant AuthClient as authClient
participant BrowserCheck as Environment Check
participant URLResolution as URL Resolution
Client->>AuthClient: Initialize
AuthClient->>BrowserCheck: Running in browser?
alt Browser Environment
BrowserCheck->>URLResolution: Use window.location.origin
URLResolution-->>AuthClient: baseURL set to browser origin
else Server Environment
BrowserCheck->>URLResolution: Check NEXT_PUBLIC_AUTH_URL
alt Env Var Set
URLResolution-->>AuthClient: baseURL set to env var
else Env Var Not Set
URLResolution-->>AuthClient: baseURL set to http://localhost:3000
end
end
AuthClient-->>Client: Ready with baseURL
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/modules/auth/utils/auth-client.ts (1)
9-9: Consider validating the NEXT_PUBLIC_AUTH_URL format.The fallback to
process.env.NEXT_PUBLIC_AUTH_URLshould ideally validate that it's a properly formatted URL. An invalid format could lead to runtime errors that are difficult to debug.export const authClient = createAuthClient({ baseURL: typeof window !== "undefined" ? window.location.origin // Auto-detect from browser URL (works for any port) - : process.env.NEXT_PUBLIC_AUTH_URL || "http://localhost:3000", // Fallback for SSR + : process.env.NEXT_PUBLIC_AUTH_URL || "http://localhost:3000", // Fallback for SSR });Alternatively, add validation logic:
const getBaseURL = () => { if (typeof window !== "undefined") { return window.location.origin; } const envURL = process.env.NEXT_PUBLIC_AUTH_URL; if (envURL && !envURL.startsWith("http")) { console.warn("NEXT_PUBLIC_AUTH_URL should start with http:// or https://"); } return envURL || "http://localhost:3000"; }; export const authClient = createAuthClient({ baseURL: getBaseURL(), });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/modules/auth/utils/auth-client.ts(1 hunks)
🔇 Additional comments (1)
src/modules/auth/utils/auth-client.ts (1)
7-9: Architecture verified: auth backend is correctly implemented as Next.js API routes.The verification confirms the auth backend is deployed at
src/app/api/auth/[...all]/route.tsas part of the same Next.js application. Thewindow.location.originauto-detection is the correct approach for this same-origin architecture, and theprocess.env.NEXT_PUBLIC_AUTH_URLfallback provides appropriate SSR handling. No security concerns for this deployment model.
|
Not sure if this is actually a bug or just I didn't use it properly but Claude fixed it so that I could get it working. |
Major rebrand from fork to independent Jezweb open source project. ## Branding Changes - Project name: fullstack-next-cloudflare-demo → Full Flare Stack - Version: 0.1.0 → 1.0.0 (production-ready) - Repository: github.com/jezweb/full-flare-stack - License: MIT (Jez Dawes / Jezweb) - Status: Public open source project ## Documentation Added - Complete README.md rewrite with new branding and value proposition - CHANGELOG.md documenting fork history and v1.0.0 improvements - COMPONENT_INVENTORY.md (43 shadcn/ui components documented) - COMPOSED_PATTERNS_ROADMAP.md (pattern build priorities) - docs/development-planning/ (architecture guides) - docs/templates/PATTERN_TEMPLATE.md ## Architecture Improvements - Three-layer component system (primitives → patterns → features) - Component decision framework - 43 shadcn/ui components pre-installed - Pattern extraction methodology (after 3rd use) - Comprehensive development workflow documentation ## Acknowledgments - Original template: @ifindev/fullstack-next-cloudflare - Contributed 11 PRs (ifindev#11-21) with fixes and documentation upstream - Thank you to @ifindev for the excellent starting point! BREAKING CHANGE: Project renamed to Full Flare Stack 🤖 Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Problem
The auth client hardcodes
localhost:3000, which breaks when:Solution
Use
window.location.originto auto-detect the current URL in the browser. This works for:Falls back to
NEXT_PUBLIC_AUTH_URLenv var for SSR compatibility.Testing
Changes
src/modules/auth/utils/auth-client.tsSummary by CodeRabbit