๐พ vue-tennis | SSR Migration(Nuxt + Firebase) | 2. Firebase Usage
SSR Migration(Nuxt + Firebase) Series
1. Set Environment
2. Firebase Usage ๐๐ป
๐ฅ firestore usage
pages > index.vue
๋ด์ฉ ์ถ๊ฐ
<script>
export default {
async mounted() {
console.log('mounted')
const db = await this.$fire.firestore
// const users = this.$fire.firestore.collection('users').doc(uid).get()
console.log('here', db)
},
}
</script>
console
์ฃผ์ ์ฌํญ
1.this.$firestore
(x) โthis.$fire.firestore
(o)
*๋ฒ์ ์ ๋ฐ๋ผ ๋ค๋ฆ(ํ์์ ํ๊ฒฝ =>"@nuxtjs/firebase": "^7.6.1"
)
2.nuxt.config.js > firebase > services
โfirestore: true
ํ์ธ
๐จ๐ปโ๐ functions usage
REF : ๋ก์ปฌ์์ ํจ์ ์คํ
https://firebase.google.com/docs/functions/local-emulator
REF : ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก์ ํธ์ Firebase ์ถ๊ฐ
https://firebase.google.com/docs/web/setup
REF : Functions
https://firebase.google.com/docs/functions/manage-functions?hl=ko
1. ์ ํ
$ npm install -g firebase-tools
: firebase-tools ์ค์น
$ firebase login
: firebase์ ๋ก๊ทธ์ธ
2. firebase ์ด๊ธฐํ
$ firebase init
: functions
๋ง ์ ํํ ํ ์ค์น
(ํ๋ก์ ํธ ๋ด์ functions
ํด๋๊ฐ ์์ฑ๋ ๊ฒ์ ํ์ธํ ์ ์์)
3. nuxt.config.js
๋ด์ฉ ์์
firebase: {
config: {
...
},
services: {
...
functions: {
location: 'asia-northeast3', // firebase project ์ค์ ์ ๋ฐ๋ฆ
// Cloud Functions ์์น
// https://firebase.google.com/docs/functions/locations
},
4. firebase admin SDK ์ค์
local
์์myFirebaseProject
๋ฅผ ๋ค๋ฃฐ ์ ์๊ฒ ๋จ
๊ทธ๋งํผ ๋ฆฌ์คํฌ๊ฐ ํฌ๊ธฐ ๋๋ฌธ์key
๊ด๋ฆฌ์ ์ ๊ฒฝ์จ์ผ ํจ
1. firebase > myProject > ํ๋ก์ ํธ ์ค์ > ์๋น์ค ๊ณ์ > firebase admin SDK ๊ตฌ์ฑ ์ค๋ํซ
ํ์ธ
2. ํ๋จ์ ์ ๋น๊ณต๊ฐ ํค ์์ฑ
์ ํตํด์ ํค ํ์ผ์ ๋ค์ด๋ฐ์ ํ myProject > functions
๊ฒฝ๋ก๋ก ์ด๋
(key ํ์ผ๋ช
์ ์๋ง๊ฒ ์์ ํจ / ํ์์ ๊ฒฝ์ฐ vue-tennis-key.json
)
3. firebase console
๋ด firebase admin SDK ๊ตฌ์ฑ ์ค๋ํซ
์ ๋ณต์ฌํ์ฌ
myProject > functions > index.js
์ ๋ถ์ฌ๋ฃ์
const functions = require('firebase-functions')
var admin = require('firebase-admin')
var serviceAccount = require('./vue-tennis-key.json')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'myDatabaseURL',
})
const db = admin.database()
const fdb = admin.firestore()
4. gitignore ๋ฑ๋ก (์ค์โ๏ธโ๏ธ)
root > functions > .gitignore
root > .gitignore
๊ณผ ๊ตฌ๋ถ๋จ์ ์ฃผ์ ๐จ
node_modules
vue-tennis-key.json
.env
5. ์๋ก์ด ํ ์คํธ ํจ์(function) ์์ฑ
: root > functions > index.js
๋ด์ฉ ์์
exports.createUser = functions.auth.user().onCreate(async (user) => {
console.log('firebase funtions : createUser')
const { uid, email, displayName, photoURL } = user
const time = new Date()
const userInfo = {
email,
displayName,
photoURL,
nickName: displayName,
sex: '',
birth: '',
location: '',
}
await fdb.collection('users').doc(uid).set(userInfo) // set user at Firestore
userInfo.createdAt = time.getTime()
db.ref('users').child(uid).set(userInfo) // set user at RealTime Database
})
exports.deleteUser = functions.auth.user().onDelete(async (user) => {
console.log('firebase funtions : deleteUser')
const { uid } = user
await db.ref('users').child(uid).remove()
await fdb.collection('users').doc(uid).delete()
})
6. ํจ์ ๋ฐฐํฌ(deploy function)
: $ firebase deploy --only functions:createUser
: $ firebase deploy --only functions:deleteUser
7. ํ ์คํธ ํจ์ ์๋ ํ์ธ
: firebase > myProject > Functions > ๋ก๊ทธ
- ์์์ ์์ฑํ
function
์์console
๋ก ์ฐ์ ๋ด์ฉ์ ํ์ธํ ์ ์์ - ์ฌ๊ธฐ์ ์์๋ก ์์ฑํ
createUser
ํจ์์ ๊ฒฝ์ฐ,
auth
์์user
๊ฐ ์๋ก ์์ฑ๋๊ฑฐ๋ ์ญ์ ๋๋ ๊ฒฝ์ฐ๋ฅผ ํธ๋ฆฌ๊ฑฐ๋ก ํ๊ณ ์๊ธฐ ๋๋ฌธ์,
์๋auth usage
ํํธ์์ ์์ฑํ ์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์๋ํจ.
๋ฐ๋ผ์auth usage
ํํธ๋ฅผ ์์ฑํ ํ์7. ํ ์คํธ ํจ์ ์๋ ํ์ธ
๋ด์ฉ์ ํ์ธ ๋ฐ๋
๐ auth usage
1. nuxt.config.js
๋ด์ฉ ์์
: ์๋์ ๊ฐ์ด ์์ฑํ ๊ฒฝ์ฐ, Auth State
๊ฐ ๋ณ๊ฒฝ๋ ๋๋ง๋ค onAuthStateChanged
Action์ด ์คํ๋จ.
firebase: {
config: {
...
},
services: {
...
auth: {
initialize: {
onAuthStateChangedAction: 'onAuthStateChanged',
},
ssr: true,
disableEmulatorWarnings: false,
},
}
}
2. pages > mypage.vue
๋ด์ฉ ์์
<template>
<v-container>
<v-card>
<v-card-text>
<v-btn @click="login" :loading="isProcessing">login</v-btn>
<v-btn @click="logout" :loading="isProcessing">logout</v-btn>
</v-card-text>
</v-card>
</v-container>
</template>
<script>
export default {
data() {
return {
isProcessing: false,
}
},
methods: {
async login() {
if (this.isProcessing) return
this.isProcessing = true
const provider = new this.$fireModule.auth.GoogleAuthProvider()
try {
const snapshot = await this.$fire.auth.signInWithPopup(provider)
} catch (err) {
alert('๋ก๊ทธ์ธ ์คํจ', err)
console.log(err)
} finally {
this.isProcessing = false
}
},
async logout() {
if (this.isProcessing) return
this.isProcessing = true
try {
await this.$fire.auth.signOut()
console.log('๋ก๊ทธ์์ ์ฑ๊ณต')
} catch (err) {
alert('๋ก๊ทธ์์ ์คํจ.', err)
console.log(err)
} finally {
this.isProcessing = false
}
},
},
}
</script>
http://localhost:3000/mypage
3. Vuex(store)
store > state.js
export default () => ({
user: null,
fireUser: null,
})
store > mutations.js
mutations
์์state.user = payload
์ ๊ฐ์ด ์ง์ ํ ๋น์ ํ๋ฉด ์๋ฌ ๋ฐ์ ๐จ
import initialState from './state'
export default {
RESET_STORE: (state) => {
Object.assign(state, initialState())
},
SET_FIRE_USER: (state, fireUser) => {
const { uid, email, displayName, photoURL } = fireUser
state.fireUser = { uid, email, displayName, photoURL }
},
SET_USER: (state, user) => {
if (user) {
const {
email,
displayName,
photoURL,
nickName,
sex,
birth,
location,
} = user
state.user = {
email,
displayName,
photoURL,
nickName,
sex,
birth,
location,
}
} else {
state.user = null
}
},
}
store > actions.js
export default {
async onAuthStateChanged({ commit }, { authUser }) {
let unsubscribe = null
if (!authUser) {
if (unsubscribe) unsubscribe()
return
}
commit('SET_FIRE_USER', authUser)
const subscribe = (authUser) => {
const ref = this.$fire.firestore.collection('users').doc(authUser.uid)
unsubscribe = ref.onSnapshot((doc) => {
if (doc.exists) commit('SET_USER', doc.data())
}, console.error)
}
subscribe(authUser)
},
}
๐งฏ๐ง๐ปโ๐๐ฉ๐ปโ๐๐ฏ
REF : nuxt-firebase-demo
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(๐พ vue-tennis | SSR Migration(Nuxt + Firebase) | 2. Firebase Usage), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@protect-me/vue-tennis-ssr-2์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค