Eliminating redundant auth checks
hi, I'm repeating this piece of code in basically every convex function to check if the user is logged in (first check before any other check for authorizations).
is this best practice? (I'm using Clerk)
19 Replies
Hi!
Eventually, most teams roll this into something like customFunctions from convex-helpers
see this article: https://stack.convex.dev/custom-functions
Customizing serverless functions without middleware
Re-use code and centralize request handler definitions with discoverability and type safety and without the indirection of middleware or nesting of wr...
that allows you to generate a custom
query
builder that automatically enforces authentication for any given handlerthanks! will look into this
related... I don't think I understood how to make sure if the user is logged in.
Is the following code enough?
Because if I don't check the "act as a user" checkbox in the dashboard it returns null. But if I check it and leave as default it doesn't return null, even though I didn't set a valid "subject"

@stefano
I am going to try and answer your question, not sure if thats what you need. But I'd recommend throwing an error when a user is not authenticated and catching it on your frontend. Moreover I have something like the following setup for my projects: And now instead of using
I am going to try and answer your question, not sure if thats what you need. But I'd recommend throwing an error when a user is not authenticated and catching it on your frontend. Moreover I have something like the following setup for my projects: And now instead of using
query
from convex use the exported query
from aboveHow do I handle it in Front End?
If I use
useQuery (/convex/react
I keep getting the Convex Error in the UI:
Error: [CONVEX Q(example:getData)] [Request ID: ] Server Error
Uncaught Error: Authentication required
.
example.ts
Make sure you are using the exported query and I’d recommend wrapping your component which queries convex db in an Authenticated component @Ica
Thanks. Managed to make it work as you suggested.
what do you mean by "Authenticated component"?
Module: react | Convex Developer Hub
Tools to integrate Convex into React applications.
Module: react | Convex Developer Hub
Tools to integrate Convex into React applications.
any example code you can point me at?
it's still not clear to me why would I need to authenticate a component if I check the user's authentication at root page level.
I use Clerk for my application, so I have a
middleware.ts
file where I defined the protected routes of my application:
Depends on how you are checking it at the root level? Are you loading the children only if authenticated?
Exactly. If not authenticated for the specified routes in middleware.ts, the application redirects the user to the login page
What’s the exact problem you are facing? Can you please elaborate?
No problem here, I'm just looking to understand if I need to wrap every component that fetches something from Convex DB with the
Authenticated
react moduleYou definitely don’t need to use it. Like I have been using the above snippet I put, and that’s been just working fine. It’s only if sometimes (depending on how you write and use your query), the auth state or user is not set in time and authenticated component helps you put a nice loading state or whatever one wants
ok got it, thanks!