diff --git a/Admin/src/App.jsx b/Admin/src/App.jsx index 5adb23c..f56e8f7 100644 --- a/Admin/src/App.jsx +++ b/Admin/src/App.jsx @@ -28,9 +28,10 @@ function App() { try { const response = await axios.post( - "http://localhost:3000/api/v1/admin/checkToken", + `${import.meta.env.VITE_SERVER_URL}/api/v1/admin/checkToken`, {}, { + withCredentials : true , headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", diff --git a/Admin/src/Components/AddEvents/AddEvents.jsx b/Admin/src/Components/AddEvents/AddEvents.jsx index 30b159d..a5a2677 100644 --- a/Admin/src/Components/AddEvents/AddEvents.jsx +++ b/Admin/src/Components/AddEvents/AddEvents.jsx @@ -42,6 +42,7 @@ const AddEvents = () => { }; const response = await axios.post(`${import.meta.env.VITE_SERVER_URL}/api/v1/admin/event`, eventData, { + withCredentials : true , headers: { "Authorization": `Bearer ${localStorage.getItem("token")}`, "Content-Type": "application/json", diff --git a/Admin/src/Login-Register/Login/Login.jsx b/Admin/src/Login-Register/Login/Login.jsx index f0ea70a..b56c392 100644 --- a/Admin/src/Login-Register/Login/Login.jsx +++ b/Admin/src/Login-Register/Login/Login.jsx @@ -13,7 +13,9 @@ const Login = () => { if (loginData.email === "" || loginData.password === "") return; try { - const payload = await axios.post(`${import.meta.env.VITE_SERVER_URL}/api/v1/admin/signin`, loginData); + const payload = await axios.post(`${import.meta.env.VITE_SERVER_URL}/api/v1/admin/signin`, loginData , { + withCredentials: true, + }); if (payload.status === 200) { localStorage.setItem("token", payload.data.token); localStorage.removeItem("events") diff --git a/Admin/src/Login-Register/Register/Register.jsx b/Admin/src/Login-Register/Register/Register.jsx index cc942dd..aca4d5e 100644 --- a/Admin/src/Login-Register/Register/Register.jsx +++ b/Admin/src/Login-Register/Register/Register.jsx @@ -42,7 +42,9 @@ const Register = () => { // console.log("what is the problem") const payload = await axios.post( `${import.meta.env.VITE_SERVER_URL}/api/v1/admin/signup`, - registerData + registerData , { + withCredentials: true, + } ); if (payload.status === 200) { toast.success("Account created successfully!"); diff --git a/Admin/vite.config.js b/Admin/vite.config.js index 2328e17..ff56d59 100644 --- a/Admin/vite.config.js +++ b/Admin/vite.config.js @@ -3,5 +3,6 @@ import react from '@vitejs/plugin-react-swc' // https://vite.dev/config/ export default defineConfig({ + outDir: "dist", plugins: [react()], }) diff --git a/Backend/server.js b/Backend/server.js index 24cba22..ac4c11d 100644 --- a/Backend/server.js +++ b/Backend/server.js @@ -6,20 +6,35 @@ import cookieParser from "cookie-parser"; import cors from 'cors' import userRouter from "./routes/user.js"; const app = express(); -const allowedOrigins = ["https://hyd-events-website-admin.onrender.com"]; - -app.use( - cors({ - origin: function (origin, callback) { - if (!origin || allowedOrigins.includes(origin)) { - callback(null, true); - } else { - callback(new Error("Not allowed by CORS")); - } - }, - credentials: true, // Allow cookies & authentication headers if needed - }) -); +const port = process.env.PORT || 3000 ; + +// Improved CORS configuration +const allowedOrigins = [ + "https://hyd-events-website-admin.onrender.com", + "https://hydevents-main.onrender.com", + "https://hyd-events-website.onrender.com", +]; + +app.use((req, res, next) => { + const origin = req.headers.origin; + + // Check if the origin is in our allowed list + if (allowedOrigins.includes(origin)) { + res.setHeader("Access-Control-Allow-Origin", origin); + res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); + res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); + res.setHeader("Access-Control-Allow-Credentials", "true"); + } + + // Handle preflight requests + if (req.method === "OPTIONS") { + res.writeHead(204); + res.end(); + return; + } + + next(); +}); app.use(cookieParser()); app.use(express.json()); @@ -27,8 +42,8 @@ app.use(express.json()); app.use("/api/v1/admin", adminRouter); app.use("/api/v1/user" , userRouter); -app.listen(3000, () => { - console.log("Server is running on port 3000"); +app.listen(port , () => { + console.log(`Server is running on port ${port}`); }); diff --git a/Frontend/vite.config.js b/Frontend/vite.config.js index 861b04b..709612d 100644 --- a/Frontend/vite.config.js +++ b/Frontend/vite.config.js @@ -3,5 +3,6 @@ import react from '@vitejs/plugin-react-swc' // https://vitejs.dev/config/ export default defineConfig({ + outDir: "dist", plugins: [react()], })