Elysia + Next.js: No session data on client

Hi, I’m using Better Auth with Elysia on the back-end and an auth-client on the front-end with Next.js. The login flow seems to be working correctly (I’m using the Microsoft provider), but the user session is not being persisted. After logging in, the session data always returns as: { "data": null, "error": null } My question is: shouldn’t the session data be available right after login, or do I need to explicitly fetch it from a dedicated endpoint? client:
export const authClient = createAuthClient({
baseURL: "http://localhost:3000",
plugins: [
adminClient(),
organizationClient()
]
})

const handleAzureLogin = async () => {
await authClient.signIn.social({
provider: "microsoft",
callbackURL: "http://localhost:3001/"
});
};
export const authClient = createAuthClient({
baseURL: "http://localhost:3000",
plugins: [
adminClient(),
organizationClient()
]
})

const handleAzureLogin = async () => {
await authClient.signIn.social({
provider: "microsoft",
callbackURL: "http://localhost:3001/"
});
};
server:
export const auth = betterAuth({
trustedOrigins: ["http://localhost:3001"],
database: prismaAdapter(prisma, { provider: "sqlserver" }),
socialProviders: {
microsoft: {
clientId: process.env.MICROSOFT_CLIENT_ID || "",
clientSecret: process.env.MICROSOFT_CLIENT_SECRET || "",
},
},
plugins: [
admin(),
organization(),
]
});

const betterAuth = new Elysia({ name: "better-auth" })
.mount(auth.handler)
.macro({
auth: {
async resolve({ status, request: { headers } }) {
const session = await auth.api.getSession({
headers,
});
console.log(session)
if (!session) return status(401);
return {
user: session.user,
session: session.session,
};
},
},
});

const app = new Elysia()
.use(betterAuth)
.use(
cors({
origin: "http://localhost:3001",
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
credentials: true,
allowedHeaders: ["Content-Type", "Authorization"],
}),
)
.get("/user", ({ user }) => user, {
auth: true,
})
.listen(3000);

console.log(
`Server running at ${app.server?.hostname}:${app.server?.port}`
);
export const auth = betterAuth({
trustedOrigins: ["http://localhost:3001"],
database: prismaAdapter(prisma, { provider: "sqlserver" }),
socialProviders: {
microsoft: {
clientId: process.env.MICROSOFT_CLIENT_ID || "",
clientSecret: process.env.MICROSOFT_CLIENT_SECRET || "",
},
},
plugins: [
admin(),
organization(),
]
});

const betterAuth = new Elysia({ name: "better-auth" })
.mount(auth.handler)
.macro({
auth: {
async resolve({ status, request: { headers } }) {
const session = await auth.api.getSession({
headers,
});
console.log(session)
if (!session) return status(401);
return {
user: session.user,
session: session.session,
};
},
},
});

const app = new Elysia()
.use(betterAuth)
.use(
cors({
origin: "http://localhost:3001",
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
credentials: true,
allowedHeaders: ["Content-Type", "Authorization"],
}),
)
.get("/user", ({ user }) => user, {
auth: true,
})
.listen(3000);

console.log(
`Server running at ${app.server?.hostname}:${app.server?.port}`
);
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?