How to set cookie in Express?
I am trying to use the better-auth api methods, I followed the docs and I did everything that was mentioned, but everytime I try to sign-in or sign-up, I find that the cookies are missing, I will share with you the code I have in case I made a mistake and I am not conscious of it:
// handlers.ts
import type { Response } from "express";
import type { SignInRequest, SignUpRequest } from "./request-types";
import { auth } from "@/lib/auth";
import { APIError } from "better-auth/api";
import { setCookieToHeader } from "better-auth/cookies";
import { fromNodeHeaders } from "better-auth/node";
export const signIn = async (req: SignInRequest, res: Response) => {
try {
const { university_email, password } = req.body;
const response = await auth.api.signInEmail({
headers: fromNodeHeaders(req.headers),
asResponse: true,
returnHeaders: true,
body: {
email: university_email,
password: password,
}
});
setCookieToHeader(response.headers); // I just added this in hope it works
res.status(response.status).json(response.body);
} catch (error) {
if (error instanceof APIError) {
console.error("Sign-in error:", error.body);
res.status(error.statusCode).json({ message: error.body?.message || "An error occurred during sign-in." });
return;
}
console.error("Unexpected sign-in error:", error);
res.status(500).json({ message: "An internal server error occurred." });
}
}
// index.ts
import express from "express";
import cookieParser from "cookie-parser";
import cors from "cors";
import authRouter from "@/services/auth/routes"
import config from "@/config/config";
const authApp = express();
authApp.use(cookieParser());
authApp.use(express.urlencoded({ extended: true }));
authApp.use(
cors({
origin: config.server.corsOrigins,
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
credentials: true,
})
);
authApp.use("/api/auth", authRouter);
export { authApp };// handlers.ts
import type { Response } from "express";
import type { SignInRequest, SignUpRequest } from "./request-types";
import { auth } from "@/lib/auth";
import { APIError } from "better-auth/api";
import { setCookieToHeader } from "better-auth/cookies";
import { fromNodeHeaders } from "better-auth/node";
export const signIn = async (req: SignInRequest, res: Response) => {
try {
const { university_email, password } = req.body;
const response = await auth.api.signInEmail({
headers: fromNodeHeaders(req.headers),
asResponse: true,
returnHeaders: true,
body: {
email: university_email,
password: password,
}
});
setCookieToHeader(response.headers); // I just added this in hope it works
res.status(response.status).json(response.body);
} catch (error) {
if (error instanceof APIError) {
console.error("Sign-in error:", error.body);
res.status(error.statusCode).json({ message: error.body?.message || "An error occurred during sign-in." });
return;
}
console.error("Unexpected sign-in error:", error);
res.status(500).json({ message: "An internal server error occurred." });
}
}
// index.ts
import express from "express";
import cookieParser from "cookie-parser";
import cors from "cors";
import authRouter from "@/services/auth/routes"
import config from "@/config/config";
const authApp = express();
authApp.use(cookieParser());
authApp.use(express.urlencoded({ extended: true }));
authApp.use(
cors({
origin: config.server.corsOrigins,
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
credentials: true,
})
);
authApp.use("/api/auth", authRouter);
export { authApp };