npm i cookie-parser
import cookieParser from "cookie-parser";
app.use(cookieParser());
app.use("/api/v1/auth", auth);
import { getMe, login, register } from "../controller/auth.js";
router.post("/register", register);
export default router;
const register = asyncHandler(async (req, res, next) => {
const { name, email, password, role } = req.body;
const user = await User.create({
name,
email,
password,
role,
});
sendTokenResponse(user, 200, res);
});
const sendTokenResponse = (user, statusCode, res) => {
const token = user.getSignedJwtToken();
const options = {
expires: new Date(Date.now() + process.env.JWT_COOKIE_EXPIRE * 24 * 60 * 60 * 1000),
httpOnly: true,
};
if (process.env.NODE_ENV === "production") {
options.secure = true;
}
res.status(statusCode).cookie("token", token, options).json({ sucess: true, token });
};