๐Ÿ’ป My Work/๐Ÿ”ฅ React

[React] Firebase - Authentication์œผ๋กœ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

Jaeseo Kim 2022. 9. 19. 23:48

https://firebase.google.com/?hl=ko&gclid=CjwKCAjwpqCZBhAbEiwAa7pXeX-_LGJVKKRu3WA1Zbs-awSKivk12siAqSNzd60QEYrX7TKbOp59MxoCHK0QAvD_BwE&gclsrc=aw.ds 

 

Firebase

Firebase๋Š” ๊ณ ํ’ˆ์งˆ ์•ฑ์„ ๋น ๋ฅด๊ฒŒ ๊ฐœ๋ฐœํ•˜๊ณ  ๋น„์ฆˆ๋‹ˆ์Šค๋ฅผ ์„ฑ์žฅ์‹œํ‚ค๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” Google์˜ ๋ชจ๋ฐ”์ผ ํ”Œ๋žซํผ์ž…๋‹ˆ๋‹ค.

firebase.google.com

 

1. npm install


https://www.npmjs.com/package/firebase

npm install --save firebase

 

2. firebase ์ดˆ๊ธฐํ™”


Firebase > Console > ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ > ํ”„๋กœ์ ํŠธ > ํ”„๋กœ์ ํŠธ ์„ค์ •
์•„๋ž˜์˜ Firebase SDK ๋ฅผ ๋ณต์‚ฌํ•ด ์ค๋‹ˆ๋‹ค.

 

Firebase์™€ ๊ด€๋ จ๋œ ์„œ๋น„์Šค๋“ค์€ ์ปดํฌ๋„ŒํŠธ์™€ ๋”ฐ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด, srcํด๋”์— service ํด๋”๋ฅผ ์ƒ์„ฑ ํ›„ firebase.js ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

../src/service/firebase.js

 

๋ณต์‚ฌํ•œ ๋‚ด์šฉ์„ ๋ถ™์—ฌ์ค๋‹ˆ๋‹ค.

  • ํ•„์š” ์—†๋Š” ๊ฒƒ๋“ค์€ ์•ˆ ์จ์ฃผ๊ณ , ๋กœ๊ทธ์ธ๊ณผ ํšŒ์›๊ฐ€์ž… ํ•  ๋•Œ ํ•„์š”ํ•œ api key์™€ auth domain๋งŒ ๋‚จ๊ฒจ์คฌ์Šต๋‹ˆ๋‹ค.
  • key๋Š” ์™ธ๋ถ€์— ๋…ธ์ถœ๋˜๋ฉด ์•ˆ ๋˜๋‹ˆ๊นŒ, env ํŒŒ์ผ์— ์ˆจ๊ฒจ์ค๋‹ˆ๋‹ค.
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";

/** ํ”„๋กœ์ ํŠธ config */
const firebaseConfig = {
  apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
  authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

// Authentication SDK ์ถ”๊ฐ€ ๋ฐ ์ดˆ๊ธฐํ™”
// Initialize Firebase Authentication and get a reference to the service
const auth = getAuth(app);

export { auth };

 

 

๋กœ๊ทธ์ธ์„ ํ•  ๋•Œ, ์ด๋ ‡๊ฒŒ Google ๋กœ๊ทธ์ธ ํŒ์—…์ฐฝ์ด ๋œจ๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 

3.  Google ๋กœ๊ทธ์ธ ์—…์ฒด ์„ค์ •


์šฐ์„ , ์—ฐ๊ฒฐํ•˜๊ณ  ์‹ถ์€ ๋กœ๊ทธ์ธ ์—…์ฒด๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”. (์ €๋Š” Google๋งŒ ํ–ˆ์Šต๋‹ˆ๋‹ค.)

Firebase > Console > ๋ณธ์ธ project ์„ ํƒ > Authentication > Sign-in method

 

4. Google ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฆ


https://firebase.google.com/docs/auth/web/google-signin?authuser=0 

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ Google์„ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฆ  |  Firebase

Google์€ ํ‘์ธ ๊ณต๋™์ฒด๋ฅผ ์œ„ํ•œ ์ธ์ข…์  ํ‰๋“ฑ์„ ์ถ”๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ ์˜๊ฒฌ ๋ณด๋‚ด๊ธฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ Google์„ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฆ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ์ •๋ฆฌํ•˜๊ธฐ ๋‚ด ํ™˜๊ฒฝ์„ค์ •์„ ๊ธฐ

firebase.google.com

 

../src/service/firebase.js ์— Google auth provider๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

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

/** ํ”„๋กœ์ ํŠธ config */
const firebaseConfig = {
  apiKey: env.REACT_APP_FIREBASE_API_KEY,
  authDomain: env.REACT_APP_FIREBASE_AUTH_DOMAIN,
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

// Authentication SDK ์ถ”๊ฐ€ ๋ฐ ์ดˆ๊ธฐํ™”
// Initialize Firebase Authentication and get a reference to the service
const auth = getAuth(app);

/** ๊ตฌ๊ธ€ auth provider */
const googleAuthProvider = new GoogleAuthProvider();

export { auth, googleAuthProvider };

 

../src/service/auth.js ์„ ์ƒ์„ฑํ•ด์ฃผ๊ณ , ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ๋กœ๊ทธ์ธ ํŒ์—…์ฐฝ์„ ๊ตฌํ˜„ํ•ด์ค๋‹ˆ๋‹ค.

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

/** ๋กœ๊ทธ์ธ ํŒ์—… */
export const signInGoogle = signInWithPopup(auth, googleAuthProvider)
  .then((result) => {
    // This gives you a Google Access Token. You can use it to access the Google API.
    const credential = GoogleAuthProvider.credentialFromResult(result);
    const token = credential.accessToken;
    // The signed-in user info.
    const user = result.user;
    // ...
  })
  .catch((error) => {
    // Handle Errors here.
    const errorCode = error.code;
    const errorMessage = error.message;
    // The email of the user's account used.
    const email = error.customData.email;
    // The AuthCredential type that was used.
    const credential = GoogleAuthProvider.credentialFromError(error);
    // ...
  });

 

signInGoogle์„ ์ž˜ ํ™œ์šฉํ•˜์—ฌ, ๋ณธ์ธ์˜ ํ”„๋กœ์ ํŠธ ๋‚ด์—์„œ ๋กœ๊ทธ์ธ ํŒ์—…์ฐฝ์„ ๋œจ๊ฒŒ ํ•  ๊ณณ์— ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด Google ๋กœ๊ทธ์ธ ํŒ์—…์ฐฝ์ด ๋œจ๋„๋ก...