Hono and Better-Auth
Do you think if I create my own route for better-auth, would it be double api calling? If I want to keep only one api.
47 Replies
@lonelyplanet any tips?
I see you created a new thread I think it is same concern as me 😄
GitHub
GitHub - lonelyplanetdev/better-auth-with-hono-handler-nextjs
Contribute to lonelyplanetdev/better-auth-with-hono-handler-nextjs development by creating an account on GitHub.
Thats an option to use the better-auth invokable functions without the using the auth handler in hono, Downside you would have to create your own routes for auth logic and couldnt use better-auth/client but you can use hono/client in that example
@Sithu Khant That will allow you to manage one api but will make implementation more complex
it won't work
the better-auth live in
/api/auth/*
, if we mess up with its pass, auth
server code will fail
You need different path like /api/authorize/sign-in
for your own one
that is what I am doing it.You just dont add the handler
the downside is I need any single routes like better-auth has
that handler?
Step 7
it will still work?
Have a look at that repo
It works and uses hono+nextjs
if it is true, I am hype!
GitHub
better-auth-with-hono-handler-nextjs/src/app/api/[[...route]]/route...
Contribute to lonelyplanetdev/better-auth-with-hono-handler-nextjs development by creating an account on GitHub.
let me try that...
There is a small type error on the headers but its still working, the type error was safe to ignore since its just reading the headers
what the f**k, it is working...!
Wait i think the signout wont work...
its because i messed up the headers
I think the handler is only for frameworks
The handler is there for the client but also means you dont have to manually write out all your auth apis
I think if you have custom middleware, you might not need
headers: c.req.header(),
I have my own middlewarebut the concept is there you dont need the handler to use betterAuth.api
Like this:
That should work fine!, you still need to pass in headers to every betterAuth.api call
thanks, I would try
hey, how did you do for signout?
use this as an example
why I need raw headers?
The functions require headers to get the current session
without headers how would the signout method know to delete users a session and not users b session
now, I can use like this on my client code:
I don't need both auth client and hono client anymore, I only would need hono client!
That is cool!
Auth client will not work this way anyway auth client needs the auth handler
I mean it is not a problem to use both auth client and hono client if client and server are in the same folder, but might not be an ideal way if you have api from different server
If you are deploying backend and frontend separately you can use a mono repo for type safety in development.
really, how can I do that?
like graphql?
but I don't like that one
maybe openapi?
You could have hono backend export the AppType and it can be used inside of the front end to create the honoClient with that exported type
Turbo repo would be the easiest way to start playing with monorepos.
thanks, I would try
@lonelyplanet I think there is something wrong with my codes.
the cookies didn't get store!!!
this is my auth routes:
The routes im doing since im in nextjs im using the nextjsCookies plugin in auth instance
since your nolonger using the handler you need yo handle cookies yourself
yep
I think i need to follow this docs:
https://www.better-auth.com/docs/concepts/cookies
Cookies | Better Auth
Learn how cookies are used in Better Auth.
this is just my current auth server:
use
import {
getCookie,
getSignedCookie,
setCookie,
setSignedCookie,
deleteCookie,
} from 'hono/cookie'
for your cookies setting and getting
your shouldn't need to get a cookie since your passing all headers in on every request
then?
man!, I don't know too much about cookies!
but for signup you want to first check if the
const res = await auth.api.signUpEmail(..rest of code);
then do const sessionToken = res.token; // this will be string | undefined
this token is the value you will set the cookie
I think i have a solution for you give me a secondreally...? thanks!
https://github.com/better-auth/better-auth/issues/1470
Seems like there might be a bug atm
GitHub
The setSignedCookie type and import causes not TS errors and appear...
Is this suited for github? Yes, this is suited for github To Reproduce Import setSignedCookie from better auth into a file Try use it. Current vs. Expected behavior Should be a valid function as th...
I got it working!!!
I just need to set this:
like this:
chatgpt helped me.. xD!
Does it work?
yes... it is freaking working!
The
auth.ts
file still the sameIm sureprised glad it works
thanks man