NanaGaisie
NanaGaisie10mo ago

User Identity returns null when using useQuery

I am using nextjs for my project. When using useQuery in the client, the user identity initially returns the user object but returns null after a short while. The component keeps flickering alternating from authenticated state to non-authenticated state. Am I doing anything wrong?
3 Replies
NanaGaisie
NanaGaisieOP10mo ago
export const getAll = query({
handler: async (ctx) => {
const identity = await ctx.auth.getUserIdentity();
console.log({
email: identity?.email,
tokenIdentifier: identity?.tokenIdentifier,
});

const tokenIdentifier = identity?.tokenIdentifier;

if (!tokenIdentifier) {
throw new ConvexError({
message: "User not authenticated",
code: "NOT_AUTHENTICATED",
});
}

const user = await ctx.db
.query("user")
.withIndex("by_token", (q) => q.eq("tokenIdentifier", tokenIdentifier!))
.unique();

if (!user) {
throw new ConvexError({ message: "User not found", code: "NOT_FOUND" });
}

const workspaces = await ctx.db
.query("workspace")
.withIndex("by_user", (q) => q.eq("user", user._id))
.collect();

return workspaces;
},
});
export const getAll = query({
handler: async (ctx) => {
const identity = await ctx.auth.getUserIdentity();
console.log({
email: identity?.email,
tokenIdentifier: identity?.tokenIdentifier,
});

const tokenIdentifier = identity?.tokenIdentifier;

if (!tokenIdentifier) {
throw new ConvexError({
message: "User not authenticated",
code: "NOT_AUTHENTICATED",
});
}

const user = await ctx.db
.query("user")
.withIndex("by_token", (q) => q.eq("tokenIdentifier", tokenIdentifier!))
.unique();

if (!user) {
throw new ConvexError({ message: "User not found", code: "NOT_FOUND" });
}

const workspaces = await ctx.db
.query("workspace")
.withIndex("by_user", (q) => q.eq("user", user._id))
.collect();

return workspaces;
},
});
This is my query function.
Michal Srb
Michal Srb10mo ago
Hey @NanaGaisie, my guess is that your issue is with your provider setup. Follow https://docs.convex.dev/auth/debug and double check the guide you followed to set up auth. Also:
useMutation(api.user.store);
useMutation(api.user.store);
on its own doesn't do anything, you need to call the mutation from a useEffect (see https://docs.convex.dev/auth/database-auth#calling-storeuser-from-react)
Debugging Authentication | Convex Developer Hub
You have followed one of our authentication guides but something is not working.
Storing Users in the Convex Database | Convex Developer Hub
You might want to have a centralized place that stores information about the
ian
ian9mo ago
going to mark this as closed. also a lot of discussion in https://discord.com/channels/1019350475847499849/1225252977699455067

Did you find this page helpful?