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

Cloudflare Workers Support #725

Closed
rohanbam opened this issue Sep 30, 2020 · 22 comments
Closed

Cloudflare Workers Support #725

rohanbam opened this issue Sep 30, 2020 · 22 comments
Labels
question Ask how to do something or how something works

Comments

@rohanbam
Copy link

Can NextAuth.js be used inside a Cloudflare Worker?

I see that the server side example for using NextAuth has the following line
export default (req, res) => NextAuth(req, res, options)

Since Cloudflare Workers do not have a node runtime, I was wondering if the req and res objects would work inside a Cloudflare Worker. If not is there a work around that you can suggest? Thank you.

@rohanbam rohanbam added the question Ask how to do something or how something works label Sep 30, 2020
@iaincollins
Copy link
Member

Can NextAuth.js be used inside a Cloudflare Worker?

Unfortunately not.

CloudFlare Workers are missing features required by Next.js and common node modules required by Next.js and NextAuth.js. While highly cost effective in some scenarios in practice I've found the workers are limited to use for bespoke functions written explicitly with them in mind.

However, you can use them in tandem with a Next.js app which uses NextAuth.js that deployed somewhere else to do something like read and validate a JWT to determine if a user should be returned content (that's a GREAT use case!) but you couldn't deploy a Next.js or NextAuth.js app as a CloudFlare Worker directly.

If you are looking at similar technologies, it is also possible to deploy Next.js and NextAuth.js to AWS Lambda @ Edge - though it's a little trickier to deploy than to a regular AWS Lambda (and in turnthat more involved than deploying to Vercel or Heroku or with Docker). Lambda @ Edge instances are similar to AWS Lambda but with some limitations (memory, CPU and environment variables need to be baked in); like CloudFlare Workers they run at the Edge, but on the AWS CloudFront CDN.

@iaincollins
Copy link
Member

Closing this one as we've answer the question as best we can. Happy to respond to any follow up questions!

@rohanbam
Copy link
Author

Thank you.

@pbshgthm
Copy link

read and validate a JWT to determine if a user should be returned content

@iaincollins I'm trying to implement this. Here are my concerns. I need to make a few calls to the DB and process the data, which occasionally might take more than 10s, thus resulting in a timeout on Vercel. Hence I'm I'd like to have the API external and have the client directly call the external API, so the timeout issue can be overcome. But now the external API need to validate the requests from the client. I tried to find out how to validate the JWT, but it looks like the client doesn't have access to the raw token and can only the data can be accessed via useSession(). How should I go about in this situation? Or are there any flaws in my understanding so for? Thanks in advance!

@cryptiklemur
Copy link

NextJS Now supports running api routes on Cloudflare workers. Can we get this re-opened @iaincollins

https://nextjs.org/docs/api-routes/edge-api-routes

@konradbjk
Copy link

@iaincollins ^^ reopen?

@Pelps12
Copy link

Pelps12 commented Dec 15, 2022

Now we have @auth/core, is this possible?

@eli-front
Copy link

Any updates here?

@Pelps12
Copy link

Pelps12 commented Dec 24, 2022

I got it to work. I can make a sample repository by tomorrow

@eli-front
Copy link

I got it to work. I can make a sample repository by tomorrow

Thanks so much! You're a lifesaver

@zmarouf
Copy link

zmarouf commented Jan 3, 2023

@Pelps12 Could you share how you did it please? I'm hitting this with Cloudflare Pages.

@jebreuer
Copy link

jebreuer commented Jan 5, 2023

I'd love to get next-auth running on cloudflare. Just tried the next-auth example app (https://github.com/nextauthjs/next-auth-example) with the suggested changes and instructions for next apps on cloudflare: https://github.com/cloudflare/next-on-pages

I hit the wall here:

▲ warn  - You are using the experimental Edge Runtime with `experimental.runtime`.
▲
▲ Failed to compile.
▲
▲ ./node_modules/openid-client/lib/helpers/deep_clone.js:1:33
▲ Module not found: Can't resolve 'v8'
▲
▲ Import trace for requested module:
▲ ./node_modules/openid-client/lib/issuer.js
▲ ./node_modules/openid-client/lib/index.js
▲ ./node_modules/next-auth/core/lib/oauth/callback.js
▲ ./node_modules/next-auth/core/routes/callback.js
▲ ./node_modules/next-auth/core/routes/index.js
▲ ./node_modules/next-auth/core/index.js
▲ ./node_modules/next-auth/next/index.js
▲ ./pages/server.tsx

Any help with this would be highly appreciated.

Btw, npm i v8 only gets me further down the rabbit hole full of errors. There seems to be something so obviously wrong that I cannot see it.

@geekyayush
Copy link

@Pelps12 I would appreciate it if you could share the repo.

@cryptiklemur
Copy link

@Pelps12 dont tease us!

@eli-front
Copy link

I have been in communication with @Pelps12. Here is a repo with the core next auth functionality that I built. You will need to set up your adapter & finish the provider, but this should take you part of the way there. I will finish this when I have some more availability and I would appreciate any PRs. I can explain to someone the how if they are willing to put in the time to finish it. https://github.com/eli-front/next-auth-edge

@eli-front
Copy link

I have been in communication with @Pelps12. Here is a repo with the core next auth functionality that I built. You will need to set up your adapter & finish the provider, but this should take you part of the way there. I will finish this when I have some more availability and I would appreciate any PRs. I can explain to someone the how if they are willing to put in the time to finish it. https://github.com/eli-front/next-auth-edge

@aequasi @geekyayush @jebreuer @zmarouf

@cryptiklemur
Copy link

There's no integration with next yet on @auth. @auth/nextjs isn't published. Seems next-auth/core still requires things like buffer

@eli-front
Copy link

You should be able to use @auth/core with next if you'd like.

@cryptiklemur
Copy link

Theres no SessionProvider to put in _app in @auth/core

@eli-front
Copy link

eli-front commented Jan 25, 2023

Theres no SessionProvider to put in _app in @auth/core

It's not too bad to make your own session provider. It's just a GET to /api/auth/session with specified frequency & a hook.
You can also use @auth/core in a cf worker instead of a next API route and then just set that worker endpoint as your NEXTAUTH_URL. Then you should be able to use the next-auth package still because you won't be using it anyone for the API route.

@EliBates
Copy link

EliBates commented Feb 6, 2023

@balazsorban44 Would you consider re-opening this? it seems there is a lot of renewed interest in this topic.

@balazsorban44
Copy link
Member

balazsorban44 commented Feb 6, 2023

We recently announced @auth/core/Auth.js, and a new generation of runtime agnostic packages for ANY framework. Check out https://twitter.com/balazsorban44/status/1603082914362986496

Although experimental, @auth/core should be running on Cloudflare and similar platforms already.

As for next-auth, this won't be backported, we plan to introduce @auth/nextjs instead, which will be using the new core under the hood. No ETA, but we are using it in a Next.js development environment already https://github.com/nextauthjs/next-auth/blob/main/apps/dev/nextjs/pages/api/auth/%5B...nextauth%5D.ts

The client-side is pretty much next-auth/react for now. Works well with an Auth.js backend too.

If you find any compatibility issues, I suggest opening a new issue, rather than commenting here. We don't monitor closed issues. Thanks! 🙏

@nextauthjs nextauthjs locked as resolved and limited conversation to collaborators Feb 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Ask how to do something or how something works
Projects
None yet
Development

No branches or pull requests