-
Notifications
You must be signed in to change notification settings - Fork 272
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
chore(express): Refactor requireAuth
and clerkMiddleware
middlewares
#4234
chore(express): Refactor requireAuth
and clerkMiddleware
middlewares
#4234
Conversation
🦋 Changeset detectedLatest commit: a7eb3eb The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
requireAuth
to redirect to sign in page instead of forwarding errorrequireAuth
and clerkMiddleware
middlewares
it('throws error if clerkMiddleware is not executed before requireAuth', async () => { | ||
const customMiddleware: RequestHandler = (_request, response, next) => { | ||
response.setHeader('x-custom-middleware', 'custom'); | ||
return next(); | ||
}; | ||
|
||
const response = await runMiddleware([requireAuth, customMiddleware]).expect(500); | ||
|
||
assertNoDebugHeaders(response); | ||
expect(response.header).not.toHaveProperty('x-clerk-auth-custom', 'custom-value'); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this tests as requireAuth
does not rely on clerkMiddleware
anymore
it('supports usage with request handler: app.use(clerkMiddleware(requestHandler))', async () => { | ||
const handler: RequestHandler = (_req, res, next) => { | ||
res.setHeader('x-clerk-auth-custom', 'custom-value'); | ||
return next(); | ||
}; | ||
|
||
const response = await runMiddleware(clerkMiddleware(handler, { enableHandshake: true }), { | ||
Cookie: '__clerk_db_jwt=deadbeef;', | ||
}).expect(200, 'Hello world!'); | ||
|
||
expect(response.header).toHaveProperty('x-clerk-auth-custom', 'custom-value'); | ||
assertSignedOutDebugHeaders(response); | ||
}); | ||
|
||
it('supports usage with parameters and request handler: app.use(clerkMiddleware(requestHandler, options))', async () => { | ||
const handler: RequestHandler = (_req, res, next) => { | ||
res.setHeader('x-clerk-auth-custom', 'custom-value'); | ||
return next(); | ||
}; | ||
const options = { publishableKey: 'pk_test_Y2xlcmsuZXhhbXBsZS5jb20k', enableHandshake: true }; | ||
|
||
const response = await runMiddleware(clerkMiddleware(handler, options), { | ||
Cookie: '__clerk_db_jwt=deadbeef;', | ||
}).expect(200, 'Hello world!'); | ||
|
||
expect(response.header).toHaveProperty('x-clerk-auth-custom', 'custom-value'); | ||
assertSignedOutDebugHeaders(response); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed as we removed option to pass a handler in clerkMiddleware
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is beautiful I love it. Such a quick turnaround!
const enableHandshake = options.enableHandshake || false; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
const middleware: RequestHandler = async (request, response, next) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have we covered for the scenario where someone uses both middlewares? A little conditional at the top of this helper to just call next if auth
is already on the request I think would take care of that. May be worth adding a test for this as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, but added just now and a test too!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to error if there are both? Could we just add a conditional check for req.auth
at the top of the function instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I get you now, we want to allow usage of both, but check for req.auth
first then call next()
if exists so we dont have to run the same logic etc 👍🏼 Perfect for scenario like
app.use(clerkMiddleware());
app.get('/protected', requireAuth(), (req, res) => {
res.send('This is a protected route');
});
I love those changes, the DX is much better now ❤️ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me! Nice work Rob 👏
Description
This PR refactors both
clerkMiddleware
andrequireAuth
Express middlewares to simplify DX and reduce confusion when choosing between middlewares:clerkMiddleware
Removed option to pass a handler. It does not do anything at all, which can be replaced by a one-liner from userland:
requireAuth
Is now using the same logic as
clerkMiddleware
(useauthenticateRequest
and attachauth
torequest
), except it will redirect tosignInUrl
if unauthenticated instead of callingnext(new Error())
. So you can haverequireAuth
middleware withoutclerkMiddleware
:Other removed exports:
UnauthorizedError
(not used anywhere)ForbiddenError
(not used anywhere)Documentation for this one is still being worked on, we didn't announce the SDK formally, so I think
minor
update is good. The documentation will have migration guide from@clerk/clerk-sdk-node
to@clerk/express
. anyway.Resolves ECO-201
Checklist
npm test
runs as expected.npm run build
runs as expected.Type of change