How to add credentials to a Hono client?

I see hono documentarion unfortunately lacks a lot of information on how to use it appropriately, and It's no different with this piece of information, so I'd like to know how to or if it's even possible to make authentication via cookies. Can anyone help me?
9 Replies
ambergristle
ambergristle3d ago
does this help? https://hono.dev/docs/helpers/cookie hono also comes w basic + bearer auth middleware and there are hono integrations for a variety of auth services
Amodeus R.
Amodeus R.OP2d ago
I need a way to include credentials in the frontend, because I'm actually getting/setting cookies in the backend, but for some reason it sets but doesn't seem to be getting it, or at least it doesn't recognize it somehow, because it always throws an unauthorized route error. My API call only worked when I used a normal fetch with the option credentials included, or when it wasn't a cross-site request, that's why I think the problem is the lack of credentials included
ambergristle
ambergristle23h ago
i'm not sure i follow all of this if you have a concrete/specific problem, could you share: - what your current setup is - what you're trying to accomplish - what isn't working as expected? and some snippets will probably be helpful
Amodeus R.
Amodeus R.OP23h ago
It's simple. I want to access authenticated routes, but whenever I try to access an authenticated route the authentication doesn't work and it returns 401 unauthorized (the message I configured to when it can't authenticate). I don't quite understand why, but I know it works when it's in the same route/port or something like this, but as soon as I change for different ports or sites it doesn't work. Adding the credentials: include to the fetch function and some other configurations in the backend made it work, but I don't see any credentials: include option in Hono, and that's all I need and want to make these routes work cross-site. You can take a look at the repo if you want to here https://github.com/AmodeusR/expense-tracker
ambergristle
ambergristle22h ago
there's no such thing as "simple" when it comes to code
Amodeus R.
Amodeus R.OP22h ago
Well.. it was simple with the fetch function
ambergristle
ambergristle22h ago
it might have felt simple to implement, but there's still a lot going on i still don't really understand what issue you're experiencing. it sounds like you've set up a cookie-based auth route, but you're running into cors issues that's probably where i'd start but tbh, the answer to your issue is probably hidden in all the details you're omitting
Amodeus R.
Amodeus R.OP22h ago
The cors are already allowed, the cors error is different from a 401 unauthorized error that as I said I myself configured to happen if the authentication fails. I already shared the repo, so there's nothing more I'm omitting, the only thing I wanted was a way to add the option I said I wanted, since such option made things work with the javascript fetch function, so it'd likely make hono work as well As I said previously, I'm making authentication via cookies, but the cookies aren't being sent because it's a cross-site connection and to make the cookies be sent the fetch function needs the credentials: include added to it, hence I came here to know how to add it to the hono client function, because I don't want to use the fetch function, but the actual honojs client
ambergristle
ambergristle21h ago
here are the hono docs RE initializing RPC requests: https://hono.dev/docs/guides/rpc#init-option the hono docs are actually fairly comprehensive. they're not always super clear, so you do have to read them closely but idk that i've actualy come across anything that wasn't documented at all yet as w any newer/bleeding edge tech, a little patience goes a long way

Did you find this page helpful?