RoninR
Convex Community14mo ago
18 replies
Ronin

Race condition error upon await ctx.auth.getUserIdentity();[revised post]

Business Use Case: This condition was encountered by a new joiner trying to create an account on clarity and trying to join a space.

Observed Behavior:

The initial code for the getById function I wrote is as follows:

export const getById = query({
    args: {
        id: v.id("spaces")
    },
    handler: async(ctx, args) => {
        const identity = await ctx.auth.getUserIdentity();
        if(!identity){
            throw new Error("Not authenticated");
        }
        const user = await ctx.db
                .query("users")
                .withIndex("by_token", (q) => q.eq("tokenIdentifier", identity.tokenIdentifier))
                .unique();
        if(!user){
            throw new Error("User not found");
        }
        const member = await ctx.db
            .query("spaceMembers")
            .withIndex("by_space_id_user_id", (q) => 
                q.eq("spaceId", args.id).eq("userId", user._id)   
            )
            .unique();
        if(!member){
            return null;
        }
        return await ctx.db.get(args.id);
    }
});
393563507-87d11131-ad2f-4aa2-a7cf-076d88732974.jpg
393563573-be7eace4-387e-4511-ad57-dea6d04f0d9f.png
Was this page helpful?