javascript
javascript copied to clipboard
[sdk-node] Export authenticateRequest function
Description
- [sdk-node] Export
authenticateRequestfunction
Context
- Currently using a nitro server which isn't compatible with the clerk express middleware when is not authenticated. I was able to create a workaround locally, but thought would make sense to export this to make it easier for others.
For others reference, this is how I did it:
// server/middleware/auth.ts
import { clerkClient } from '@clerk/clerk-sdk-node'
import { createClerkRequest } from '@clerk/backend/internal'
const incomingMessageToRequest = req => {
const headers = Object.keys(req.headers).reduce((acc, key) => Object.assign(acc, { [key]: req?.headers[key] }), {})
const protocol = req.connection?.encrypted ? 'https' : 'http'
const dummyOriginReqUrl = new URL(req.url || '', `${protocol}://clerk-dummy`)
return new Request(dummyOriginReqUrl, {
method: req.method,
headers: new Headers(headers)
})
}
export default defineEventHandler(async event => {
const requestState = await clerkClient.authenticateRequest(
createClerkRequest(incomingMessageToRequest(event.node.req))
)
const auth = requestState.toAuth()
event.context.user = auth
})
Checklist
- [x]
npm testruns as expected. - [x]
npm run buildruns as expected. - [ ] (If applicable) JSDoc comments have been added or updated for any package exports
- [ ] (If applicable) Documentation has been updated
Type of change
- [ ] 🐛 Bug fix
- [ ] 🌟 New feature
- [ ] 🔨 Breaking change
- [x] 📖 Refactoring / dependency upgrade / documentation
- [ ] other:
🦋 Changeset detected
Latest commit: c856f4fa7ded9442463e938f95c3b2fff343ad7c
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
| Name | Type |
|---|---|
| @clerk/clerk-sdk-node | Patch |
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
Hello @greguintow , many thanks for the PR!
We have plans to deprecate @clerk/clerk-sdk-node and replace it with @clerk/express soon as the "node SDK" abstraction no longer feels valid - there are many JS runtimes introduced the past 2 years that need to be supported.
The isomorphic @clerk/backend package supports all JS runtimes and except for some low-level utils, it should export the same helpers as @clerk/clerk-sdk-node except for the helpers that are specific to Express.
Would this maybe handle your use case better? If not, it'd be great if you could share any insights to help us better understand your requirements :)
Hello @greguintow , many thanks for the PR! We have plans to deprecate
@clerk/clerk-sdk-nodeand replace it with@clerk/expresssoon as the "node SDK" abstraction no longer feels valid - there are many JS runtimes introduced the past 2 years that need to be supported.The isomorphic
@clerk/backendpackage supports all JS runtimes and except for some low-level utils, it should export the same helpers as@clerk/clerk-sdk-nodeexcept for the helpers that are specific to Express.Would this maybe handle your use case better? If not, it'd be great if you could share any insights to help us better understand your requirements :)
hey @nikosdouvlis , yeah I think that would be good, thaanks!
Hello @greguintow , many thanks for the PR! We have plans to deprecate
@clerk/clerk-sdk-nodeand replace it with@clerk/expresssoon as the "node SDK" abstraction no longer feels valid - there are many JS runtimes introduced the past 2 years that need to be supported. The isomorphic@clerk/backendpackage supports all JS runtimes and except for some low-level utils, it should export the same helpers as@clerk/clerk-sdk-nodeexcept for the helpers that are specific to Express. Would this maybe handle your use case better? If not, it'd be great if you could share any insights to help us better understand your requirements :)hey @nikosdouvlis , yeah I think that would be good, thaanks!
Great :) I'm going to close this one for now - please reopen it if you think the existing solution is not enough! Many thanks for opening the PR in the first place 🙏🏻