Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion convex/auth.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const jwtIssuerDomain = process.env.CLERK_JWT_ISSUER_DOMAIN;

if (!jwtIssuerDomain) {
console.warn("CLERK_JWT_ISSUER_DOMAIN environment variable is not set");
}

export default {
providers: [
{
domain: process.env.CLERK_JWT_ISSUER_DOMAIN!,
domain: jwtIssuerDomain || "placeholder.convex.cloud",
applicationID: "convex",
},
],
Expand Down
22 changes: 20 additions & 2 deletions convex/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,33 @@ import { QueryCtx, MutationCtx } from "./_generated/server";
export async function getCurrentUserId(
ctx: QueryCtx | MutationCtx
): Promise<string | null> {
const identity = await ctx.auth.getUserIdentity();
return identity?.subject || null;
try {
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
console.warn("No user identity found in context");
return null;
}
if (!identity.subject) {
console.warn("User identity found but no subject field");
return null;
}
return identity.subject;
} catch (error) {
console.error("Error getting user identity:", error);
return null;
}
}

export async function requireAuth(
ctx: QueryCtx | MutationCtx
): Promise<string> {
const userId = await getCurrentUserId(ctx);
if (!userId) {
console.error("Authentication failed: No user ID found");
console.error("Context auth details:", {
hasAuth: !!ctx.auth,
hasGetUserIdentity: typeof ctx.auth?.getUserIdentity === 'function'
});
throw new Error("Unauthorized");
}
return userId;
Expand Down
3 changes: 1 addition & 2 deletions src/components/convex-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import type { ReactNode } from "react";
const convexUrl = process.env.NEXT_PUBLIC_CONVEX_URL;

const convexClient = new ConvexReactClient(
convexUrl || "https://placeholder.convex.cloud",
{ expectAuth: false }
convexUrl || "https://placeholder.convex.cloud"
);

export function ConvexClientProvider({ children }: { children: ReactNode }) {
Expand Down
Loading