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
-H "Authorization: Bearer 9cwrI6O2vIiDvS3qUgEtc879pSWy1aRU" null% ``` Is this a bug, or do I have some misunderstanding?
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}`);
});
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"}}
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
can you try enabling bearer plugin if you dont ? add
bearer()
to your plugin in your backend auth config and check if this persists.