SSR Migration(Nuxt + Firebase) Series
1. Set Environment
2. Firebase 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
νμΈ
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
$ npm install -g firebase-tools
: firebase-tools μ€μΉ
$ firebase login
: firebaseμ λ‘κ·ΈμΈ
$ firebase init
: functions
λ§ μ νν ν μ€μΉ
(νλ‘μ νΈ λ΄μ functions
ν΄λκ° μμ±λ κ²μ νμΈν μ μμ)
nuxt.config.js
λ΄μ© μμ firebase: {
config: {
...
},
services: {
...
functions: {
location: 'asia-northeast3', // firebase project μ€μ μ λ°λ¦
// Cloud Functions μμΉ
// https://firebase.google.com/docs/functions/locations
},
local
μμmyFirebaseProject
λ₯Ό λ€λ£° μ μκ² λ¨
κ·Έλ§νΌ 리μ€ν¬κ° ν¬κΈ° λλ¬Έμkey
κ΄λ¦¬μ μ κ²½μ¨μΌ ν¨
firebase > myProject > νλ‘μ νΈ μ€μ > μλΉμ€ κ³μ > firebase admin SDK κ΅¬μ± μ€λν«
νμΈμ λΉκ³΅κ° ν€ μμ±
μ ν΅ν΄μ ν€ νμΌμ λ€μ΄λ°μ ν myProject > functions
κ²½λ‘λ‘ μ΄λ(key νμΌλͺ
μ μλ§κ² μμ ν¨ / νμμ κ²½μ° vue-tennis-key.json
)
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()
root > functions > .gitignore
root > .gitignore
κ³Ό ꡬλΆλ¨μ μ£Όμ π¨node_modules
vue-tennis-key.json
.env
: 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()
})
: $ firebase deploy --only functions:createUser
: $ firebase deploy --only functions:deleteUser
: firebase > myProject > Functions > λ‘κ·Έ
function
μμ console
λ‘ μ°μ λ΄μ©μ νμΈν μ μμcreateUser
ν¨μμ κ²½μ°,auth
μμ user
κ° μλ‘ μμ±λκ±°λ μμ λλ κ²½μ°λ₯Ό νΈλ¦¬κ±°λ‘ νκ³ μκΈ° λλ¬Έμ,auth usage
ννΈμμ μμ±ν μ½λλ₯Ό κΈ°λ°μΌλ‘ μλν¨.auth usage
ννΈλ₯Ό μμ±ν νμ 7. ν
μ€νΈ ν¨μ μλ νμΈ
λ΄μ©μ νμΈ λ°λnuxt.config.js
λ΄μ© μμ : μλμ κ°μ΄ μμ±ν κ²½μ°, Auth State
κ° λ³κ²½λ λλ§λ€ onAuthStateChanged
Actionμ΄ μ€νλ¨.
firebase: {
config: {
...
},
services: {
...
auth: {
initialize: {
onAuthStateChangedAction: 'onAuthStateChanged',
},
ssr: true,
disableEmulatorWarnings: false,
},
}
}
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
export default () => ({
user: null,
fireUser: null,
})
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
}
},
}
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