fawwaz
fawwaz•16mo ago

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>;
};
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")
}
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")
}
2 Replies
Michal Srb
Michal Srb•16mo ago
Hey @fawwaz the code you shared looks correct. Is it possible that the query is called from somewhere else than the gated component?
fawwaz
fawwazOP•16mo ago
hey @Michal Srb thanks a bunch it turns out indeed the somewhere else the request was firing, all good now 🙂

Did you find this page helpful?