Skip to content
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

Add composable middleware #164

Merged
merged 28 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a8503dc
Added tests and made a start to auth.ts
PaulAsjes Nov 26, 2024
ecd3173
Add tests for cookie and callback route
PaulAsjes Nov 26, 2024
60f70f8
Tests for session and actions
PaulAsjes Dec 2, 2024
0c55d70
Add jsdom tests for tsx files
PaulAsjes Dec 3, 2024
47aee50
Add new workflow
PaulAsjes Dec 3, 2024
10d2004
Clean up jest config file
PaulAsjes Dec 3, 2024
ff50e18
Didn't mean to add this
PaulAsjes Dec 3, 2024
77393b9
Add jest config and setup scripts to ts exclude
PaulAsjes Dec 3, 2024
33559b0
Impersonation shouldn't be a client component for now
PaulAsjes Dec 3, 2024
72f1c2e
100% test coverage
PaulAsjes Dec 4, 2024
27a5a1e
Add debug flag
PaulAsjes Dec 4, 2024
3b568af
Add another test and change coverage engine to have local and github …
PaulAsjes Dec 4, 2024
05abae4
Should actually add the test
PaulAsjes Dec 4, 2024
df7ced3
Address feedback
PaulAsjes Dec 13, 2024
2656a76
Also run prettier on test files
PaulAsjes Dec 13, 2024
3e6968f
Merge remote-tracking branch 'origin/main' into pma/composable-middle…
PaulAsjes Jan 2, 2025
0399fcc
wip
PaulAsjes Jan 3, 2025
851ac7a
wip
PaulAsjes Jan 7, 2025
142ecca
Merge remote-tracking branch 'origin/main' into pma/composable-middle…
PaulAsjes Jan 7, 2025
f05af89
Add tests
PaulAsjes Jan 8, 2025
a42594c
Delete getSession in favor of authkit method
PaulAsjes Jan 8, 2025
bae11b8
Restore package-lock.json
PaulAsjes Jan 8, 2025
61f9127
Flip debug back to false
PaulAsjes Jan 8, 2025
cfacbcc
Remove deprecated tests and update readme
PaulAsjes Jan 8, 2025
7b27eb5
Make options object optional and fix tests
PaulAsjes Jan 8, 2025
46a857d
Merge remote-tracking branch 'origin/main' into pma/composable-middle…
PaulAsjes Jan 10, 2025
159f118
Update tests
PaulAsjes Jan 10, 2025
dd9143e
Merge remote-tracking branch 'origin/main' into pma/composable-middle…
PaulAsjes Jan 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,27 +301,36 @@ In the above example the `/admin` page will require a user to be signed in, wher

`unauthenticatedPaths` uses the same glob logic as the [Next.js matcher](https://nextjs.org/docs/pages/building-your-application/routing/middleware#matcher).

### Retrieve session in middleware
### Composing middleware

Sometimes it's useful to check the user session if you want to compose custom middleware. The `getSession` helper method will retrieve the session from the cookie and verify the access token.
If you don't want to use `authkitMiddleware` and instead want to compose your own middleware, you can use the `authkit` method. In this mode you are responsible to handling what to do when there's no session on a protected route.

```ts
import { authkitMiddleware, getSession } from '@workos-inc/authkit-nextjs';
import { NextRequest, NextFetchEvent } from 'next/server';
export default async function middleware(request: NextRequest) {
// Perform logic before or after AuthKit

export default async function middleware(request: NextRequest, event: NextFetchEvent) {
// authkitMiddleware will handle refreshing the session if the access token has expired
const response = await authkitMiddleware()(request, event);
// Auth object contains the session, response headers and an auhorization URL in the case that the session isn't valid
// This method will automatically handle setting the cookie and refreshing the session
const { session, headers, authorizationUrl } = await authkit(request, {
debug: true,
});

// If session is undefined, the user is not authenticated
const session = await getSession(response);
// Control of what to do when there's no session on a protected route is left to the developer
if (request.url.includes('/account') && !session.user) {
console.log('No session on protected path');
return NextResponse.redirect(authorizationUrl);

// ...add additional middleware logic here
// Alternatively you could redirect to your own login page, for example if you want to use your own UI instead of hosted AuthKit
return NextResponse.redirect('/login');
}

return response;
// Headers from the authkit response need to be included in every non-redirect response to ensure that `withAuth` works as expected
return NextResponse.next({
headers: headers,
});
}

// Match against pages that require auth
// Match against the pages
export const config = { matcher: ['/', '/account/:path*'] };
```

Expand Down
Loading
Loading