Karan5599
Karan55992d ago

Clerk With Convex Issue on SSR

const clerkJwt = await (await auth()).getToken({ template: "convex" });
console.log("env", process.env.NEXT_PUBLIC_CONVEX_URL);
try {
const currentUser = await fetchQuery(
api.schema.user.getCurrentUser,
{},
{
token: clerkJwt ?? undefined,
}
);
console.log("Current user:", currentUser);
} catch (error) {
console.error("Error fetching current user:", error);
return null;
}
const clerkJwt = await (await auth()).getToken({ template: "convex" });
console.log("env", process.env.NEXT_PUBLIC_CONVEX_URL);
try {
const currentUser = await fetchQuery(
api.schema.user.getCurrentUser,
{},
{
token: clerkJwt ?? undefined,
}
);
console.log("Current user:", currentUser);
} catch (error) {
console.error("Error fetching current user:", error);
return null;
}
i am running this function in Server component , and it throws the error saying fetch failed middleware.ts
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";

const isPublicRoute = createRouteMatcher(["/sign-in(.*)", "/sign-up(.*)", "/"]);
export default clerkMiddleware(async (auth, request) => {
const { sessionId } = await auth();
console.log("Session ID:", sessionId);
if (isPublicRoute(request)) {
if (sessionId) {
console.log("Redirecting to dashboard");
return NextResponse.redirect(new URL("/dashboard", request.url));
}
}
if (!isPublicRoute(request)) {
await auth.protect();
}
});
export const config = {
matcher: ["/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)", "/(api|trpc)(.*)"],
};
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";

const isPublicRoute = createRouteMatcher(["/sign-in(.*)", "/sign-up(.*)", "/"]);
export default clerkMiddleware(async (auth, request) => {
const { sessionId } = await auth();
console.log("Session ID:", sessionId);
if (isPublicRoute(request)) {
if (sessionId) {
console.log("Redirecting to dashboard");
return NextResponse.redirect(new URL("/dashboard", request.url));
}
}
if (!isPublicRoute(request)) {
await auth.protect();
}
});
export const config = {
matcher: ["/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)", "/(api|trpc)(.*)"],
};
/convex/user.ts
export const getCurrentUser = query({
args: {},
handler: async (ctx) => {
const user = await ctx.auth.getUserIdentity();
console.log("Current user:", user);
return user;
},
});
export const getCurrentUser = query({
args: {},
handler: async (ctx) => {
const user = await ctx.auth.getUserIdentity();
console.log("Current user:", user);
return user;
},
});
I have tried with preLoadQuery if i convert this to client component , it works fine
No description
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?