Firebase ์‹œ์ž‘ํ•˜๊ธฐ๐Ÿ”ฅ

ํˆฌ๋ฐ•์†ยท2021๋…„ 2์›” 23์ผ
0

Firebase

๋ชฉ๋ก ๋ณด๊ธฐ
1/1
post-thumbnail

Firebase ์‹œ์ž‘ํ•˜๊ธฐ
์„ค์น˜

npm install --save firebase

firebase๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์–‘ํ•˜๋‹ค. ๋‚˜๋Š” Node.js ๊ธฐ๋ฐ˜์œผ๋กœ ์‹œ์ž‘ํ–ˆ๊ธฐ์— ์œ„์™€ ๊ฐ™์ด ํ”„๋กœ์ ํŠธ์— firebase๋ฅผ ์„ค์น˜ํ–ˆ๋‹ค.
์…‹ํŒ…

import * as firebase from "firebase/app";
import "firebase/auth";
import "firebase/firestore";
var firebaseConfig = {
  //... configuration
};
firebase.initializeApp(firebaseConfig); // Initialize

firebase๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์œ„์™€ ๊ฐ™์ด ํ•„์š”ํ•œ ๊ธฐ๋Šฅ(๋‚˜์˜ ๊ฒฝ์šฐ auth์™€ firestore)์„ importํ•˜๊ณ , firebase ์•ฑ์„ ๋งŒ๋“ค๋ฉด ์ œ๊ณต๋˜๋Š” configuration์„ ์ž…๋ ฅํ•œ ํ›„ ์ดˆ๊ธฐํ™” ํ•ด์ค€๋‹ค.

export const firebaseInstance = firebase;
export const authService = firebase.auth();
export const dbService = firebase.firestore();

๋˜ํ•œ ๋‚˜๋Š” firebase ๊ด€๋ จ ์ฝ”๋“œ๋ฅผ ๋ณ„๋„์˜ ํŒŒ์ผ์— ์ €์žฅํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—์„œ firebase๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก exportํ•œ๋‹ค.
์ธ์ฆ ๊ธฐ๋Šฅ

import { authService, firebaseInstance } from '../fbase.js';

์…‹ํŒ…์—์„œ exportํ•œ firebaseInstance์™€ authService๋ฅผ import ํ•ด์ค€๋‹ค.

const onLoginClick = async (event) => {
    const {
        target: { name },
    } = event;
    const provider = new firebaseInstance.auth.GoogleAuthProvider();
    await authService.signInWithRedirect(provider);
};
const onLogOutClick = async () => {
    await authService.signOut();
};

๋‚˜๋Š” ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ๋งŒ ์‚ฌ์šฉํ–ˆ๋‹ค. ์ ์ ˆํ•œ ์ปดํฌ๋„ŒํŠธ(๋กœ๊ทธ์ธ ๋ฒ„ํŠผ)์— onClick={onLoginClick} ์†์„ฑ์„ ์ ์šฉํ•˜๋ฉด ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋! (๋กœ๊ทธ์•„์›ƒ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€)
์ด ๋•Œ, async/await์„ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ์€ ๋น„๋™๊ธฐ๋กœ ์ง„ํ–‰๋˜๋„๋ก ์ฒ˜๋ฆฌํ–ˆ๋‹ค.

DB (firestore)
import { authService, dbService } from '../fbase.js';

authService์™€ dbService๋ฅผ import ํ•œ๋‹ค.
๋ฐ์ดํ„ฐ ์ž…๋ ฅ

onSubmit = async (event) => {
    event.preventDefault();
    await dbService.collection(authService.currentUser.uid).add({
        info: this.state,
        createdTime: Date.now(),
        createdName: authService.currentUser.displayName,
    });
    

์ ์ ˆํ•œ ์ปดํฌ๋„ŒํŠธ์— onSubmit={onSubmit} ์†์„ฑ์„ ์ ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
์ฐธ๊ณ ๋กœ firestore๋Š” noSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ, collection์•ˆ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ doc๋“ค์ด ์œ„์น˜ํ•˜๊ณ , ๊ฐ๊ฐ์˜ doc๋“ค์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ key:value ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. (collection ํด๋” ์•ˆ์— doc๋“ค์ด ์žˆ๋Š” ๊ฒƒ๊ณผ ๋น„์Šท)
๋‚˜์˜ ๊ฒฝ์šฐ collection์˜ ์ด๋ฆ„์„ ๋กœ๊ทธ์ธํ•œ ์œ ์ €์˜ uid ์†์„ฑ์œผ๋กœ ์„ค์ •ํ–ˆ๋‹ค. ์ฐธ๊ณ ๋กœ doc์˜ ์ด๋ฆ„์€ firestore๊ฐ€ ๋žœ๋ค์œผ๋กœ ์ƒ์„ฑํ•ด์ค€๋‹ค.
๋‚ด๊ฐ€ ์ž…๋ ฅํ•œ ์ •๋ณด๋Š” ์ •๋ณด์˜ ์ž…๋ ฅ ์‹œ์ (createdTime: Date.now()), ์ž‘์„ฑ์ž์˜ ์ด๋ฆ„(createdName: authService.currentUser.displayName : ๋‚˜์˜ ๊ฒฝ์šฐ ๊ตฌ๊ธ€ ๊ณ„์ •์— ์„ค์ •๋œ ์ด๋ฆ„)๊ณผ info(this.state : ๊ฐ์ฒด๋กœ ์“ฐ์—ฌ์ง„ ์—ฌ๋Ÿฌ ์ •๋ณด๋“ค)๋‹ค.
๋ฐ์ดํ„ฐ ์ถœ๋ ฅ


useEffect(() => {
    dbService.collection(authService.currentUser.uid).orderBy("createdTime", "desc").onSnapshot((snapshot) => {
        const problemArray = snapshot.docs.map((doc) => ({
            id: doc.id,
            ...doc.data(),
        }));
        setProblems(problemArray);
    });
}, []);

React Hooks๋ฅผ ์ด์šฉํ•˜์˜€๋‹ค.
์ถœ๋ ฅํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ์˜ collection ์ด๋ฆ„(๋‚˜์˜ ๊ฒฝ์šฐ authService.currentUser.uid์„ collection ๋ฉ”์†Œ๋“œ์˜ ์ธ์ž๋กœ ์ค€๋‹ค. orderBy("createdTime", "desc")๋กœ ํ•ด๋‹น collection์˜ docs๋“ค์„ createdTime์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. ์ดํ›„ onSnapshot์„ ์ด์šฉํ•ด problemArray์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์•„ problem hook์— ์ €์žฅํ•œ๋‹ค.
์ด ๋•Œ, doc.id๋Š” ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์‹œ ์ž„์˜๋กœ ์ƒ์„ฑ๋œ doc์˜ ์ด๋ฆ„์ด๋‹ค. ์ฐธ๊ณ ๋กœ onSnapshot์„ ์ด์šฉํ•˜๋ฉด firestore์— ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ์—…๋ฐ์ดํŠธ๋œ๋‹ค.
๋ฐ์ดํ„ฐ ์‚ญ์ œ

const onDeleteClick = async (problem) => {
    const ok = window.confirm('์ •๋ง ์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ??');
    if (ok) {
        await dbService.doc(`${authService.currentUser.uid}/${problem.id}`).delete();
    }
};

์ ์ ˆํ•œ ์ปดํฌ๋„ŒํŠธ์— onClick={onDeleteClick} ์†์„ฑ์„ ์ฃผ๋ฉด ์›ํ•˜๋Š” docs๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด๋•Œ doc์˜ ์ธ์ž๋Š” (collectionName/docId)์•ผ ํ•œ๋‹ค. ๋‚˜์˜ ๊ฒฝ์šฐ collection์˜ ์ด๋ฆ„์€ authService.currentUser.uid์ด๊ณ , doc์˜ ์ด๋ฆ„์€ problem.id๋กœ ๋ฐ›์•„์™”๋‹ค.

async resetData() {
    if (window.confirm('์ •๋ง ์ดˆ๊ธฐํ™” ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?\n์ž…๋ ฅํ•œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค.')) {
        await dbService
            .collection(authService.currentUser.uid)
            .get()
            .then((res) => {
                res.forEach((element) => {
                    element.ref.delete();
                });
            });
    }
}

ํ•œ collection์˜ ๋ชจ๋“  docs๋ฅผ ํ•œ๋ฒˆ์— ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋Šฅ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋˜์ง€ ์•Š๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์œ„์™€ ๊ฐ™์ด resetData ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

https://velog.io/@dojunggeun/Firebase-%EC%9D%B8%EC%A6%9D-Firestore

profile
์ƒˆ์‹น ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค.๐ŸŒฑ

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