antes de alterar login
This commit is contained in:
@@ -1,22 +1,53 @@
|
||||
import { initializeApp, getApps } from "firebase/app";
|
||||
import { initializeApp, getApps, getApp } from "firebase/app";
|
||||
import { getAuth } from "firebase/auth";
|
||||
import { getDatabase } from "firebase/database";
|
||||
|
||||
// As variáveis de ambiente devem ser configuradas no Vercel e no ficheiro .env.local
|
||||
const firebaseConfig = {
|
||||
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY || "AIzaSyCPz7Pd3tJj3QkF7fV_vudCJythNsyR57k",
|
||||
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN || "namesa-429c1.firebaseapp.com",
|
||||
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID || "namesa-429c1",
|
||||
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET || "namesa-429c1.firebasestorage.app",
|
||||
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID || "476421715902",
|
||||
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID || "1:476421715902:web:placeholder", // placeholder needed for web client SDK
|
||||
// Nota importante: Como verificado na codebase Android,
|
||||
// O ReservaMesa usa Realtime Database e não Firestore.
|
||||
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
|
||||
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
|
||||
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
|
||||
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
|
||||
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
|
||||
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
|
||||
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL || "https://namesa-429c1-default-rtdb.firebaseio.com"
|
||||
};
|
||||
|
||||
// Initialize Firebase only if there are no apps initialized yet
|
||||
const app = getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0];
|
||||
// Validate that all required environment variables are present
|
||||
const requiredEnvVars = [
|
||||
"NEXT_PUBLIC_FIREBASE_API_KEY",
|
||||
"NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN",
|
||||
"NEXT_PUBLIC_FIREBASE_PROJECT_ID",
|
||||
"NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET",
|
||||
"NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID",
|
||||
"NEXT_PUBLIC_FIREBASE_APP_ID",
|
||||
];
|
||||
|
||||
const missingVars = requiredEnvVars.filter(
|
||||
(key) => !process.env[key]
|
||||
);
|
||||
|
||||
if (missingVars.length > 0) {
|
||||
console.error(
|
||||
"[Firebase] Missing environment variables:",
|
||||
missingVars.join(", "),
|
||||
"\nPlease ensure all Firebase environment variables are set in .env.local or your hosting platform."
|
||||
);
|
||||
}
|
||||
|
||||
// Initialize Firebase only once — prevents duplicate app errors in development (HMR) and SSR
|
||||
let app;
|
||||
try {
|
||||
app = getApps().length === 0 ? initializeApp(firebaseConfig) : getApp();
|
||||
} catch (error: any) {
|
||||
if (error?.code === "app/duplicate-app") {
|
||||
app = getApp();
|
||||
} else {
|
||||
throw new Error(
|
||||
`[Firebase] Failed to initialize: ${error?.message || "Unknown error"}. Check your environment variables.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export const auth = getAuth(app);
|
||||
export const db = getDatabase(app);
|
||||
|
||||
Reference in New Issue
Block a user