Hono and Better-Auth
Hi there,
I know this is the better-auth thing, but just asking for a help.
This is my
auth
from api
:
This is my Hono root route:
This is my Hono middleware:
This is auth client from web
:
This is requireAuth
function to use in +page.server.ts
:
But I am getting Unauthorized
responsive back although I successfully logged in.
I can do sign-in and sign-out, but I am just getting authorized when I use loggedIn
middelware. How can I fix that?3 Replies
sounds like an issue with the cookies
could be cross-origin, or something about how sveltekit handles cookies
i would start by checking whether
- credentials are included in the response server-side
- whether they're also available client side
- that they're being included in the outgoing (non-login) requests
I've spent hours trying to figure this out. Found a couple of interesting things:
1. Better auth sign-up and login works
2.
auth.api.getSession
doesn't appear to be triggered in Hono middleware. Despite having the Bearer token passed as an authorization header, the bearer-auth function doesn't trigger.
3. Since better auth use baseURL
to then check for auth, is it because the better auth client in the middleware is trying to call another Hono route endpoint, causing loops?
Workaround ##
We can just use db
to fetch the session and user data ourselves:
Happy to see if others figured why 🤔you prob don't want the
|| ""
, sounds like a bug waiting to happen
if there is no token, then there is no session
simple as
and even if it cannot cause a bug, you are wasting precious time by querying the database for a row that matches where session.token = ""