Edge Helper for creating a Response #548
Replies: 2 comments 1 reply
-
That being said, I would be down to create a pull request for this and also the tutorials necessary to explain the (improved) edge flow. |
Beta Was this translation helpful? Give feedback.
-
I love using web standards but the browser expects multiple
Instead of an array:
The issue is ...Or so I thought! Well, it was correct but it finally changed with This is great news since we can just add a new middleware: Side note: |
Beta Was this translation helpful? Give feedback.
-
Hello all. I love the simplicity and customisability of Lucia. It already supports a wide range of frameworks and for that I am hyped. The reason I chose for Lucia is that it is edge compatible, something I am very interested in. When using the
auth.handleRequest()
in combination with edge api routes from Nextjs I found some things I think would be a nice addition for Lucia to make the dx even better. I have some Ideas I want to share.Disclaimers:
new AuthRequest
directly instead of usingauth.handleRequest()
so that you can see which parameters in the middleware I am using.First of all the
node()
middleware is not compatible with the edge api routes. For this a new middleware is needed. I created it and called itvercelEdge()
Then with this middleware we can go onwards creating our authHandler.
Basic example:
This works like expected. The cookie gets validated and afterwards it gets cleared. The response then is returned and the cookie gets set. No problems at all here.
However, this gets interesting when you want to return for instance the user:
A possible way to fix this, is using the
getCookie()
function:But again this gets messy when you want to add more cookies (or headers):
We are now at a point where the simplicity (and honestly beauty) of Lucia is gone. I like the
handleRequest()
flow and that it does the stuff for you. But to make work beautiful with the edge api we need to make a couple of changes.First of all, the first
const res = new Response()
is never returned when wanting to add JSON to the response. It only gets used because the middleware wants it, but forgetCookie()
it doesn't get used. So it is extra, and we can remove it.The new
vercelEdge()
now only takes a request. But hold on, what about setting that cookie? Well, what if instead of setting the cookie, we store it in the class properties and store it when previously setting the cookie. Like insetSession()
:What this enables us to do is to reference the cookie in other functions. And then we can create a new function.
With this function we can have the simplicity of Lucia back. And our edge handler looks like:
This is very nextjs specific and I do not know if this can be applied to other frameworks. I am hacking around with this and have it working for my use-case. I wanted to share my thoughts so that maybe in the future there can be a nice (and maybe better than mine) solution.
Beta Was this translation helpful? Give feedback.
All reactions