Better-Auth with Express Server

I set up basic Better-Auth in an Express server with sqlite database. Followings the docs I have auth.js
import { betterAuth } from "better-auth";
import Database from "better-sqlite3";

export const auth = betterAuth({
database: new Database("./sqlite.db"),
emailAndPassword: {
enabled: true,
},
secret: process.env.BETTER_AUTH_SECRET,
tokenTransport: "both",
});
import { betterAuth } from "better-auth";
import Database from "better-sqlite3";

export const auth = betterAuth({
database: new Database("./sqlite.db"),
emailAndPassword: {
enabled: true,
},
secret: process.env.BETTER_AUTH_SECRET,
tokenTransport: "both",
});
And index.js as ``` import { toNodeHandler } from "better-auth/node"; import { auth } from "./auth.js"; import { fromNodeHeaders } from "better-auth/node"; const app = express(); const port = 3000; app.all("/api/auth/*splat", toNodeHandler(auth)); app.get("/", (req, res) => { res.send("Hello World!"); }); app.get("/api/me", async (req, res) => { const headers = fromNodeHeaders(req.headers); const session = await auth.api.getSession({ headers }); return res.json(session); }); app.listen(port, () => { console.log(Server is running at http://localhost:${port}`); });
I can log in using curl
I can log in using curl
glebzvonkov@Glebs-MacBook-Pro backup_test % curl -X POST http://localhost:3000/api/auth/sign-up/email \ -H "Content-Type: application/json" \ -d '{"email": "[email protected]", "password": "strongpassword", "name": "Fresh User"}' {"token":"9cwrI6O2vIiDvS3qUgEtc879pSWy1aRU","user":{"id":"B6R5LKbb4FUyvVFYhuPpbKDLiymhXvYf","email":"[email protected]","name":"Fresh User","image":null,"emailVerified":false,"createdAt":"2025-04-17T18:00:59.787Z","updatedAt":"2025-04-17T18:00:59.787Z"}}
But when I try api.getSession route I always get null
But when I try api.getSession route I always get null
glebzvonkov@Glebs-MacBook-Pro backup_test % curl -X GET http://localhost:3000/api/me \
-H "Authorization: Bearer 9cwrI6O2vIiDvS3qUgEtc879pSWy1aRU" null% ``` Is this a bug, or do I have some misunderstanding?
1 Reply
KiNFiSH
KiNFiSH3d ago
can you try enabling bearer plugin if you dont ? add bearer() to your plugin in your backend auth config and check if this persists.

Did you find this page helpful?