session always returns null

First post here! Any help/correction would be appreciated!! When i try to get the session using authClient.getSession(), for localhost (backend) I am able to retrieve the sessions details. But when my backend application is deployed on a linode server, session is always null. I noticed that when my application (backend) was running locally the Cookie was present in the request headers and am able to get session data. But when this same application (backend) was hosted on Linode the Cookie was not present in the request headers and session is always null. I'm using React and hosting it on Vercel. hono:
app.use(
"/api/*",
cors({
origin: [
"http://localhost:5173",
"http://localhost:4173",
"https://my-app.vercel.app",
"https://my-app-dev.vercel.app",
],
allowHeaders: ["Content-Type", "Authorization", "Cookie"],
allowMethods: ["POST", "GET", "OPTIONS"],
exposeHeaders: ["Content-Length", "Set-Cookie"],
maxAge: 600,
credentials: true,
})
)
app.use(
"/api/*",
cors({
origin: [
"http://localhost:5173",
"http://localhost:4173",
"https://my-app.vercel.app",
"https://my-app-dev.vercel.app",
],
allowHeaders: ["Content-Type", "Authorization", "Cookie"],
allowMethods: ["POST", "GET", "OPTIONS"],
exposeHeaders: ["Content-Length", "Set-Cookie"],
maxAge: 600,
credentials: true,
})
)
react:
export const getUserSession = async () => {
const session = await authClient.getSession()
if (!session.data) return null
return session.data
}
export const getUserSession = async () => {
const session = await authClient.getSession()
if (!session.data) return null
return session.data
}
and then using it in the __root.tsx (tanstack router) in this way :
export const Route = createRootRouteWithContext<{
queryClient: QueryClient
}>()({
beforeLoad: async ({ context }) => {
const userSession = await context.queryClient.fetchQuery({
queryKey: ["auth", "user"],
queryFn: getUserSession,
staleTime: 5000,
})
return { userSession }
},
component: RootComponent,
})
export const Route = createRootRouteWithContext<{
queryClient: QueryClient
}>()({
beforeLoad: async ({ context }) => {
const userSession = await context.queryClient.fetchQuery({
queryKey: ["auth", "user"],
queryFn: getUserSession,
staleTime: 5000,
})
return { userSession }
},
component: RootComponent,
})
to make use of this in all my other routes wherever necessary. Any help would be appreciated! Thanks in advance 🙏
1 Reply
Hakai
HakaiOP5mo ago
import { betterAuth } from "better-auth"
import {
openAPI,
admin as adminPlugin,
organization as organizationPlugin,
} from "better-auth/plugins"
import { drizzleAdapter } from "better-auth/adapters/drizzle"
import db from "@/db"
import { z } from "zod"

export const auth = betterAuth({
appName: "my-app
",

database: drizzleAdapter(db, {
provider: "pg",
}),

user: {
additionalFields: {
phoneNumber: {
type: "string",
input: true,
unique: true,
required: true,
validator: {
input: z.string().regex(/^\d{10}$/, "Invalid phone number"),
},
},
countryCode: {
type: "string",
input: true,
required: true,
validator: {
input: z.string().regex(/^\+\d{1,4}$/, "Invalid country code"),
},
},
},
},

emailAndPassword: {
enabled: true,
autoSignIn: false,
minPasswordLength: 8,
maxPasswordLength: 32,
},

plugins: [openAPI(), adminPlugin(), organizationPlugin()],

trustedOrigins: [
"http://localhost:5173",
"http://localhost:4173",
"https://my-app.vercel.app",
"https://my-app-dev.vercel.app",
],
})
import { betterAuth } from "better-auth"
import {
openAPI,
admin as adminPlugin,
organization as organizationPlugin,
} from "better-auth/plugins"
import { drizzleAdapter } from "better-auth/adapters/drizzle"
import db from "@/db"
import { z } from "zod"

export const auth = betterAuth({
appName: "my-app
",

database: drizzleAdapter(db, {
provider: "pg",
}),

user: {
additionalFields: {
phoneNumber: {
type: "string",
input: true,
unique: true,
required: true,
validator: {
input: z.string().regex(/^\d{10}$/, "Invalid phone number"),
},
},
countryCode: {
type: "string",
input: true,
required: true,
validator: {
input: z.string().regex(/^\+\d{1,4}$/, "Invalid country code"),
},
},
},
},

emailAndPassword: {
enabled: true,
autoSignIn: false,
minPasswordLength: 8,
maxPasswordLength: 32,
},

plugins: [openAPI(), adminPlugin(), organizationPlugin()],

trustedOrigins: [
"http://localhost:5173",
"http://localhost:4173",
"https://my-app.vercel.app",
"https://my-app-dev.vercel.app",
],
})

Did you find this page helpful?