Express API postman testing - every request return null

I'm testing my API from postman and every request to logout or get-session response null. My signup and login endpoint are working correctly. My setup is the next: This is my auth controller: async function getSession(req: Request, res: Response) { const headers = req.headers; const session = await AuthServices.getSession(headers); if(session) { return res.status(200).json(session); } return res.status(404).json({ message: 'No session found' }); } async function Logout(req: Request, res: Response) { await AuthServices.Logout(req.headers); return res.status(200).json({ message: 'Logged out successfully' }); } the routes are the next: export const AuthRouter = Router(); AuthRouter.get("/api/v1/session", AuthController.getSession); AuthRouter.post("/api/v1/logout", AuthController.Logout); my auth services are the next: async function getSession (headers: IncomingHttpHeaders) { const session = await auth.api.getSession({ headers: fromNodeHeaders(headers), }); return session; } async function LoginEmailAndPassword(email: string, password: string) { const loginResult = await auth.api.signInEmail({ body: { email, password, }, }); return loginResult; } and my better-auth instance is this: const db = DB.getInstance().getDB(); export const auth = betterAuth({ database: mongodbAdapter(db, { client: DB.getInstance().getClient(), }), emailAndPassword: { enabled: true, requireEmailVerification: false }, plugins: [ openAPI() ] }) I'm using the token like: const myHeaders = new Headers(); myHeaders.append("Accept", "application/json"); myHeaders.append("Authorization", "Bearer MY_TOKEN"); const requestOptions = { method: "GET", headers: myHeaders, redirect: "follow" }; fetch("http://192.168.80.10:4200/api/v1/session", requestOptions) .then((response) => response.text()) .then((result) => console.log(result)) .catch((error) => console.error(error));
1 Reply
Kritius
KritiusOP13h ago
Oh, I can solve my problem. This was failed due to I wanted to use token like a bearer token by default. If you want use token as bearer token, you will need following these steps: https://www.better-auth.com/docs/plugins/bearer
Bearer Token Authentication | Better Auth
Authenticate API requests using Bearer tokens instead of browser cookies

Did you find this page helpful?