๐Ÿฅฌ๐Ÿ–ํ”„๋ก ํŠธ๋ฅผ Firebase์— ์‹ธ์„œ ๋“œ์…”๋ณด์„ธ์š” | 4 - Sign in with Google ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž

Dongjun Kimยท2022๋…„ 12์›” 5์ผ
3
post-thumbnail

firebase๋Š” hosting ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ๋ฐฑ์—”๋“œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.
์ด๋ฒˆ์—๋Š” ํ”ํžˆ ๋ณผ ์ˆ˜ ์žˆ๋Š” Sign in with Google ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž.

์ด ํฌ์ŠคํŒ…์€ ์‹œ๋ฆฌ์ฆˆ์˜ 4๋ฒˆ์งธ ๊ธ€๋กœ 1ํŽธ, 2ํŽธ, 3ํŽธ์„ ๋†“์ณค๋‹ค๋ฉด ๊ผญ ๋ณด๊ณ  ์˜ค์ž!

ํ”„๋กœ์ ํŠธ์—์„œ Google Auth ํ™œ์„ฑํ™”ํ•˜๊ธฐ

Firebase ์ฝ˜์†”์˜ Authentication ๋ฉ”๋‰ด๋กœ ๊ฐ€์ค€๋‹ค.


2๋ฒˆ์งธ ํƒญ์˜ Sign-in method์—์„œ ์ œ๊ณต์—…์ฒด ์ค‘ Google์„ ์„ ํƒํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


ํ”„๋กœ์ ํŠธ์— Google Auth ์ถ”๊ฐ€ํ•˜๊ธฐ

Google Auth๋ฅผ importํ•ด์™€์„œ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

import { getAuth, GoogleAuthProvider, signInWithPopup } from "firebase/auth";

index.js์˜ ๋งจ ์œ„์— ์œ„์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. ์ด์ œ getAuth, GoogleAuthProvider, signInWithPopup์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค.


const auth = getAuth();

firebase์˜ ๊ธฐ๋Šฅ๋“ค์€ get(โ€ฆ) ํ˜•์‹์˜ ์ด๋ฆ„์„ ๊ฐ–๋Š” ๋ชจ๋“ˆ์„ ์ œ๊ณตํ•œ๋‹ค. ๋ถˆ๋Ÿฌ์™€์„œ auth๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•ด์ค€๋‹ค.


๋ฒ„ํŠผ๊ณผ onclick ์™„์„ฑํ•˜๊ธฐ

<button class="signInBtn">Sign in with Google</button>

์šฐ์„  index.html ํŒŒ์ผ์— ๋ฒ„ํŠผ์„ ๋งŒ๋“ค์–ด์ฃผ์ž.


const signInBtn = document.getElementById("signInBtn");

index.js์—์„œ๋Š” ๋ฒ„ํŠผ์„ ๋ถˆ๋Ÿฌ์˜จ ํ›„


signInBtn.onclick = function () {
  const provider = new GoogleAuthProvider();
  provider.addScope("profile");
  provider.addScope("email");

  signInWithPopup(auth, provider)
    .then((result) => {
      const user = result.user;
      const outputElement = document.createElement("h1");

      // outputDiv.textContent = Object.keys(user);
      //providerId,proactiveRefresh,reloadUserInfo,reloadListener,uid,auth,stsTokenManager,accessToken,displayName,email,emailVerified,phoneNumber,photoURL,isAnonymous,tenantId,providerData,metadata

      outputElement.textContent = `Welcome, ${user.displayName}`;
      document.body.appendChild(outputElement);
    })
    .catch((error) => {
      const errorMessage = error.message;
      const outputElement = document.createElement("h1");
      outputElement.textContent = `Error: ${errorMessage}`;
      document.body.appendChild(outputDiv);
    });
};

ํด๋ฆญ ์ด๋ฒคํŠธ ์‹œ ์‹คํ–‰๋  ๋กœ์ง์„ ์œ„์™€ ๊ฐ™์ด ๋งŒ๋“ค์–ด์ค€๋‹ค.

๋ณต์žกํ•ด๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, Google์ด๋ž€ Provider๋ฅผ ํ†ตํ•ด signInWithPopup์„ ์‹คํ–‰์‹œํ‚ค๊ณ , ์„ฑ๊ณตํ•˜๋ฉด result๋กœ๋ถ€ํ„ฐ ์‚ฌ์šฉ์ž์˜ ์ด๋ฆ„์„, ์‹คํŒจํ•˜๋ฉด ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ ๋ฟ์ด๋‹ค.

์ฃผ์„์ฒ˜๋ฆฌ๋œ ๋ถ€๋ถ„์€ result.user ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์–ด๋–ค ์ด๋ฆ„์˜ key๋“ค์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ๋ชฐ๋ผ์„œ ํ™”๋ฉด์— ์ฐ์–ด๋ณธ ํ”์ ์ด๋‹ค. displayName์ด ์žˆ์œผ๋‹ˆ ์„ฑ๊ณตํ•˜๋ฉด ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ํ™˜์˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ์ž.

index.js ์ „์ฒด ์ฝ”๋“œ

import { initializeApp } from "firebase/app";
import {
  getAuth,
  GoogleAuthProvider,
  getRedirectResult,
  signInWithRedirect,
  onAuthStateChanged,
  signInWithPopup,
} from "firebase/auth";
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
  apiKey: "AIzaSyATCD9cxXSJ2RSUwd9WSDHTDMV7FuSaCgo",
  authDomain: "fir-demo-48710.firebaseapp.com",
  projectId: "fir-demo-48710",
  storageBucket: "fir-demo-48710.appspot.com",
  messagingSenderId: "49933278590",
  appId: "1:49933278590:web:bb643d3e5d7b2077cd672f",
  measurementId: "G-HFF9Z86BJM",
};

initializeApp(firebaseConfig);
const auth = getAuth();

const signInBtn = document.getElementById("signInBtn");
signInBtn.onclick = function () {
  const provider = new GoogleAuthProvider();
  provider.addScope("profile");
  provider.addScope("email");

  signInWithPopup(auth, provider)
    .then((result) => {
      const user = result.user;
      const outputElement = document.createElement("h1");
      // outputDiv.textContent = Object.keys(user);
      //providerId,proactiveRefresh,reloadUserInfo,reloadListener,uid,auth,stsTokenManager,accessToken,displayName,email,emailVerified,phoneNumber,photoURL,isAnonymous,tenantId,providerData,metadata
      outputElement.textContent = `Welcome, ${user.displayName}`;
      document.body.appendChild(outputElement);
    })
    .catch((error) => {
      const errorMessage = error.message;
      const outputElement = document.createElement("h1");
      outputElement.textContent = `Error: ${errorMessage}`;
      document.body.appendChild(outputDiv);
    });
};

์ „์ฒด ์ฝ”๋“œ๋Š” ์œ„์™€ ๊ฐ™๋‹ค.

build and deploy

npm run build

์ฝ”๋“œ์— ๋ณ€ํ™”๊ฐ€ ์ƒ๊ฒผ์œผ๋‹ˆ webpack์œผ๋กœ ๋‹ค์‹œ ๋นŒ๋“œ์‹œ์ผœ์ฃผ์ž.

firebase deploy

๋ฐฐํฌ์‹œํ‚จ๋‹ค. (๋งŒ์•ฝ firebase๋ผ๋Š” ๊ฑธ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” ๋‚ด์šฉ์˜ ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜ค๋ฉด, ์ด์ „ ๊ธ€๋กœ ๋Œ์•„๊ฐ€์„œ firebase tools ์„ค์น˜๋ถ€ํ„ฐ firebase login, firebase init๊นŒ์ง€ ์‹คํ–‰์‹œ์ผœ์ฃผ์ž.)


๋ฐฐํฌ๋œ ์‚ฌ์ดํŠธ์—์„œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ๋กœ๊ทธ์ธํ•ด๋ณด์ž.


์ž˜ ๋‚˜์˜ค๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

Summary

Firebase ํ”„๋กœ์ ํŠธ ์‹œ์ž‘๋ถ€ํ„ฐ, ๋ฐฐํฌ ๊ทธ๋ฆฌ๊ณ  Google Auth ๊ธฐ๋Šฅ ์ถ”๊ฐ€๊นŒ์ง€ ํ•ด๋ดค๋‹ค.
์ด ์™ธ์—๋„ Firebase์—๋Š” Storage ๋“ฑ ๋ฐฑ์—”๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์ด ๋” ๋งŽ์ด ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ฒ˜์Œ ์‹œ์ž‘ํ•ด๋ณด๋ ค๊ณ  ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•œ ๊ฐ€์ด๋“œ์ธ ์ด ์‹œ๋ฆฌ์ฆˆ๋Š” ์—ฌ๊ธฐ์„œ ๋งˆ๋ฌด๋ฆฌ ์ง€์œผ๋ ค๊ณ  ํ•œ๋‹ค.

โ˜บ๏ธ์ฒ˜์Œ ์ž‘์„ฑํ•œ ํฌ์ŠคํŠธ์˜€๋Š”๋ฐ, ์•Œ๊ฒŒ ๋œ ๋‚ด์šฉ์„ ๊ณต์œ ํ•˜๋Š” ๊ฒŒ ๋‚˜๋ฆ„ ์˜๋ฏธ๊ฐ€ ์žˆ๊ณ  ์žฌ๋ฐŒ๋„ค์š”. ์•ž์œผ๋กœ ์“ฐ๊ฒŒ ๋  ๋‹ค๋ฅธ ๊ธ€๋„ ๋งŽ์ด ์ฝ์–ด์ฃผ์„ธ์š”๐Ÿ‘๐Ÿป

profile
์ค‘์š”ํ•œ ๊ฒƒ์€ ๊บพ์ด์ง€ ์•Š๋Š” ๋งˆ์Œโ˜•๏ธ

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