How to fetch session from auth server

Hello! I am loving better-auth so far, however, I am having one problem: I have an auth server with the following auth files:
// lib/auth.ts (server)
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "~/server/db";
import { env } from "~/env";

const origins: string[] = [];

if (env.NODE_ENV === "development") {
origins.push("http://localhost:3001");
}

export const auth = betterAuth({
appName: "4uAuth",
baseURL: env.BETTER_AUTH_BASE_URL,
database: drizzleAdapter(db, { provider: "mysql" }),
emailAndPassword: {
enabled: true,
},
socialProviders: {
google: {
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
},
linkedin: {
clientId: env.LINKEDIN_CLIENT_ID,
clientSecret: env.LINKEDIN_CLIENT_SECRET,
},
microsoft: {
clientId: env.MICROSOFT_CLIENT_ID,
clientSecret: env.MICROSOFT_CLIENT_SECRET,
},
},
advanced: {
crossSubDomainCookies: {
enabled: true,
},
},
trustedOrigins: origins,
});
// lib/auth.ts (server)
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "~/server/db";
import { env } from "~/env";

const origins: string[] = [];

if (env.NODE_ENV === "development") {
origins.push("http://localhost:3001");
}

export const auth = betterAuth({
appName: "4uAuth",
baseURL: env.BETTER_AUTH_BASE_URL,
database: drizzleAdapter(db, { provider: "mysql" }),
emailAndPassword: {
enabled: true,
},
socialProviders: {
google: {
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
},
linkedin: {
clientId: env.LINKEDIN_CLIENT_ID,
clientSecret: env.LINKEDIN_CLIENT_SECRET,
},
microsoft: {
clientId: env.MICROSOFT_CLIENT_ID,
clientSecret: env.MICROSOFT_CLIENT_SECRET,
},
},
advanced: {
crossSubDomainCookies: {
enabled: true,
},
},
trustedOrigins: origins,
});
// lib/client/auth.ts (client)
import { createAuthClient } from "better-auth/react";

export const authClient = createAuthClient();
// lib/client/auth.ts (client)
import { createAuthClient } from "better-auth/react";

export const authClient = createAuthClient();
How would I fetch a session from the server in a different application? When I attempt to do so, the endpoint goes through and everything works, however, the response is blank, versus when I hit the endpoint directly I get a response with the session. Is there something wrong with my server or application configuration? If so, how do I fix it? Thanks!
2 Replies
bekacru
bekacru2w ago
better auth works with cookies by default. If your app isn't sending a proper cookie, it wont be able to read the token and return a session. if you're not using cookies, you can check the bearer plugin for authorization header auth.
Ryder Horne
Ryder HorneOP2w ago
So how would I send the proper cookie after it redirects using the callback URL? Is that possible yet? I’m not sure if I mentioned but my application is also using better-auth.

Did you find this page helpful?