diff --git a/README.md b/README.md index 234914d..9e45d4f 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,9 @@ BroCode Spot is a full-stack web application designed to streamline group orderi VITE_SUPABASE_URL=your_supabase_project_url VITE_SUPABASE_ANON_KEY=your_supabase_anon_key ``` + ### Environment Validation + The backend validates environment variables at startup. + If required variables are missing or invalid, the server will stop immediately with a clear error message. 4. **Set up Supabase** diff --git a/backend/env.js b/backend/env.js new file mode 100644 index 0000000..b2358c9 --- /dev/null +++ b/backend/env.js @@ -0,0 +1,24 @@ +const dotenv = require("dotenv"); +const { z } = require("zod"); + +dotenv.config(); + +const envSchema = z.object({ + VITE_SUPABASE_URL: z.string().url(), + VITE_SUPABASE_ANON_KEY: z.string().min(10), + PORT: z.string().optional() +}); + +const result = envSchema.safeParse(process.env); + +if (!result.success) { + console.error("\nāŒ Invalid environment configuration:\n"); + + result.error.errors.forEach((err) => { + console.error(`- ${err.path.join(".")}: ${err.message}`); + }); + + process.exit(1); // šŸ”„ FAIL FAST +} + +module.exports = result.data; \ No newline at end of file diff --git a/backend/server.js b/backend/server.js index d4a0843..96f5af8 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1,6 +1,7 @@ import { createServer } from 'node:http'; import { URL } from 'node:url'; import { database, dbPath } from './db.js'; +require("./env"); const port = Number(process.env.PORT || 4000); diff --git a/services/supabase.ts b/services/supabase.ts index 94fa17e..a160b88 100644 --- a/services/supabase.ts +++ b/services/supabase.ts @@ -4,11 +4,16 @@ import { createClient, RealtimeChannel } from '@supabase/supabase-js'; const supabaseUrl = import.meta.env.VITE_SUPABASE_URL || ''; const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY || ''; -if (!supabaseUrl || !supabaseAnonKey) { - console.warn(`Supabase credentials are not configured. - Please create a .env.local file with: - VITE_SUPABASE_URL=your_supabase_url - VITE_SUPABASE_ANON_KEY=your_supabase_anon_key`); +if (!supabaseUrl) { + throw new Error( + "Missing environment variable: VITE_SUPABASE_URL. Create a .env.local file." + ); +} + +if (!supabaseAnonKey) { + throw new Error( + "Missing environment variable: VITE_SUPABASE_ANON_KEY. Create a .env.local file." + ); } export const supabase = createClient(supabaseUrl, supabaseAnonKey, {