Oscar GalloO
Convex Community5mo ago
4 replies
Oscar Gallo

Uncaught Error: Unauthenticated at handler

I'm using NextJS with Clerk.
Every time I do logout with the Clerk <UserButton /> I get this error:
Uncaught Error: Unauthenticated at handler.

It happens in one screen where I'm making a query, what I imagine it happens is that in Signout I lost the session, and the query is there trying to fetch data, and it fails, but I'll expect the redirect to happen first. Is there a way to fix this?

This is my Provider:

"use client";

import { ReactNode } from "react";
import { ConvexReactClient } from "convex/react";
import { ConvexProviderWithClerk } from "convex/react-clerk";
import { useAuth } from "@clerk/nextjs";

if (!process.env.NEXT_PUBLIC_CONVEX_URL) {
  throw new Error("Missing NEXT_PUBLIC_CONVEX_URL in your .env file");
}

const convex = new ConvexReactClient(process.env.NEXT_PUBLIC_CONVEX_URL);

export default function ConvexClientProvider({
  children,
}: {
  children: ReactNode;
}) {
  return (
    <ConvexProviderWithClerk client={convex} useAuth={useAuth}>
      {children}
    </ConvexProviderWithClerk>
  );
}


This error happens too when I stop the server, run it again, and reload. And if I reload the page once more it works. supper weird.

Any recommendations are appreciated, thanks.
Was this page helpful?