๐Ÿ”ฅ Firebase ์†Œ์…œ ๋กœ๊ทธ์ธ ์—ฐ๋™

Yeonnยท2024๋…„ 11์›” 8์ผ
0

Backend ๐Ÿ‘€

๋ชฉ๋ก ๋ณด๊ธฐ
8/10
post-thumbnail
  • src > config > firebaseConfig.ts
    • const app = initializeApp(firebaseConfig);

      • initializeApp(): firebase ์•ฑ ์ดˆ๊ธฐํ™”
      • firebase์˜ ์„ค์ • ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” firebaseConfig ๊ฐ์ฒด๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์•„, Firebase ์„œ๋น„์Šค์— ์—ฐ๊ฒฐ
    • export const auth = getAuth(app);

      • firebase Authentication ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ( auth ๊ฐ์ฒด ์ƒ์„ฑ )
    • export const db = getFirestore(app);
      - getFirestore(app): firestore ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
      - firestore
      - firebase์—์„œ ์ œ๊ณตํ•˜๋Š” ํด๋ผ์šฐ๋“œ ๊ธฐ๋ฐ˜ NoSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
      - ๋ฐ์ดํ„ฐ์˜ ์ €์žฅ ๋ฐ ์กฐํšŒ๋ฅผ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
      - db ๊ฐ์ฒด๋ฅผ ๋‚ด๋ณด๋‚ด์–ด, ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ Firestore ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ

      import { initializeApp } from "firebase/app";
      import { getAuth } from "firebase/auth"; 
      import { getFirestore } from "firebase/firestore";
      
      const firebaseConfig = {
        apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
        authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
        projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
        storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
        messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
        appId: process.env.REACT_APP_FIREBASE_APP_ID,
      };
      
      const app = initializeApp(firebaseConfig);  // firebase ์•ฑ ์ดˆ๊ธฐํ™”
      
      export { app };
      export const auth = getAuth(app);           // auth ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
      export const db = getFirestore(app);        // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
  • src > service > firebaseAuthService.ts
    import { GoogleAuthProvider, signInWithPopup, signOut } from "firebase/auth";
    import { auth } from "../config/firebaseConfig";
    
    export const firebaseAuthService = {
    	// google social login
      async googleLogin() {
        try {
          const googleAuthProvider = new GoogleAuthProvider();
          const result = await signInWithPopup(auth, googleAuthProvider);
          return result.user;
        } catch (error) {
          console.error("โŒfirebaseAuthService: google login error");
          console.error(error);
        }
    
        return null;
      },
    
    	// google social logout
      async googleLogout() {
        await signOut(auth);
      },
    };
    
    • firebase login ํ•˜๊ธฐ
      • signInWithPopup(): ํŒ์—… ์ฐฝ์œผ๋กœ ๋กœ๊ทธ์ธ โ†’ ์ธ์ฆ ๊ณผ์ •์„ ๋งˆ์น˜๋ฉด ๋‹ค์‹œ ์•ฑ์œผ๋กœ ๋Œ์•„์˜ด !
      • signInWithRedirect(): ํŽ˜์ด์ง€ ๋ฆฌ๋‹ค์ด๋ ‰์…˜์„ ํ†ตํ•ด ์†Œ์…œ ๋กœ๊ทธ์ธ ์ง„ํ–‰ โ†’ ํŒ์—… ์ฐจ๋‹จ ๋ฌธ์ œ ํ•ด๊ฒฐ
      • signInWithEmailAndPassword(): ์ด๋ฉ”์ผ๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์‚ฌ์šฉํ•œ ์ธ์ฆ
      • signInAnonymously(): ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๊ณ  ์ต๋ช…์œผ๋กœ ์ธ์ฆ, ๊ณ„์ • ์—†์ด ์ž„์‹œ๋กœ ์ ‘๊ทผ
        • ๊ฒŒ์ŠคํŠธ ๋กœ๊ทธ์ธ
      • signInWithCredential(): ์ž๊ฒฉ ์ฆ๋ช… ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ ์ง์ ‘ ๋กœ๊ทธ์ธ

0๊ฐœ์˜ ๋Œ“๊ธ€