NanaGaisie
NanaGaisie
CCConvex Community
Created by NanaGaisie on 3/17/2024 in #support-community
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?
4 replies
CCConvex Community
Created by NanaGaisie on 3/5/2024 in #support-community
Custom Auth With Kinde
I am working on a Nextjs project with Kinde for authentication and Convex for my backend. I am implementing custom auth by integrating Kinde. I have created my own custom auth provider to use with Kinde. The convex client seems not to fetch the JWT token although setAuth is called on the convex client. I get a null response when trying to log the user's identity to the client while a user is logged in. What am I doing wrong here?
5 replies
CCConvex Community
Created by NanaGaisie on 10/24/2023 in #support-community
Using Action
Hey, I am new to convex. I have written a mutation function for my application. The function needs to call an API to get the user ID of an individual. I get this error when I save my file. error: archive defined in documents.js is a Mutation function. Only actions can be defined in Node.js. I am not familiar with actions. Can someone help me?
export const archive = mutation({
args: { id: v.id("documents") },
handler: async (ctx, args) => {
const response = await fetch("http://localhost:3000/api/auth/kindeSession");
if (!response.ok) {
throw new Error("Failed to fetch user data");
}
const data = await response.json();
const usersId = data.user.id; // Extract the 'userId' from the response

const identity = usersId;

if (!identity) {
throw new Error("Not authenticated");
}

const userId = await identity;

const existingDocument = await ctx.db.get(args.id);

if (!existingDocument) {
throw new Error("Not found");
}

if (existingDocument.userId !== userId) {
throw new Error("Unauthorized");
}

const recursiveArchive = async (documentId: Id<"documents">) => {
const children = await ctx.db
.query("documents")
.withIndex("by_user_parent", (q) =>
q.eq("userId", userId).eq("parentDocument", documentId),
)
.collect();

for (const child of children) {
await ctx.db.patch(child._id, {
isArchived: true,
});

await recursiveArchive(child._id);
}
};

const document = await ctx.db.patch(args.id, {
isArchived: true,
});

recursiveArchive(args.id);

return document;
},
});
export const archive = mutation({
args: { id: v.id("documents") },
handler: async (ctx, args) => {
const response = await fetch("http://localhost:3000/api/auth/kindeSession");
if (!response.ok) {
throw new Error("Failed to fetch user data");
}
const data = await response.json();
const usersId = data.user.id; // Extract the 'userId' from the response

const identity = usersId;

if (!identity) {
throw new Error("Not authenticated");
}

const userId = await identity;

const existingDocument = await ctx.db.get(args.id);

if (!existingDocument) {
throw new Error("Not found");
}

if (existingDocument.userId !== userId) {
throw new Error("Unauthorized");
}

const recursiveArchive = async (documentId: Id<"documents">) => {
const children = await ctx.db
.query("documents")
.withIndex("by_user_parent", (q) =>
q.eq("userId", userId).eq("parentDocument", documentId),
)
.collect();

for (const child of children) {
await ctx.db.patch(child._id, {
isArchived: true,
});

await recursiveArchive(child._id);
}
};

const document = await ctx.db.patch(args.id, {
isArchived: true,
});

recursiveArchive(args.id);

return document;
},
});
3 replies