๊ฐœ๋ฐœ ๊ณต๋ถ€/๋ฐ๋ธŒ์ฝ”์Šค TIL

[ํด๋ผ์šฐ๋”ฉ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์—”์ง€๋‹ˆ์–ด๋ง TIL] 240420, 240421 - React Native ์บ ํ•‘์•ฑ ๋งŒ๋“ค๊ธฐ ํ”„๋กœ์ ํŠธ ๋งˆ๋ฌด๋ฆฌ

๊ฐ€์šค์ด 2024. 4. 22. 01:31

Intro


ํ”„๋กœ์ ํŠธ๊ฐ€ ์›”์š”์ผ๊นŒ์ง€๋ผ ์ฃผ๋ง์—๋„ ์—ด์‹ฌํžˆ ์ž‘์—…์„ ํ–ˆ๋‹ค.

firebase authentication์œผ๋กœ ๋กœ๊ทธ์ธ ์—ฐ๋™์„ ํ•˜๊ณ  firebase firestore๋„ ์—ฐ๊ฒฐํ•ด DB ์„ค์ •ํ•˜๋ ค ํ–ˆ์œผ๋‚˜ firestore๋Š” ์—ฐ๋™์— ์‹คํŒจํ–ˆ๋‹ค ใ… 

 

 

 

 

ํ”„๋กœ์ ํŠธ ์ง„ํ–‰์ƒํ™ฉ


React Native Firebase

https://rnfirebase.io/

 

React Native Firebase | React Native Firebase

Welcome to React Native Firebase! To get started, you must first setup a Firebase project and install the "app" module. React Native Firebase is the officially recommended collection of packages that brings React Native support for all Firebase services on

rnfirebase.io

 

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

 

์šฐ์„  ํ”„๋กœ์ ํŠธ์— firebase๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ๊ธฐ๋ณธ์ ์œผ๋กœ @react-native-firebase/app์„ ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ ์™ธ์— Authentication์ด๋‚˜ firestore์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค๋ฅธ ํŒจํ‚ค์ง€๋“ค์„ ์ถ”๊ฐ€๋กœ ์„ค์น˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

npm install --save @react-native-firebase/app

 

๊ทธ ํ›„, firebase์—์„œ ๋ฐ›์€ GoogleService-Info.plist ํŒŒ์ผ์„ /ios/{projectName}.xcodeproj ์— ์ €์žฅํ•œ๋‹ค.

pod install์„ ํ•˜๋ฉด ์—„์ฒญ๋‚œ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ ์œ„ ๋งํฌ์—์„œ ํ•˜๋‚˜์”ฉ ํ•ด๊ฒฐํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

 

 

Firebase Authentication

Firebase Authentication ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋กœ๊ทธ์ธ/ํšŒ์›๊ฐ€์ž…์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

SNS ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ๋„ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

 

  • firebase-auth ์„ค์น˜
npm install @react-native-firebase/auth
cd ios/ && pod install

 

 

firebase-auth์—์„œ onAuthStateChanged ๋ชจ๋“ˆ์„ ํ†ตํ•ด ํ˜„์žฌ ์ธ์ฆ ์ƒํƒœ๋ฅผ ๊ตฌ๋…ํ•˜๊ณ  ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

์•ฑ์„ ๋‚˜๊ฐ”๋‹ค ๋“ค์–ด์™€๋„ ์ธ์ฆ ์ƒํƒœ๊ฐ€ ์•ฑ ์บ์‹œ์— ์ €์žฅ๋œ๋‹ค.

import React, { useState, useEffect } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { MainStack, LoginStack } from './src/router';
import auth from '@react-native-firebase/auth';

function App(): React.JSX.Element {
  const [initializing, setInitializing] = useState(true);
  const [user, setUser] = useState();

  function onAuthStateChanged(user) {
    setUser(user);
    if (initializing) setInitializing(false);
  }

  useEffect(() => {
    const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
    return subscriber;
  }, []);

  if (initializing) return null;

  return (
    <NavigationContainer>{user ? <MainStack /> : <LoginStack />}</NavigationContainer>
  );
}

export default App;

 

 

๋กœ๊ทธ์ธ/ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด ๊ตฌํ˜„

 

์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ, Home ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋ฉฐ ๋กœ๊ทธ์ธ๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์œผ๋กœ ์ด๋™ํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

 

๋กœ๊ทธ์ธ/ํšŒ์›๊ฐ€์ž…์˜ ๊ฒฝ์šฐ, ๊ฐ„๋‹จํ•œ validation์„ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค.

๋ชจ๋“  ํ•„๋“œ์— ๊ฐ’์ด ์ž…๋ ฅ์ด ๋˜์—ˆ๋Š”์ง€ ์ฒดํฌ -> ์ด๋ฉ”์ผ ํ˜•์‹์ด ๋งž๋Š”์ง€ -> ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž๋ฆฌ์ˆ˜ ์ฒดํฌ -> ์•ฝ๊ด€ ๋™์˜ ์ฒดํฌ๊ฐ€ ๋˜์–ด์žˆ์„ ๊ฒฝ์šฐ ๋งŒ ํšŒ์›๊ฐ€์ž… ๊ฐ€๋Šฅ ๋“ฑ...

 

์‹œ๊ฐ„์ƒ Google, Facebook ๋กœ๊ทธ์ธ์€ ๊ตฌํ˜„ํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.

 

 

Firebase Firestore

Firebase Authentication์€ ์ •์ƒ์ ์œผ๋กœ ์„ค์น˜ํ•˜๊ณ  ๋™์ž‘ํ•˜์˜€๋Š”๋ฐ Firestore๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ณผ์ •์—์„œ ๋งŽ์€ ์˜ค๋ฅ˜๋ฅผ ๋งŒ๋‚ฌ๊ณ  ๊ฒฐ๊ตญ ๊ธฐ๊ฐ„ ๋‚ด์— ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.

 

โš ๏ธ [!] The following Swift pods cannot yet be integrated as static libraries:

The Swift pod FirebaseCoreInternal
depends upon GoogleUtilities
, which does not define modules. To opt into those targets generating module maps (which is necessary to import them from Swift when building as static libraries), you may set use_modular_headers!
globally in your Podfile, or specify :modular_headers => true
for particular dependencies.

 

  • Podfile
pod 'GoogleUtilities', :modular_headers => true

 

:modular_headers => true๋กœ ์„ค์ •ํ•˜๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์˜ค๊ณ  ์ด๋ฅผ Podfile์— ์ ์œผ๋ฉด

 

Module ‘FirebaseCore’ not found

 

์ด๋Ÿฐ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์™”๋‹ค.

pod 'FirebaseCore', :modular_headers => true

 

๊ณ„์† Podfile์„ ์ˆ˜์ •ํ•˜๊ณ  Pod ์ดˆ๊ธฐํ™”/์„ค์น˜๋ฅผ ๋ฐ˜๋ณตํ•˜๋А๋ผ ๋งŽ์€ ์‹œ๊ฐ„์ด ์†Œ์š”๋๋‹ค ใ… ใ… 

 

โš ๏ธ [!] The following Swift pods cannot yet be integrated as static libraries:

The Swift pod FirebaseFirestore depends upon FirebaseCoreExtension and FirebaseFirestoreInternal, which do not define modules. To opt into those targets generating module maps (which is necessary to import them from Swift when building as static libraries), you may set use_modular_headers! globally in your Podfile, or specify :modular_headers => true for particular dependencies.

 

use_modular_headers! # ๋ชจ๋“  Pods์— ๋Œ€ํ•ด ๋ชจ๋“ˆ ํ—ค๋” ์‚ฌ์šฉ ์„ค์ •

pod 'FirebaseFirestore', :modular_headers => true

 

๊ณ„์† ๋ฐ˜๋ณต...ํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.

 

๋ณธ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•  ๋•Œ Flipper๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ๊ณ  Firebase๋Š” Flipper์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋“ค๋„ ์›์ธ์ด ์žˆ์—ˆ์„ ์ง€๋„ ๋ชจ๋ฅธ๋‹ค.

 

 

 

 

 


+ ์ถ”ํ›„์— ํŒ€ ํ”„๋กœ์ ํŠธ์—์„œ ๋‹ค์‹œ Firestore๋ฅผ ์—ฐ๋™ํ•˜๋Š” ์ž‘์—…์„ ๋„์ „ํ•ด ๋ณด์•˜๋Š”๋ฐ ๊ทธ๋•Œ ๋“œ๋””์–ด ์„ฑ๊ณตํ•˜์˜€๋‹ค!

์ด๋Ÿฌํ•œ ์‹คํŒจ๋“ค์ด ๊ฒช๊ณ  ์ˆ˜์›”ํ•˜๊ฒŒ ์„ฑ๊ณตํ•  ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํŒ€ ํ”„๋กœ์ ํŠธ์—์„œ React-native๊ฐ€ ๋ฒ„์ „์ด ์˜ฌ๋ผ๊ฐ€๋ฉด์„œ ๋‚ด ๋งฅ๋ถ๋„ ์†Œํ”„ํŠธ์›จ์–ด ์—…๋ฐ์ดํŠธ๊ฐ€ ํ•„์š”ํ–ˆ๋Š”๋ฐ ๋งฅ๋ถ ์†Œํ”„ํŠธ์›จ์–ด ๋ฒ„์ „ ๋ฌธ์ œ์˜€์„์ˆ˜๋„..?

 

 

๋งˆ๋ฌด๋ฆฌ


์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋Š” firestore๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž๋Š” ์š•์‹ฌ์— firebase๋ฅผ ์—ฐ๋™ํ•˜๋Š” ๊ณผ์ •์—์„œ ๋„ˆ๋ฌด ๋งŽ์€ ์‹œ๊ฐ„์ด ์†Œ๋ชจ๋˜์–ด ๊ตฌํ˜„ํ•ด์•ผ ํ•  ์‚ฌํ•ญ๋“ค์„ ๋ชจ๋‘ ๊ตฌํ˜„ํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.  ๊ทธ๋ž˜๋„ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ณผ์ •์—์„œ ๋งŽ์ด ๋ฐฐ์› ๋˜ ์‹œ๊ฐ„์ด์—ˆ๋‹ค. ๊ณต์‹ ๋ฌธ์„œ์™€ ์˜ค๋ฅ˜ ๋‚ด์šฉ์„ ๊ผผ๊ผผํžˆ ์ฝ์–ด์•ผ๊ฒ ๋‹ค๊ณ  ๋А๊ผˆ๊ณ  ์ธ๋‚ด์‹ฌ๋„ ๊ธฐ๋ฅผ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ํ‘..