diff --git a/convex/auth.config.ts b/convex/auth.config.ts index 552efd9b..cf844f3b 100644 --- a/convex/auth.config.ts +++ b/convex/auth.config.ts @@ -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", }, ], diff --git a/convex/helpers.ts b/convex/helpers.ts index 1d92f10c..bf1e8b30 100644 --- a/convex/helpers.ts +++ b/convex/helpers.ts @@ -3,8 +3,21 @@ import { QueryCtx, MutationCtx } from "./_generated/server"; export async function getCurrentUserId( ctx: QueryCtx | MutationCtx ): Promise { - 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( @@ -12,6 +25,11 @@ export async function requireAuth( ): Promise { 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; diff --git a/src/components/convex-provider.tsx b/src/components/convex-provider.tsx index 5f0a32f4..6bee2b92 100644 --- a/src/components/convex-provider.tsx +++ b/src/components/convex-provider.tsx @@ -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 }) {