Skip to content

Commit db5fd9e

Browse files
committed
backend
1 parent bef8c77 commit db5fd9e

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ BroCode Spot is a full-stack web application designed to streamline group orderi
8383
VITE_SUPABASE_URL=your_supabase_project_url
8484
VITE_SUPABASE_ANON_KEY=your_supabase_anon_key
8585
```
86+
### Environment Validation
87+
The backend validates environment variables at startup.
88+
If required variables are missing or invalid, the server will stop immediately with a clear error message.
8689

8790
4. **Set up Supabase**
8891

backend/env.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const dotenv = require("dotenv");
2+
const { z } = require("zod");
3+
4+
dotenv.config();
5+
6+
const envSchema = z.object({
7+
VITE_SUPABASE_URL: z.string().url(),
8+
VITE_SUPABASE_ANON_KEY: z.string().min(10),
9+
PORT: z.string().optional()
10+
});
11+
12+
const result = envSchema.safeParse(process.env);
13+
14+
if (!result.success) {
15+
console.error("\n❌ Invalid environment configuration:\n");
16+
17+
result.error.errors.forEach((err) => {
18+
console.error(`- ${err.path.join(".")}: ${err.message}`);
19+
});
20+
21+
process.exit(1); // 🔥 FAIL FAST
22+
}
23+
24+
module.exports = result.data;

backend/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createServer } from 'node:http';
22
import { URL } from 'node:url';
33
import { database, dbPath } from './db.js';
4+
require("./env");
45

56
const port = Number(process.env.PORT || 4000);
67

services/supabase.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import { createClient, RealtimeChannel } from '@supabase/supabase-js';
44
const supabaseUrl = import.meta.env.VITE_SUPABASE_URL || '';
55
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY || '';
66

7-
if (!supabaseUrl || !supabaseAnonKey) {
8-
console.warn(`Supabase credentials are not configured.
9-
Please create a .env.local file with:
10-
VITE_SUPABASE_URL=your_supabase_url
11-
VITE_SUPABASE_ANON_KEY=your_supabase_anon_key`);
7+
if (!supabaseUrl) {
8+
throw new Error(
9+
"Missing environment variable: VITE_SUPABASE_URL. Create a .env.local file."
10+
);
11+
}
12+
13+
if (!supabaseAnonKey) {
14+
throw new Error(
15+
"Missing environment variable: VITE_SUPABASE_ANON_KEY. Create a .env.local file."
16+
);
1217
}
1318

1419
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {

0 commit comments

Comments
 (0)