prometheas πŸ”₯
prometheas πŸ”₯β€’4mo ago

Trouble using useQuery and convexQuery with queries that use ctx.auth.getUserIdentity()

So, like it says on the tin β€” I am unable to use the standard useQuery() (from convex/react) or the @convex-dev/react-query package's convexQuery() functions to call query() functions in my Convex API that check ctx.auth.getUserIdentity() as an auth guard mechanism. In other places in my nextjs project (server actions), I can invoke such query functions by adding a third argument with an auth token, like this:
import { auth } from '@clerk/nextjs/server';
import { fetchQuery } from 'convex/nextjs';

const token = (await auth().getToken({ template: 'convex' })) ?? null

const someResult = await fetchQuery(
api.messages.getLatest,
{ limit: 30 },
{ token } // <------ clerk auth token
);
import { auth } from '@clerk/nextjs/server';
import { fetchQuery } from 'convex/nextjs';

const token = (await auth().getToken({ template: 'convex' })) ?? null

const someResult = await fetchQuery(
api.messages.getLatest,
{ limit: 30 },
{ token } // <------ clerk auth token
);
Am I using the wrong approach for auth guarding the query funciton? Or is there some different mechanism for passing a token to the useQuery() and convexQuery() functions?
11 Replies
jamalsoueidan
jamalsoueidanβ€’4mo ago
token will automatically go to the server...on the server you can do simple check...
const userId = await getAuthUserId(ctx);
if (!userId ) {
throw new ConvexError("User must be logged in.");
}
const userId = await getAuthUserId(ctx);
if (!userId ) {
throw new ConvexError("User must be logged in.");
}
prometheas πŸ”₯
prometheas πŸ”₯OPβ€’4mo ago
Thanks for taking the time to attempt an assist, @jamalsoueidan πŸ™ … So, getAuthUserId(ctx) doesn't seem to be available in any package I'm using β€” to confirm, is that simply intended to be a generic name for a project-defined Convex function? The specific Convex API function I'm attempting unsuccessfully to use is ctx.auth.getUserIdentity() and finding β€” when invoked from client code β€” that it's returning null. And, in case it may be something I hadn't made clear enough, and may account for a difference in outcomes, I'm using Clerk auth… Given that you seem to believe that identity details ought to be figured out automatically, and the fact that NONE of Convex's React docs show explicit token passing, I'm starting to wonder if perhaps it's worth double-checking how my project's convex provider is configured in tandem with Clerk, as that bit was implemented by a developer no longer on the team.
jamalsoueidan
jamalsoueidanβ€’4mo ago
import { convexAuth, getAuthUserId } from "@convex-dev/auth/server";
prometheas πŸ”₯
prometheas πŸ”₯OPβ€’4mo ago
frst of all: YOU, @jamalsoueidan, are a champion community helper β€”Β i super appreciate you!
jamalsoueidan
jamalsoueidanβ€’4mo ago
Did you make it work?
prometheas πŸ”₯
prometheas πŸ”₯OPβ€’4mo ago
i had a look at the way convex and clerk were integrated… and it looked NOTHING like the examples
jamalsoueidan
jamalsoueidanβ€’4mo ago
you need a provider for the authentication on the frontend
prometheas πŸ”₯
prometheas πŸ”₯OPβ€’4mo ago
i have blown it all away and replaced it with what the Convex docs show, and β€” so far, so good β€” but i've got a bunch of testing around the rest of the app to ensure i haven't broken anything (sadly, our automated tests are lighter than I'd have preferred) yes, i know… but there was all sorts of insanse stuff going on there
jamalsoueidan
jamalsoueidanβ€’4mo ago
im using convex auth very simple, 3 linies.. const [convex] = useState(() => new ConvexReactClient(ENV.CONVEX_URL)); <ConvexAuthProvider client={convex}> {children} </ConvexAuthProvider>
prometheas πŸ”₯
prometheas πŸ”₯OPβ€’4mo ago
i would explain, but I think my attempting to do so would make anyone else dumber for having tried to read and understand that weirdness lol i do appreciate your assist, though, very much. i'll confirm in a bit on the outcomes
jamalsoueidan
jamalsoueidanβ€’4mo ago
yes good, i hope you get it to work.... usually on any project i start with auth πŸ˜„

Did you find this page helpful?