๐ŸŒŸ Firebase Auth ์‚ฌ์šฉํ•˜๊ธฐ

summereuna๐Ÿฅยท2022๋…„ 3์›” 8์ผ

๐ŸŒŸ Twinkle (React, Firebase)

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

์ƒ๋Œ€๊ฒฝ๋กœ > ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ์ˆ˜์ •


  1. /src/components/App.js์—์„œ firebase importํ•˜๊ธฐ
    1-1. ์ƒ๋Œ€๊ฒฝ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ
import AppRouter from "./Router";
import { useState } from "react";
//๐Ÿ”ฅ์ƒ๋Œ€ ๊ฒฝ๋กœ(relative import, = ํŒŒ์ผ ๊ฒฝ๋กœ)๋กœ ์ž„ํฌํŠธ
import firebase from "../firebase";

function App() {
  //useState Hook
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  //Router ๋ Œ๋”ํ•˜๊ธฐ
  //AppRouter์— prop ์ „๋‹ฌํ•˜๊ธฐ
  return (
    <>
      <AppRouter isLoggedIn={isLoggedIn} />
      <footer>&copy; twinkle {new Date().getFullYear()}</footer>
    </>
  );
}

export default App;

1-2. ์ ˆ๋Œ€๊ฒฝ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ(absolute import)๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž

์ ˆ๋Œ€๊ฒฝ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ(absolute import)
์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“ˆ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ์ง€์›ํ•˜๋„๋ก ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด ์ž‘์—…์€ ํ”„๋กœ์ ํŠธ์˜ ๋ฃจํŠธ์— jsconfig.json ๋˜๋Š” tsconfig.json ํŒŒ์ผ์„ ๊ตฌ์„ฑํ•˜์—ฌ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
ํ”„๋กœ์ ํŠธ์—์„œ TypeScript๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ tsconfig.json ํŒŒ์ผ์ด ์ด๋ฏธ ์žˆ์„ ๊ฒƒ์ด๋‹ค.
์•„๋ž˜๋Š” JavaScript ํ”„๋กœ์ ํŠธ์˜ jsconfig.json ํŒŒ์ผ์˜ ์˜ˆ์ด๋‹ค.
ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

TypeScript๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ํ”„๋กœ์ ํŠธ์˜ tsconfig.json ํŒŒ์ผ์˜ compilerOptions ๋‚ด์—์„œ baseUrl ์„ค์ •์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
์ ˆ๋Œ€๊ฒฝ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ์ง€์›ํ•˜๋„๋ก ํ”„๋กœ์ ํŠธ๋ฅผ ๊ตฌ์„ฑํ–ˆ์œผ๋ฏ€๋กœ src/components/Button.js์— ์œ„์น˜ํ•œ ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

import Button from 'components/Button';

1-2-1. ๋”ฐ๋ผ์„œ ์ ˆ๋Œ€๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํŠธ์œ™ํด ํด๋”์— jsconfig.json ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

1-2-2. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ firebase๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.

import firebase from "firebase";

๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ๊ฒฝ๋กœ๋„ ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ๋ฐ”๊ฟ”์ฃผ์ž.

//์ ˆ๋Œ€ ๊ฒฝ๋กœ(absolute import)
import AppRouter from "components/Router";
import { useState } from "react";
//์ ˆ๋Œ€ ๊ฒฝ๋กœ(absolute import)
import firebase from "firebase";

function App() {
  //useState Hook
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  //Router ๋ Œ๋”ํ•˜๊ธฐ
  //AppRouter์— prop ์ „๋‹ฌํ•˜๊ธฐ
  return (
    <>
      <AppRouter isLoggedIn={isLoggedIn} />
      <footer>&copy; twinkle {new Date().getFullYear()}</footer>
    </>
  );
}

export default App;

๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์„œ ์ž ๊น, firebase.js ํŒŒ์ผ ์ด๋ฆ„์„ fbase.js๋กœ ๋ฐ”๊พธ๊ณ  ๊ฐ€์ž.


firebase์—์„œ ์ž„ํฌํŠธ ํ•ด์˜ค๊ณ  ๊ทธ๋Ÿฌ๋Š”๋ฐ ์ด๋ฆ„์ด ๊ฐ™์•„์„œ ํ—ท๊ฐˆ๋ฆฐ๋‹ค..^^...
๋‹คํ–‰ํžˆ ๋˜‘๋˜‘ํ•œ VSC๋Š” ํŒŒ์ผ ์ด๋ฆ„ ๋ฐ”๊พธ๋ฉด ์ž๋™์œผ๋กœ import ๊ฒฝ๋กœ ์—…๋Žƒํ•ด์ค€๋‹ค.
App.js, index.js์— import firebase from "fbase";๋กœ!!

Auth


auth๋Š” ์‹ ๊ทœ ์‚ฌ์šฉ์ž ๊ฐ€์ž… ๋ฐ ๊ธฐ์กด ์‚ฌ์šฉ์ž ๋กœ๊ทธ์ธ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

๊ธฐ๋ณธ ์…‹์—…

  1. auth ๊ด€๋ จ doc๋ฅผ ํ™•์ธํ•˜์ž.

๐Ÿ”ฅ (Firebase v.9๋กœ ์ง„ํ–‰)
์•„๋ž˜์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜๋ฉด Firebase ์„œ๋น„์Šค(Cloud Firestore, ์ธ์ฆ, ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ์›๊ฒฉ ๊ตฌ์„ฑ ๋“ฑ)๋ฅผ ๊ฐœ๋ณ„ ํ•˜์œ„ ํŒจํ‚ค์ง€ ๋‚ด์—์„œ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
import { } from 'firebase/<service>';

  • ์›น์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ Firebase ์„œ๋น„์Šค ์ค‘ ์›น์šฉ ์ธ์ฆ
    import { } from 'firebase/auth';
  • ์›น์šฉ Cloud Firestore
    import { } from 'firebase/firestore';
  • ์›น์šฉ Cloud Storage
    import { } from 'firebase/storage';
  1. /src/firebase.js ์ž‘์„ฑ
import { initializeApp } from "firebase/app";
//๐Ÿ”ฅ
import { getAuth } from "firebase/auth";
// Follow this pattern to import other Firebase services
// import { } from 'firebase/<service>';

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: process.env.REACT_APP_API_KEY,
  authDomain: process.env.REACT_APP_AUTH_DOMAIN,
  projectId: process.env.REACT_APP_PROJECT_ID,
  storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
  appId: process.env.REACT_APP_APP_ID,
};

// Initialize Firebase(Firebase ํ”„๋กœ์ ํŠธ ์•ฑ)
const app = initializeApp(firebaseConfig);
//๐Ÿ”ฅ ๊ตณ์ด firebase ์ „์ฒด ๋‹ค ์•ˆ๋‚ด๋ณด๋‚ด๊ณ  authService๋งŒ exportํ•  ์ˆ˜ ์žˆ๋‹ค.
//๐Ÿ”ฅ authService ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ๋งˆ๋‹ค getAuth(app) ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ app.js์—์„œ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœํ•˜๊ณ  export ์‹œํ‚ค๋ฉด ๋œ๋‹ค.
export const authService = getAuth(app);
  1. /src/components/App.js ์ž‘์„ฑ
//์ ˆ๋Œ€ ๊ฒฝ๋กœ(absolute import)
import AppRouter from "components/Router";
import { useState } from "react";
//์ ˆ๋Œ€ ๊ฒฝ๋กœ(absolute import)
//๐Ÿ”ฅ fbase์—์„œ authService ๊ฐ€์ ธ์˜ค๊ธฐ(export๋กœ ๋‚ด๋ณด๋ƒˆ๊ธฐ ๋•Œ๋ฌธ์— {} ์ค‘๊ด„ํ˜ธ ์ณ์„œ ๊ฐ€์ ธ์™€์•ผ ํ•จ)
import { authService } from "fbase";

function App() {
  //useState Hook
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  console.log(authService);
  //Router ๋ Œ๋”ํ•˜๊ธฐ
  //AppRouter์— prop ์ „๋‹ฌํ•˜๊ธฐ
  return (
    <>
      <AppRouter isLoggedIn={isLoggedIn} />
      <footer>&copy; twinkle {new Date().getFullYear()}</footer>
    </>
  );
}

export default App;

App.js์—์„œ authService ์‚ฌ์šฉํ•ด๋ณด์ž.

Firebase > Docs > Reference > JavaScript - version 9 > auth > Auth

  1. ์ฝ˜์†”์— authService.currentUser๋ฅผ ๋กœ๊ทธ๋กœ ์ฐ์–ด๋ณด์ž.
//App.js

//์ ˆ๋Œ€ ๊ฒฝ๋กœ(absolute import)
import AppRouter from "components/Router";
import { useState } from "react";
//์ ˆ๋Œ€ ๊ฒฝ๋กœ(absolute import)
//fbase์—์„œ authService ๊ฐ€์ ธ์˜ค๊ธฐ(export๋กœ ๋‚ด๋ณด๋ƒˆ๊ธฐ ๋•Œ๋ฌธ์— {} ์ค‘๊ด„ํ˜ธ ์ณ์„œ ๊ฐ€์ ธ์™€์•ผ ํ•จ)
import { authService } from "fbase";

function App() {
  //๐Ÿ”ฅ ์ธ์ฆ์„œ๋น„์Šค์˜ ํ˜„์žฌ ์œ ์ € ์•Œ์•„๋ณด๊ธฐ
  console.log(authService.currentUser);
  //useState Hook
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  //Router ๋ Œ๋”ํ•˜๊ธฐ
  //AppRouter์— prop ์ „๋‹ฌํ•˜๊ธฐ
  return (
    <>
      <AppRouter isLoggedIn={isLoggedIn} />
      <footer>&copy; twinkle {new Date().getFullYear()}</footer>
    </>
  );
}

export default App;
  • ๊ฒฐ๊ณผ
    null์ด ๋œฌ๋‹ค. ์ •์ƒ! ์ธ์ฆํ•œ ํšŒ์›์ด ์•„๋ฌด๋„ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ^3^~

  • authService.currentUser()
    ์˜ค์ผ€์ด ์ด์ œ ์ด๊ฑธ ์–ด๋””๋‹ค ์“ฐ๋ฉด ์ข‹์„๊นŒ? ๋ฐ”๋กœ useState์— ๋„ฃ์–ด์„œ ๋กœ๊ทธ์ธ ์ธ์ฆ์„ ํ™•์ธ ํ•ด ์ฃผ์ž.

//App.js

//์ ˆ๋Œ€ ๊ฒฝ๋กœ(absolute import)
import AppRouter from "components/Router";
import { useState } from "react";
//์ ˆ๋Œ€ ๊ฒฝ๋กœ(absolute import)
//fbase์—์„œ authService ๊ฐ€์ ธ์˜ค๊ธฐ(export๋กœ ๋‚ด๋ณด๋ƒˆ๊ธฐ ๋•Œ๋ฌธ์— {} ์ค‘๊ด„ํ˜ธ ์ณ์„œ ๊ฐ€์ ธ์™€์•ผ ํ•จ)
import { authService } from "fbase";

function App() {
  //๐Ÿ”ฅ ์ธ์ฆ์„œ๋น„์Šค์˜ ํ˜„์žฌ ์œ ์ €๊ฐ€ ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€์— ๋”ฐ๋ผ (๋กœ๊ทธ์ธํ–ˆ๋Š”์ง€ ์•ˆํ–ˆ๋Š”์ง€์— ๋”ฐ๋ผ)state ๋‹ฌ๋ผ์ง
  const [isLoggedIn, setIsLoggedIn] = useState(authService.currentUser);
  //Router ๋ Œ๋”ํ•˜๊ธฐ
  //AppRouter์— prop ์ „๋‹ฌํ•˜๊ธฐ
  return (
    <>
      <AppRouter isLoggedIn={isLoggedIn} />
      <footer>&copy; twinkle {new Date().getFullYear()}</footer>
    </>
  );
}

export default App;

User interface

Firebase > Docs > Reference > JavaScript - version 9 > auth > User

Properties

PropertyTypeDescription
emailVerifiedbooleanWhether the email has been verified with sendEmailVerification() and applyActionCode().
isAnonymousbooleanWhether the user is authenticated using the ProviderId.ANONYMOUS provider.
metadataUserMetadataAdditional metadata around user creation and sign-in times.
providerDataUserInfo[]Additional per provider such as displayName and profile information.
refreshTokenstringRefresh token used to reauthenticate the user. Avoid using this directly and prefer User.getIdToken() to refresh the ID token instead.
tenantIdstring/nullThe user's tenant ID.

Methods

MethodDescription
delete()Deletes and signs out the user.
getIdToken(forceRefresh)Returns a JSON Web Token (JWT) used to identify the user to a Firebase service.
getIdTokenResult(forceRefresh)Returns a deserialized JSON Web Token (JWT) used to identitfy the user to a Firebase service.
reload()Refreshes the user, if signed in.
toJSON()Returns a JSON-serializable representation of this object.


Firebase ์ฝ˜์†” > Authentification


Firebase ์ฝ˜์†”์—์„œ Authentification์œผ๋กœ ๊ฐ€๋ฉด ์•„์ง ์œ ์ €๊ฐ€ ์•„๋ฌด๋„ ์—†๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋กœ๊ทธ์ธ ๋ฐฉ๋ฒ• ์„ค์ •

๋กœ๊ทธ์ธ ๋ฐฉ๋ฒ• ์„ค์ • ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด ๋ณด์ž.

"์ด๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ"๋กœ ๋กœ๊ทธ์ธํ•˜๊ธฐ

๋กœ๊ทธ์ธ ์ œ๊ณต ์—…์ฒด ํ•ญ๋ชฉ์—์„œ ์ด๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•œ๋‹ค.

  • ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ์ด๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์‚ฌ์šฉ ์„ค์ •์œผ๋กœ ๋ฐ”๊พผ ํ›„ ์ €์žฅํ•œ๋‹ค.

"๊ตฌ๊ธ€"๋กœ ๋กœ๊ทธ์ธํ•˜๊ธฐ

์ƒˆ ์ œ๊ณต์—…์ฒด ์ถ”๊ฐ€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ๊ตฌ๊ธ€๋„ ์„ ํƒํ•œ๋‹ค.

  • ์‚ฌ์šฉ ์„ค์ •์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ํ”„๋กœ์ ํŠธ ์ง€์› ์ด๋ฉ”์ผ์— ๋‚ด ์ด๋ฉ”์ผ์„ ์„ ํƒํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค.

"๊นƒํ—ˆ๋ธŒ"๋กœ ๋กœ๊ทธ์ธํ•˜๊ธฐ

์ƒˆ ์ œ๊ณต์—…์ฒด ์ถ”๊ฐ€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ๊นƒํ—ˆ๋ธŒ๋„ ์ถ”๊ฐ€ํ•˜์ž.

  • ๋จผ์ € ์•„๋ž˜์˜ ์Šน์ธ ์ฝœ๋ฐฑ URL์„ GitHub ์•ฑ ๊ตฌ์„ฑ์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.
  • GitHub ์•ฑ ๊ตฌ์„ฑ์— ์ถ”๊ฐ€ํ•˜๋Ÿฌ ๊ฐ€๋ณด์ž.
    โœ… GitHub > Settings > Developer settings > OAuth App > New OAuth App


  • Applicaion name์„ ์ž…๋ ฅ ํ•œ ํ›„, Homepage URL์€ ์œ„์˜ ์ฝœ๋ฐฑurl์—์„œ ๋’ท ๋ถ€๋ถ„์„ ๋บ€ url์„ ์ž…๋ ฅํ•˜๊ณ , Authorization callback URL์— ์œ„์˜ ์ฝœ๋ฐฑ url ์ „์ฒด๋ฅผ ์ž…๋ ฅํ•ด ๋‘์ž. ์ด๋Š” GitHub์„ ์œ„ํ•œ ๋ถ€๋ถ„์ด๋‹ค.
  • Register application์„ ํด๋ฆญํ•˜๋ฉด Client ID์™€ Client secret์ด ๋œฌ๋‹ค. ์ด๋ฅผ ๋ณต์‚ฌํ•˜์ž.
  • GitHub๋ฅผ ์‚ฌ์šฉ ์„ค์ •์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ํด๋ผ์ด์–ธํŠธ ID์™€ ํด๋ผ์ด์–ธํŠธ ๋ณด์•ˆ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•œ ํ›„ ์ €์žฅํ•œ๋‹ค.
profile
Always have hope๐Ÿ€ & constant passion๐Ÿ”ฅ

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