WEB/Front-End๐ŸŒ

[React+Firebase] Set up, Auth

narang111 2022. 1. 4. 02:48

์›ํ•˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์—

> npx create-react-app ๋‚ดํ”Œ์ ์ด๋ฆ„

> code ๋‚ดํ”Œ์ ์ด๋ฆ„ 

์ด๋ ‡๊ฒŒ ๋ช…๋ น์–ด ์ž…๋ ฅํ•ด์„œ VScode ํŽธ์ง‘๊ธฐ๋กœ ๋‚ด ํ”Œ์  ์—ด๊ธฐ

 

VScode ํ„ฐ๋ฏธ๋„์—์„œ firebase๋ฅผ install ํ•ด์ค€๋‹ค.

npm i firebase@9.6.1

 

src ํด๋”์— firebase.js ํŒŒ์ผ ๋งŒ๋“ค์–ด์ฃผ๊ณ 

๋‚ด๊ฐ€ firebase ์‚ฌ์ดํŠธ์—์„œ ๋งŒ๋“  ์•ฑ ์ •๋ณด ๋„ฃ์–ด์ฃผ๊ธฐ

import firebase from 'firebase/app';

const firebaseConfig = {
 ...
};

export default firebase.initializeApp(firebaseConfig);

 

index.js์—์„œ importํ•ด์ฃผ๊ณ  console.log ์ฐ์–ด๋ณด๊ธฐ (์‹คํ–‰์€ VScode ํ„ฐ๋ฏธ๋„์—์„œ npm run start)

 

object๊ฐ€ ์ž˜ ๋‚˜์˜จ๋‹ค.

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
// import ๋ถ€๋ถ„
import firebase from 'firebase/compat/app';
console.log(firebase);

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

reportWebVitals();

 

 

firebase.js ์— key์™€ url์„ ์ ์–ด๋‘๋Š” ๊ฒƒ ๋Œ€์‹ ์— .env ํŒŒ์ผ์„ ์ƒ์„ฑํ–ˆ๋‹ค. (.env ํŒŒ์ผ์€ srcํด๋” '์•ˆ'๋ง๊ณ  ํด๋”์™€ ๊ฐ™์€ ๊นŠ์ด์— ์žˆ์–ด์•ผํ•œ๋‹ค)

.envํŒŒ์ผ์—๋Š” ํ™˜๊ฒฝ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€๋Š”๋ฐ React.js ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฒฝ์šฐ์— ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์จ์•ผ ํ•œ๋‹ค๋ฉด

REACT_APP์œผ๋กœ ์‹œ์ž‘ํ•ด์•ผํ•˜๊ณ  ๊ทธ ๋’ค๋กœ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ด๋ฆ„์„ ๋ถ™์—ฌ์ค˜์•ผํ•œ๋‹ค.

์›์น™์ด๋‹ค!!!

์ด ๋ถ€๋ถ„์€ Github๋งŒ์„ ์œ„ํ•œ ์ฝ”๋“œ์ด๋‹ค. ์„œ๋น„์Šค๋ฅผ ์œ„ํ•ด ๋นŒ๋“œํ•˜๊ณ  ์›น ์‚ฌ์ดํŠธ๋ฅผ ๋„์šฐ๋ฉด

create-react-app์€ ์ด ์ฝ”๋“œ๋ฅผ ๋ชจ๋‘ ์‹ค์ œ ๊ฐ’๋“ค๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

 

 

React Router Setup

src ํด๋”์— components, routes ํด๋” ๋‘ ๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค. 

routes์—๋Š” Auth, EditProfile, Home, Profile 4๊ฐœ์˜ ํŽ˜์ด์ง€๊ฐ€ ๋  ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  components์— Router.js ํŒŒ์ผ์„ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.

๐Ÿ”ฝ Router.js

๋กœ๊ทธ์ธ ์ƒํƒœ๋ฉด Home ์œผ๋กœ ์•„๋‹ˆ๋ผ๋ฉด Auth ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

import React, { useState } from 'react';
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
import Auth from '../routes/Auth';
import Home from '../routes/Home';

const AppRouter = ({ isLoggedIn }) => {
  return (
    <Router>
      <Switch>
        {isLoggedIn ? (
          <>
            <Route exact path="/">
              <Home />
            </Route>
          </>
        ) : (
          <>
            <Route exact path="/">
              <Auth />
            </Route>
          </>
        )}
      </Switch>
    </Router>
  );
};

export default AppRouter;

state ๋Š” App์œผ๋กœ ๋„ฃ์–ด์ฃผ๊ณ  props ๋กœ ์ „๋‹ฌํ•ด์คฌ๋‹ค.

import React, { useState } from 'react';
import AppRouter from './Router';

function App() {
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  // props ์ „๋‹ฌ
  return <AppRouter isLoggedIn={isLoggedIn} />;
}

export default App;

 

 

 

App ์•ˆ์— AppRouter๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” footer๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ..!

 

https://firebase.google.com/docs/reference/js/auth

 

์ธ์ฆ ํŒจํ‚ค์ง€  |  Firebase

Test SDK for Cloud Functions for Firebase

firebase.google.com

 

auth๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ œ์ผ ๋จผ์ € ํ•  ๊ฒƒ์€ import ํ•˜๊ธฐ์ด๋‹ค.

https://create-react-app.dev/docs/importing-a-component

 

Importing a Component | Create React App

This project setup supports ES6 modules thanks to webpack.

create-react-app.dev

 

์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ import ํ•ด์•ผํ–ˆ๋˜ ๊ฒƒ์„ ๊ทธ๋ƒฅ import ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

(์ปดํ“จํ„ฐ๊ฐ€ ๋ชจ๋“  ๊ฒƒ๋“ค์ด src๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋œ๋‹ค.)

- ๋ฒ„์ „ ๋ฌธ์ œ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ์”ฉ ๋ฐ”๋€Œ์—ˆ๋‹ค.

// ์—ฌ๊ธฐ์—์„œ
import fbase from "../fbase";
// ์ด๋ ‡๊ฒŒ
import { auth } from 'fbase';

 

firbase.js ์ด๋ฆ„์„ fbase.js๋กœ ๋ฐ”๊พธ๊ณ  auth๋ฅผ import ํ•ด์คฌ๋‹ค

๐Ÿ”ฝ fbase.js

import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';

const firebaseConfig = {
  ...
};

const firebaseApp = initializeApp(firebaseConfig);
export const authService = getAuth(firebaseApp);

 

์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ์•ฑ์—์„œ Authentication ํƒญ์— ๋“ค์–ด๊ฐ€์„œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค.

 

์ด๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ, Google, .Github๋ฅผ ์ผœ์คฌ๋Š”๋ฐ

Github๋ฅผ ํ‚ค๋ ค๋ฉด Github ์—์„œ App์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผํ•œ๋‹ค.

https://github.com/settings/apps

 

GitHub: Where the world builds software

GitHub is where over 73 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and feat...

github.com

 

 

 

Register ํ•ด์ค€ ํ›„ ๋ณด์ด๋Š” ID์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

!์˜ค๋ฅ˜ - ์ด๋Ÿฐ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋Š”๋ฐ Network ํƒญ์—์„œ No throttling์œผ๋กœ ์„ค์ •ํ•˜๋‹ˆ๊นŒ ๋‹ค์‹œ ์ž˜ ๋๋‹ค.

VM156:6770 crbug/1173575, non-JS module files deprecated.
(anonymous) @ VM156:6770