fawwazF
Convex Communityβ€’3y agoβ€’
2 replies
fawwaz

getUserIdentity from inside a query

hey all πŸ‘‹ , I have a query that is in a component wrapped in an auth gate. the Auth gate checks for authentication status and only then does it render the component with the query. However, looking at the logs, it looks like the query fires twice once the query handler can get the user Identity through ctx.auth.getUserIdentity but another time calling ctx.auth.getUserIdentity return null.

here is my auth gate component
const AuthGateProps: React.FC<AuthGatePropsProps> = ({ children }) => {
  const { isAuthenticated, isLoading } = useConvexAuth();
  if (isLoading) {
    return <div>Loading...</div>;
  }

  if (!isAuthenticated) return <div>Not authenticated</div>;
  console.log({ isAuthenticated });
  return <Authenticated>{children}</Authenticated>;
};


and the code for the query handler
export const get = query({
  args: {
    id: v.id("campaigns")
  },
  handler: async (ctx,args) => {
    const userIdentity = await ctx.auth.getUserIdentity()
    console.log("$$$userIdentity", userIdentity);
    if(!userIdentity){
      throw new Error("userIdentity not found")
    }
Was this page helpful?