I'm trying to implement Firebase in a Nuxt app (static). I installed @nuxtjs/firebase, configured it, and make it worked with auth (both login and logout works). I want to lock the entire app behing a login.
But when i enable the middleware the app crash. This is because, I think, there is some additional step to do in order to make the module work in SSR. I think that in the server the middleware will trigger a redirect as the user is not set, am I right? How can I achieve a working middleware?
Code for middleware auth.js:
export default function ({ store, redirect }) {
if (!store.state.user) {
return redirect('/login')
}
}
Code for store index.js
export const state = () => ({
user: null,
userData: {}
})
export const getters = {
}
export const mutations = {
onAuthStateChangedMutation(state, {authUser}){
if (!authUser) {
state.user = null
} else {
const { uid, email } = authUser
state.user = { uid, email }
}
},
setUserData(state, payload){
state.userData = payload
}
}
export const actions = {
async onAuthStateChangedAction({commit}, {authUser}){
if (!authUser) {
state.userData = {}
} else {
try{
const userRef = await this.$fire.firestore.collection('users').doc(authUser.uid).get()
const userData = await userRef.data()
commit('setUserData', userData)
}
catch(e){
console.log(e)
}
}
}
}
On nuxt.config
firebase: {
services:{
auth:{
ssr: true,
initialize:{
onAuthStateChangedMutation: 'onAuthStateChangedMutation',
onAuthStateChangedAction: 'onAuthStateChangedAction',
}
},
firestore: true,
analytics: true
}
},
router:{
middleware: 'auth'
}