Kinde + Bun + Hono + React
Hey I was just hoping to get some advice/validation on code I've written for using Kinde with a bun and hono app. I'm using the typescript SDK.
My session manager is storing all the session items in cookies as the next.js libary does. Then I have some custom middleware for protected routes.
My backend is serving up a react app, the cookies get sent with every request, and everything is working there. I have a
/api/me
endpoint that checks if the user is logged in. The react app calls that endpoint when it first loads to check if the user is logged in.
8 Replies
Hey @saM69420,
Thanks for reaching out.
Are you able to explain more on what a "bun and hono app" is?
Also just checking you aren't experiencing any issue and you just want us to validate your code/approach?
No issues, I just want to make sure i'm not shooting myself in the foot or causing any security issues. This is for a tutorial so I don't want to give people bad advice
bun is the typescript runtime instead of node.js
hono is the backend framework instead of express
We are here to help anytime you need some sense-checking on your code, especially for any security vulnerabilities. Also thanks for elaborating on
bun
and hono
.
I have passed your code to an teammate of mine who is an expert on TypeScript and Node.js
Loved your last Kinde video by the way, thanks for much for the support you give us.thank you 😊
No worries.
Looks good to me @saM69420 , one suggestion would be to set
httpOnly
on the cookies to prevent them being accessed potentially by cross-site scripts client-side. For security, secure
and sameSite
are also good to set if appropriate for your app.Thanks @leo_kinde I've updated the cookie options. It doesn't work with Strict, so I set it to Lax. I guess that's because of the way the callback redirect works upon successful sign in.
Then client side, my react app is using react query to grab the user details from the server and keeps them i'm memory for
Infinity
which works since logout and login require complete redirects anyway.
Then any component can get the user's details by performing the query that's already been cached.
So no extra context providers and no client side processing of the tokens.
I would love any suggestions if there's room for improvement with any of this. Im trying to keep it as simple and robust as possible while only using the Kinde typescript SDK on the backend.Sounds like a reasonable approach @saM69420 , if the page is long lived with lots of interactivity, it could be worth rechecking auth periodically, but might not be relevant depending on the app.