Envoy_
Envoy_10h ago

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}</>;
};
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.
5 Replies
Convex Bot
Convex Bot10h ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Envoy_
Envoy_OP10h ago
so when i log in there is a cookies present called - better-auth.session_token but when i refresh i get these two headers - better-auth.session_token - better-auth.convex_jwt
erquhart
erquhart6h ago
when you say headers you mean cookies right Recommend checking out the Convex doc for SSR w/ Next.js https://docs.convex.dev/client/react/nextjs/server-rendering
Envoy_
Envoy_OP5h ago
yes cookies, after login i get session_token cookie but not convex_jwt cookie, the jwt cookie comes only when i refresh
erquhart
erquhart2h ago
Oh really, what version of @convex-dev/better-auth do you have

Did you find this page helpful?