ํฐ์คํ ๋ฆฌ ๋ทฐ
Cloud/Firebase
๐ ์ธ์ฆ ๊ธฐ๋ฅ ๊ตฌํํ๊ณ Firebase ์๋ฒ ์ฐ๊ฒฐํ๊ธฐ
yunieyunie 2023. 1. 16. 10:11๐ ์๋ฒ ์ด๊ธฐ
- 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!");
});
- vscode ํฐ๋ฏธ๋์์ functions ํ์ผ๋ก ๊ฒฝ๋ก ์ฎ๊ธฐ๊ณ ๋ค์์ ์ ๋ ฅํด ํ์ด์ด๋ฒ ์ด์ค ํจ์๋ฅผ ๋ก์ปฌ์์ ์คํ
firebase emulators:start --only functions
- ๋ก์ปฌ ํธ์คํธ ์๋ฒ url์ด ์ด๋ฆฌ๋ฉด ์ด์ด์ Hello from Firebase! ๊ฐ ์ถ๋ ฅ๋จ์ ํ์ธํ์.
๐ ํ์ด์ด๋ฒ ์ด์ค์์ ์๋น์คํค ๋ฐ๊ณ ํ๋ก์ ํธ์ ์ ์ฉํ๊ธฐ
- functions ํด๋ ์์ service ํด๋๋ฅผ ๋ง๋ค๊ณ serviceKey.json ํ์ผ ์์ฑ
- ํ์ด์ด๋ฒ ์ด์ค ์ฝ์ -> ํ๋ก์ ํธ ์ค์ -> ์๋น์ค ๊ณ์ -> -> Firebase Admin SDK -> ์ ๋น๊ณต๊ฐ ํค ์์ฑ -> ๋ค์ด๋ฐ์ ํ์ผ์ ์ด์ด ๋ด๋ถ๋ฅผ ๋ณต์ฌํ์ฌ serviceKey.json ํ์ผ์ ์ ๋ ฅ ํ ์ ์ฅ
- functions ํด๋ ์์ config ํด๋๋ฅผ ๋ง๋ค๊ณ firebaseModule.json ํ์ผ ์์ฑ
- Firebase Admin SDK์์ Admin SDK ๊ตฌ์ฑ ์ค๋ํซ(Node.js) ๋ณต์ฌ ํ firebaseModule.json ํ์ผ์ ์ ๋ ฅ
- ์๋น์คํค ์์น ์์ ํ๊ณ ์ ์ผ ํ๋จ์ ํ์ด์ด๋ฒ ์ด์ค ๋ด๋ณด๋ด๊ธฐ ์ฝ๋ ์์ฑ ํ ์ ์ฅ
var serviceAccount = require('../service/serviceKey.json');
...
module.exports = admin
functions ํด๋ ์์ router ํด๋๋ฅผ ๋ง๋ค๊ณ ๊ทธ ์์ client.js ํ์ผ ์์ฑ
client.js ํ์ผ์ ๋ค์ ๋ด์ฉ ์ ๋ ฅ
const firebaseApp = requre('../config/firebaseModule')
const express = require('express')
const Fauth = firebaseApp.auth()
const router = express.Router()
module.exports = router
- ํฐ๋ฏธ๋์์ ctrl+c๋ก ์๋ฒ ๋๊ณ ๋ค์ ์ฝ๋ ์์๋๋ก ์คํ
npm i cors
firebase emulators:start --only functions
- 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 ๋ง๋ค๊ธฐ
- functions ํด๋ ์ index.js ํ์ผ์ ๋ค์ ์ด๊ณ ๋ค์๊ณผ ๊ฐ์ด ์์ ํ ์ ์ฅ
const functions = require("firebase-functions");
const clientApi = require('./router/client')
exports.clientApi = functions.https.onRequest(clientApi);
- ํ ์คํธ๋ฅผ ์ํด 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
- ์๋ฒ url ๋งจ ๋ค helloworld๋ฅผ ์ง์ฐ๊ณ clientApi/helloworld ์ ๋ ฅ ํ msg: 'helloworld' ๋์ค๋์ง ํ์ธ
์ด๋ฅผ ํตํด ํ์๊ฐ์ ์ด๋ ๋ก๊ทธ์ธ ๋ฑ์ ์ธ์ฆ ๊ธฐ๋ฅ๋ ์ฒ๋ฆฌ ๊ฐ๋ฅํ๋ค.
๐ฎ ์์๋ก ํ์๊ฐ์ ๋ง๋ค๊ณ ํ์ธํ๊ธฐ
- ์ด๋ฉ์ผ๊ณผ ๋น๋ฐ๋ฒํธ๋ก ์ธ์ฆํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ด ์์ ํ ์ ์ฅ
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
์๋ฒ ์๋ก๊ณ ์นจ
ํฐ๋ฏธ๋์ uid๊ฐ ๋ํ๋จ์ ํ์ธํ์ด์ด๋ฒ ์ด์ค ์ฝ์ -> Authentication ์๋ก๊ณ ์นจ
์ ์ ๊ฐ ๋ฑ๋ก๋์ด ์์์ ํ์ธ
์ด์ ์ด๋ฅผ ์ด์ฉํด์ ์ค์ ํ์ ๋ง๋๋ ์๋ฒ๋ฅผ ์ธํ ํด๋ณด์
๐ป ์ค์ ํ์ ๋ง๋๋ ์๋ฒ ์ธํ
- ์ด๋ฉ์ผ๊ณผ ๋น๋ฐ๋ฒํธ๋ก ์ธ์ฆํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ด ์์ ํ ์ ์ฅ
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
'Cloud > Firebase' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ Firebase๋ก ๋ก๊ทธ์ธ, ๋ก๊ทธ์์ ์ฒ๋ฆฌํ๊ธฐ (0) | 2023.01.16 |
---|---|
๐ป Firebase ํ๋ก์ ํธ ํ๊ฒฝ ์ธํ ํ๊ธฐ (0) | 2023.01.13 |
๋๊ธ