diff --git a/app/Backend/config/passport-config.js b/app/Backend/config/passport-config.js index 26d2f29..8861f21 100644 --- a/app/Backend/config/passport-config.js +++ b/app/Backend/config/passport-config.js @@ -14,14 +14,20 @@ const getCleanEnvVar = (varName) => { // Get and validate Google OAuth credentials const clientID = getCleanEnvVar('GOOGLE_CLIENT_ID'); const clientSecret = getCleanEnvVar('GOOGLE_CLIENT_SECRET'); -const callbackURL = getCleanEnvVar('GOOGLE_CALLBACK_URL') || 'http://localhost:5000/auth/google/callback'; + +// Determine callback URL +let defaultCallback = 'http://localhost:3000/auth/google/callback'; +if (process.env.PORT) { + defaultCallback = `http://localhost:${process.env.PORT}/auth/google/callback`; +} +const callbackURL = getCleanEnvVar('GOOGLE_CALLBACK_URL') || defaultCallback; if (!clientID || !clientSecret) { console.error('\nāŒ Missing required Google OAuth environment variables!\n'); console.error('Add these to your .env file (WITHOUT quotes):'); console.error(' GOOGLE_CLIENT_ID=your_client_id_here'); console.error(' GOOGLE_CLIENT_SECRET=your_client_secret_here'); - console.error(' GOOGLE_CALLBACK_URL=http://localhost:5000/auth/google/callback'); + console.error(` GOOGLE_CALLBACK_URL=${defaultCallback}`); console.error(' SESSION_SECRET=your_random_secret\n'); throw new Error('Missing Google OAuth credentials'); } diff --git a/app/Backend/routes/auth.js b/app/Backend/routes/auth.js index 65498a7..72e2cd8 100644 --- a/app/Backend/routes/auth.js +++ b/app/Backend/routes/auth.js @@ -79,8 +79,11 @@ router.get('/check-name/:name', async (req, res) => { */ const checkUsernameHandler = async (req, res) => { try { + // Force JSON content type + res.setHeader('Content-Type', 'application/json'); + // accepting from query (GET) or body (POST) - const username = req.query.username || req.body.username || req.params.username; + const username = req.query.username || req.body.username || req.params.username || req.query.name || req.body.name; if (!username || username.trim().length < 2) { return res.status(400).json({ diff --git a/app/Backend/server.js b/app/Backend/server.js index 49f1b62..230cbb7 100644 --- a/app/Backend/server.js +++ b/app/Backend/server.js @@ -73,9 +73,34 @@ app.get("/debug/cookies", (req, res) => { }); // āœ… Configure CORS *before* Helmet or routes +const allowedOrigins = [ + "http://localhost:5173", + "http://localhost:3000", + "http://localhost:5000", +]; + +if (process.env.FRONTEND_URL) { + allowedOrigins.push(process.env.FRONTEND_URL); +} + app.use( cors({ - origin: process.env.FRONTEND_URL || "http://localhost:5173", + origin: function (origin, callback) { + // Allow requests with no origin (like mobile apps or curl requests) + if (!origin) return callback(null, true); + + if (allowedOrigins.includes(origin) || origin.endsWith('.vercel.app')) { + return callback(null, true); + } + + // In development, allow all + if (process.env.NODE_ENV !== 'production') { + return callback(null, true); + } + + const msg = 'The CORS policy for this site does not allow access from the specified Origin.'; + return callback(new Error(msg), false); + }, credentials: true, methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], allowedHeaders: ["Content-Type", "Authorization", "Cache-Control", "Pragma"], diff --git a/vercel.json b/vercel.json index 58a9143..7f48227 100644 --- a/vercel.json +++ b/vercel.json @@ -18,9 +18,20 @@ "src": "/api/(.*)", "dest": "/app/Backend/server.js" }, + { + "src": "/auth/(.*)", + "dest": "/app/Backend/server.js" + }, { "src": "/(.*)", "dest": "/app/frontend/$1" + }, + { + "handle": "filesystem" + }, + { + "src": "/(.*)", + "dest": "/app/frontend/index.html" } ] -} \ No newline at end of file +}