Envoy_
Envoy_2mo 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.
10 Replies
Convex Bot
Convex Bot2mo 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_OP2mo 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
erquhart2mo 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_OP2mo ago
yes cookies, after login i get session_token cookie but not convex_jwt cookie, the jwt cookie comes only when i refresh
erquhart
erquhart2mo ago
Oh really, what version of @convex-dev/better-auth do you have
Cole
Cole2mo ago
@erquhart this seems similar to the problem I’ve been having as well Wondering if it’s a race condition somewhere
erquhart
erquhart2mo ago
We specifically set that cookie for ssa via the convex plugin, need a repro case for when it fails as it works properly in the example
Envoy_
Envoy_OP2mo ago
Sorry i was away for long time, actually i was using Sign In With Ethereum(siwe) Plugin, with Local Install of Better Auth https://www.better-auth.com/docs/plugins/siwe so to create a session using siwe it sends request to /siwe/verify, i guess that's why jwt is not available without refresh. https://github.com/better-auth/better-auth/blob/canary/packages/better-auth/src/plugins/siwe/index.ts#L65C1-L66C1
erquhart
erquhart2mo ago
Ah, try 0.9.2, should work now
Envoy_
Envoy_OP2mo ago
yes it works now

Did you find this page helpful?