ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ”“ ์„œ๋ฒ„ ์—ด๊ธฐ

  1. Yustagram ํ”„๋กœ์ ํŠธ ํด๋” ๊ฒฝ๋กœ ๊ธฐ์ค€ functions ํด๋” ์•ˆ index.js ํŒŒ์ผ์— ๋‹ค์Œ ๋‚ด์šฉ ๋„ฃ๊ธฐ
const functions = require("firebase-functions");

  exports.helloworld = functions.https.onRequest((request, response) => {
      functions.logger.info("Hello logs!", {
          structuredData: true
      });
      response.send("Hello from Firebase!");
  });
  1. vscode ํ„ฐ๋ฏธ๋„์—์„œ functions ํŒŒ์ผ๋กœ ๊ฒฝ๋กœ ์˜ฎ๊ธฐ๊ณ  ๋‹ค์Œ์„ ์ž…๋ ฅํ•ด ํŒŒ์ด์–ด๋ฒ ์ด์Šค ํ•จ์ˆ˜๋ฅผ ๋กœ์ปฌ์—์„œ ์‹คํ–‰
firebase emulators:start --only functions
  1. ๋กœ์ปฌ ํ˜ธ์ŠคํŠธ ์„œ๋ฒ„ url์ด ์—ด๋ฆฌ๋ฉด ์—ด์–ด์„œ Hello from Firebase! ๊ฐ€ ์ถœ๋ ฅ๋จ์„ ํ™•์ธํ•˜์ž.

๐Ÿ”‘ ํŒŒ์ด์–ด๋ฒ ์ด์Šค์—์„œ ์„œ๋น„์Šคํ‚ค ๋ฐ›๊ณ  ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•˜๊ธฐ

  1. functions ํด๋” ์•ˆ์— service ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  serviceKey.json ํŒŒ์ผ ์ƒ์„ฑ
  2. ํŒŒ์ด์–ด๋ฒ ์ด์Šค ์ฝ˜์†” -> ํ”„๋กœ์ ํŠธ ์„ค์ • -> ์„œ๋น„์Šค ๊ณ„์ • -> -> Firebase Admin SDK -> ์ƒˆ ๋น„๊ณต๊ฐœ ํ‚ค ์ƒ์„ฑ -> ๋‹ค์šด๋ฐ›์€ ํŒŒ์ผ์„ ์—ด์–ด ๋‚ด๋ถ€๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ serviceKey.json ํŒŒ์ผ์— ์ž…๋ ฅ ํ›„ ์ €์žฅ
  3. functions ํด๋” ์•ˆ์— config ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  firebaseModule.json ํŒŒ์ผ ์ƒ์„ฑ
  4. Firebase Admin SDK์—์„œ Admin SDK ๊ตฌ์„ฑ ์Šค๋‹ˆํŽซ(Node.js) ๋ณต์‚ฌ ํ›„ firebaseModule.json ํŒŒ์ผ์— ์ž…๋ ฅ
  5. ์„œ๋น„์Šคํ‚ค ์œ„์น˜ ์ˆ˜์ •ํ•˜๊ณ  ์ œ์ผ ํ•˜๋‹จ์— ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋‚ด๋ณด๋‚ด๊ธฐ ์ฝ”๋“œ ์ž‘์„ฑ ํ›„ ์ €์žฅ
var serviceAccount = require('../service/serviceKey.json');
  ...
  module.exports = admin
  1. functions ํด๋” ์•ˆ์— router ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ์•ˆ์— client.js ํŒŒ์ผ ์ƒ์„ฑ

  2. client.js ํŒŒ์ผ์— ๋‹ค์Œ ๋‚ด์šฉ ์ž…๋ ฅ

const firebaseApp = requre('../config/firebaseModule')
  const express = require('express')
  const Fauth = firebaseApp.auth()
  const router = express.Router()

  module.exports = router
  1. ํ„ฐ๋ฏธ๋„์—์„œ ctrl+c๋กœ ์„œ๋ฒ„ ๋Š๊ณ  ๋‹ค์Œ ์ฝ”๋“œ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰
npm i cors

  firebase emulators:start --only functions
  1. cors ์˜ˆ์™ธ์ƒํ™ฉ์„ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด client.js ํŒŒ์ผ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ • ํ›„ ์ €์žฅ
const firebaseApp = requre('../config/firebaseModule')
  const express = require('express')
  const cors = require('cors')
  const Fauth = firebaseApp.auth()

  const router = express.Router()
  router.use(cors())
  router.options('*',cors)

  module.exports = router

๐Ÿงถ clientApi ๋งŒ๋“ค๊ธฐ

  1. functions ํด๋” ์•ˆ index.js ํŒŒ์ผ์„ ๋‹ค์‹œ ์—ด๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ • ํ›„ ์ €์žฅ
const functions = require("firebase-functions");
  const clientApi = require('./router/client')

  exports.clientApi = functions.https.onRequest(clientApi);
  1. ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด client.js ํŒŒ์ผ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ • ํ›„ ์ €์žฅ
const firebaseApp = requre('../config/firebaseModule')
  const express = require('express')
  const cors = require('cors')
  const Fauth = firebaseApp.auth()

  const router = express.Router()
  router.use(cors())
  router.options('*',cors)

  router.get('/helloworld', (req, res, next) => {
      res.json({
          msg: 'helloworld'
      })
  })

  module.exports = router
  1. ์„œ๋ฒ„ url ๋งจ ๋’ค helloworld๋ฅผ ์ง€์šฐ๊ณ  clientApi/helloworld ์ž…๋ ฅ ํ›„ msg: 'helloworld' ๋‚˜์˜ค๋Š”์ง€ ํ™•์ธ

์ด๋ฅผ ํ†ตํ•ด ํšŒ์›๊ฐ€์ž…์ด๋‚˜ ๋กœ๊ทธ์ธ ๋“ฑ์˜ ์ธ์ฆ ๊ธฐ๋Šฅ๋„ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•˜๋‹ค.


๐ŸŽฎ ์ž„์˜๋กœ ํšŒ์›๊ฐ€์ž… ๋งŒ๋“ค๊ณ  ํ™•์ธํ•˜๊ธฐ

  1. ์ด๋ฉ”์ผ๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์ธ์ฆํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ • ํ›„ ์ €์žฅ
const firebaseApp = requre('../config/firebaseModule')
  const express = require('express')
  const cors = require('cors')
  const Fauth = firebaseApp.auth()

  const router = express.Router()
  router.use(cors())
  router.options('*',cors)

  router.get('/helloworld', (req, res, next) => {

      const email = 'yuni@naver.com'
      const password = '12345678'

      Fauth.createUser({
          email : email,
          password : password
      }).then(credential => {
          const {uid} = credential

          console.log(uid)
      }).catch(err => {
          console.log(err)
      })
      res.json({
          msg: 'helloworld'
      })
  })

  module.exports = router
  1. ์„œ๋ฒ„ ์ƒˆ๋กœ๊ณ ์นจ
    ํ„ฐ๋ฏธ๋„์— uid๊ฐ€ ๋‚˜ํƒ€๋‚จ์„ ํ™•์ธ

  2. ํŒŒ์ด์–ด๋ฒ ์ด์Šค ์ฝ˜์†” -> Authentication ์ƒˆ๋กœ๊ณ ์นจ
    ์œ ์ €๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ์Œ์„ ํ™•์ธ
    ์ด์ œ ์ด๋ฅผ ์ด์šฉํ•ด์„œ ์‹ค์ œ ํšŒ์› ๋งŒ๋“œ๋Š” ์„œ๋ฒ„๋ฅผ ์„ธํŒ…ํ•ด๋ณด์ž


๐Ÿ’ป ์‹ค์ œ ํšŒ์› ๋งŒ๋“œ๋Š” ์„œ๋ฒ„ ์„ธํŒ…

  1. ์ด๋ฉ”์ผ๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์ธ์ฆํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ • ํ›„ ์ €์žฅ
const firebaseApp = requre('../config/firebaseModule')
  const express = require('express')
  const cors = require('cors')
  const Fauth = firebaseApp.auth()

  const router = express.Router()
  router.use(cors())
  router.options('*',cors)

  router.post('/user/new', (req, res, next) => {
      const {email, password} = req, body

      Fauth.createUser({
          email : email,
          password : password
      }).then(credential => {
          const {uid} = credential
          //์œ ์ € ํ”„๋กœํ•„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ
          res.status(200).json({
              msg: '์œ ์ €๊ฐ€ ์ƒ์„ฑ๋์Šต๋‹ˆ๋‹ค.'
          })
          console.log(uid)
      }).catch(err => {
          res.status(400).json({
              err
          })
      })
  })


  router.get('/helloworld', (req, res, next) => {
      res.json({
          msg: 'helloworld'
      })
  })

  module.exports = router
๋Œ“๊ธ€