-
Notifications
You must be signed in to change notification settings - Fork 50
feat: enhance onSuccess callback and middleware request #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
… tests to include response and state
…n with enhanced session management and customizable options
…ssion management, error handling, and request modifications
…ent and enhance session handling
|
Hey @abedshaaban thanks for this! I have a couple of questions. onSuccess changes Forcing onSuccess to return a Response really overloads this callback. Its initial intent was to allow for reacting to successful authentication as a side-effect hook - returning void is by design. Changing that would be a breaking change for existing users. Is there something you can't accomplish without this? For example, with the current API you can:
If you need dynamic redirects based on user data, that could be solved by making returnPathname accept a callback, or handling the routing logic on the destination page. What's the specific use case you're trying to solve? Composable middleware We actually have this already! See the Composing middleware section of the README. However, I agree it's a bit complicated - I opened #348 to simplify this with helpers that ensure headers are merged correctly. |
|
Thank you @nicknisi for your feedback. Making For the middleware I am trying to combine next-intl with workos. It works locally but breaks on Netlify and returns in the logs import { NextRequest } from 'next/server'
import { defaultLocale, locales } from '@repo/constants/internationalization'
import { authkit } from '@workos-inc/authkit-nextjs'
import createNextIntlMiddleware from 'next-intl/middleware'
const i18nMiddleware = createNextIntlMiddleware({
locales,
defaultLocale
})
export async function middleware(request: NextRequest) {
// Run the i18n middleware first
const intlResponse = i18nMiddleware(request)
// Run AuthKit middleware
const { headers: authkitHeaders } = await authkit(request)
// Copy Set-Cookie and cache control headers to the response, but exclude the internal
// x-workos-session header which contains encrypted session data and should never appear
// in HTTP responses (it's only used to pass session data between middleware and page handlers)
for (const [key, value] of authkitHeaders) {
if (key.toLowerCase() === 'x-workos-session') {
continue // Internal header - must not leak to response
}
if (key.toLowerCase() === 'set-cookie') {
intlResponse.headers.append(key, value)
} else {
intlResponse.headers.set(key, value)
}
}
return intlResponse
} |
…and adjust handleAuth function to align with new type
|
Ah, interesting! And the fixes in this PR get it working on Netlify? I need to look into this. |
|
Not 100% sure but I tried to implement it as clerk does in their middleware to combine middlewares. |
…n authkit-callback-route tests
…ertions in authkit-callback-route tests
…on in authkit-callback-route tests
This feature introduces a main change:
authkitMiddlewareto be able to merge multiple middlewares in Nextjs and be able to modify them.Usage examples:
For middlewares