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:
the routes are the next:
my auth services are the next:
and my better-auth instance is this:
I'm using the token like:
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));