Firebase server setup in Next JS

// src/firebase.js’;

import { initializeApp } from ‘firebase/app’;

import { getAuth, signInWithEmailAndPassword } from ‘firebase/auth’;

import { getFirestore } from ‘firebase/firestore’;

import { getStorage, ref } from ‘firebase/storage’;

const firebaseConfig = {

  apiKey: import.meta.env.VITE_FIREBASE_API_KEY,

  authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,

  projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,

  storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,

  messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,

  appId: import.meta.env.VITE_FIREBASE_APP_ID,

};

const firebases = initializeApp(firebaseConfig);

const auth = getAuth(firebases);

const firestore = getFirestore(firebases);

const storage = getStorage(firebases);

export { auth, firestore, signInWithEmailAndPassword, firebases, storage };

export default firebases;

Employee.js

// api.js

import { collection, query, where, getDocs } from ‘firebase/firestore’;

import { firestore } from ‘./../firebase’;

const fetchUserDataByEmail = async (email) => {

    try {

      const q = query(collection(firestore, ’employee’), where(’email’, ‘==’, email));

      const querySnapshot = await getDocs(q);

 

      if (querySnapshot.docs.length === 0) {

        console.log(‘User not found’);

        return null;

      }

 

      // Assuming there’s only one document with the specified email

      const userDoc = querySnapshot.docs[0];

      return userDoc.data();

    } catch (error) {

      console.error(‘Error fetching user data:’, error);

      throw error; // Re-throw the error for handling in components

    }

  };

 

  export { fetchUserDataByEmail };

Leave a comment

Your email address will not be published. Required fields are marked *