Skip to content

feat: add redirectUri option to middleware#29

Merged
nicknisi merged 3 commits intomainfrom
nicknisi/redirect-uri-customization
Jan 9, 2026
Merged

feat: add redirectUri option to middleware#29
nicknisi merged 3 commits intomainfrom
nicknisi/redirect-uri-customization

Conversation

@nicknisi
Copy link
Member

Summary

  • Add redirectUri option to authkitMiddleware() for dynamic redirect URI configuration
  • Propagate redirectUri through context to getSignInUrl, getSignUpUrl, and getAuthorizationUrl
  • Export AuthKitMiddlewareOptions type

Why

Closes #22. Users need to customize redirect URIs dynamically (e.g., Vercel preview deployments), matching the NextJS SDK behavior.

Notes

Priority order: explicit function param > middleware config > env var WORKOS_REDIRECT_URI

@greptile-apps
Copy link

greptile-apps bot commented Dec 15, 2025

Greptile Overview

Greptile Summary

Adds redirectUri configuration option to authkitMiddleware() enabling dynamic redirect URI customization for OAuth callbacks. This addresses the need for custom redirect URIs in dynamic environments like Vercel preview deployments.

Key Changes:

  • Added redirectUri option to middleware configuration that propagates through context
  • Updated getSignInUrl, getSignUpUrl, and getAuthorizationUrl to support redirect URI with proper priority: explicit parameter > middleware config > env var
  • Exported AuthKitMiddlewareOptions type for public API usage
  • Added comprehensive test coverage for the new functionality

Implementation Quality:

  • Clean architecture following existing patterns in the codebase
  • Proper TypeScript typing throughout
  • Follows the same priority chain pattern as the NextJS SDK
  • Well-documented with JSDoc comments and usage examples
  • Backwards compatible - all parameters are optional

Confidence Score: 5/5

  • This PR is safe to merge with no issues found
  • The implementation is clean, well-tested, and follows established patterns. All changes are additive and backwards compatible. The priority chain (explicit param > middleware config > env var) is properly implemented with correct fallback logic using the nullish coalescing operator. Tests cover both scenarios (with and without redirectUri). No security concerns identified.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
src/server/middleware.ts 5/5 Added AuthKitMiddlewareOptions interface with optional redirectUri parameter, properly passes it to context
src/server/context.ts 5/5 Added optional redirectUri field to AuthKitServerContext interface
src/server/auth-helpers.ts 5/5 Added getRedirectUriFromContext() helper function to retrieve redirect URI from middleware context
src/server/server-functions.ts 5/5 Updated getSignInUrl, getSignUpUrl, and getAuthorizationUrl to support redirectUri parameter with proper priority fallback
src/server/index.ts 5/5 Exported AuthKitMiddlewareOptions type for public API
src/server/middleware.spec.ts 5/5 Added comprehensive test coverage for redirectUri option in middleware configuration

Sequence Diagram

sequenceDiagram
    participant User
    participant App
    participant Middleware
    participant Context
    participant ServerFn as Server Functions
    participant AuthKit as AuthKit SDK
    
    User->>App: Configure authkitMiddleware({redirectUri})
    App->>Middleware: Request with middleware config
    Middleware->>Context: Store redirectUri in context
    
    User->>ServerFn: Call getSignInUrl/getSignUpUrl/getAuthorizationUrl
    ServerFn->>Context: getRedirectUriFromContext()
    Context-->>ServerFn: Return middleware redirectUri (if set)
    
    alt Explicit redirectUri parameter provided
        ServerFn->>AuthKit: Pass explicit redirectUri
    else Middleware redirectUri available
        ServerFn->>AuthKit: Pass middleware redirectUri
    else No redirectUri specified
        ServerFn->>AuthKit: Use env var WORKOS_REDIRECT_URI
    end
    
    AuthKit-->>ServerFn: Return authorization URL
    ServerFn-->>User: Return URL with correct redirect URI
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

6 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Make getAuthorizationUrl use the same conditional pattern as
getSignInUrl and getSignUpUrl for injecting context redirectUri.
@nicknisi nicknisi force-pushed the nicknisi/redirect-uri-customization branch from 7b7d3cf to ce35b17 Compare January 9, 2026 16:08
@nicknisi nicknisi merged commit 45844a3 into main Jan 9, 2026
6 checks passed
@nicknisi nicknisi mentioned this pull request Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

No way to customize WORKOS_REDIRECT_URI in middleware

2 participants