DaReal
DaReal6mo ago

Session Handling With Clerk

I am sure there is a solution for this but i don't know how to go about it. I am not currently doing any session handling in my project but I need two use cases where I believe i need to work with sessions. 1. I want to be able to know the number of currently active/online users by finding out the users who currently have an active session. Not sure if this is the right approach but i am open to ideas. 2. In clerk, i set a session lifetime to 5 minutes. If a user is currently on the site when the 5 minutes session lifetime counts down, the user is meant to be redirected to the login screen or the homepage but instead it throws an error. I am not sure how i should go about these but the only thing i noticed so far is that Clerk has webhook event triggers for sessions when they are created, ended, revoked or timed out. But i don't know if this is the right approach because i also saw a Convex stack article on session tracking. I will really appreciate the help. Thanks in advance
10 Replies
DaReal
DaRealOP6mo ago
Anyone to help?
erquhart
erquhart6mo ago
Both approaches are valid. Since Clerk is your source of truth for auth, using their event webhooks to trigger a logout makes sense.
DaReal
DaRealOP6mo ago
I understand that but when the session timesout, instead of redirecting to the login page, it throws an error and i have to refresh the page which then takes me to the login screen. Also how about finding out currently active/online users?
erquhart
erquhart6mo ago
There's a post on implementing presence in Convex here: https://stack.convex.dev/presence-with-convex
Stack
Implementing Presence with Convex
Some patterns for incorporating presence into a web app leveraging Convex, and sharing some tips & utilities I built along the way.
erquhart
erquhart6mo ago
As far as the error, I'm assuming whatever Convex queries the user has open require auth and are throwing when the session ends? If not, what errors are you seeing?
DaReal
DaRealOP6mo ago
Yes. That's exactly what happens.
erquhart
erquhart6mo ago
You'll want to use an error boundary to catch auth errors and redirect to login
erquhart
erquhart6mo ago
You can do this in a single boundary for your whole app.
DaReal
DaRealOP6mo ago
Thanks

Did you find this page helpful?