Envoy_E
Convex Community6mo ago
12 replies
Envoy_

No JWT Token when using better-auth with Next.js SSR on login

I am using better-auth component for authentication, i have protected routes in
/dashboard
which only allow authenticated users to access. I am using the following wrapper to handle this

import { redirect } from "next/navigation";

import type { PropsWithChildren } from "react";

import { api } from "@repo/backend/src/_generated/api";
import { fetchQuery } from "convex/nextjs";

import { getToken } from "@/lib/auth/server";

export const dynamic = "force-dynamic";

export const SignedIn = async ({ children }: PropsWithChildren) => {
  const token = await getToken();

  if (!token) redirect("/");

  const currentUser = await fetchQuery(api.auth.getCurrentUser, {}, { token });

  if (!currentUser) redirect("/");

  return <>{children}</>;
};


But when user logs in, and navigates to dashboard, token is undefined, it is undefined until i manually refresh the page.
Was this page helpful?