Cookies Not setted in Browser Cookies Client is Next.js APP Route and Node.js Server is outside the Next APP #148484
Replies: 3 comments 2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Body
`import "dotenv/config";
import express from "express";
import cors from "cors";
import helmet from "helmet";
import { router, eventRouter } from "./routes/routes.js";
import session from "express-session";
import cookieParser from "cookie-parser";
import { rawBodyMiddleware } from "./Middleware/rawBody.js";
import initializeSqlConnection from "./DB/database.js";
import { addSqlConnection } from "./Middleware/SqlConnection.js";
const app = express();
const PORT = process.env.PORT || 3002;
app.use("/api/auth/stripe/webhook", rawBodyMiddleware);
app.use(helmet());
app.use(express.json());
const corsOptions = {
origin: [
"https://client.vercel.app",
// "https://opserver.vercel.app",
],
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
credentials: true,
optionsSuccessStatus: 200,
};
app.use(cors(corsOptions));
app.options("*", cors(corsOptions));
app.use(cookieParser());
app.use(
session({
secret: process.env.JWT_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV === 'production',
sameSite: "none",
maxAge: 3600000, // 1 hour
path: "/"
},
})
);
// Health check endpoint
app.get("/health", (req, res) => {
res.status(200).json({ status: "healthy" });
});
initializeSqlConnection();
app.use(addSqlConnection);
// Routes
app.get("/", (req, res) => {
res.send("AuraSphere");
});
app.use("/api/auth", router);
app.use("/api", eventRouter);
app.listen(PORT, () => {
console.log(
🚀 Server is Running On Port: ${PORT}
);});`
`const signin = async (req, res) => {
const { email, password } = req.body;
let connection = req.dbConnection;
try {
const [userResult] = await connection.execute(
"SELECT * FROM users WHERE email = ?",
[email]
);
} catch (error) {
console.error(error);
return res.status(500).json({
error: "Something went wrong, User signin failed",
success: false,
});
} finally {
if (connection) connection.release();
}
};`
the Cookie being set and after a reload/refresh the page cookies disappeared
I'm Hosting the the Server and Client Both on Vercel and i try to get better result i use docker but didn't work what is should need to do.
Client Page
` const handleSignIn = async (e) => {
e.preventDefault();
showLoading('Sign In...', true);
};`
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions