Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuxt SSR auth guard with Firebase auth

I'm trying to implement auth guards in Nuxt with Firebase Auth, but I keep running in to problems. At the moment I'm able to login, but the correct page isn't loaded after login, after login the user should be redirected to the '/profile-overview' page but that doesn't happen. When I navigate away from the 'profile' page to another page and then go back I do automatically go to the 'profile-overview' page. So the login works, there is just something wrong with the navigation / refresh of the page after login. Also when I refresh the page the user is logged out again, I would except the user to still be logged in then?

My code so far:

Page:

loginGoogle () {
            this.$store.dispatch('signInWithGoogle').then(() => {
                console.log('reload')
                location.reload()
                //window.location.reload(true)
            }).catch((e) => {
                this.title = 'Google login failed'
                this.message =
                    "Something went wrong, please try again later. If this problem keeps happening please contact: [email protected] " + "Error: " + e.message;
                this.dialog = true;
            })
        },

Middleware:

export default function ({ store, redirect, route }) {
    console.log('user state' + store.state.user)
    console.log('route ' + route.name)
    store.state.user != null && route.name == 'profile' ? redirect('/profile-overview') : ''
    store.state.user == null && isAdminRoute(route) ? redirect('/profile') : ''
  }

  function isAdminRoute(route) {
    if (route.matched.some(record => record.path == '/profile-overview')) {
      return true
    }
  }

Plugin:

import { auth } from '@/services/fireInit.js'

export default context => {
  const { store } = context

  return new Promise((resolve, reject) => {
    auth.onAuthStateChanged(user => {
      if (user) {
        return resolve(store.commit('setUser', user))
      }
      return resolve()
    })
  })
}

Store (function to login only:

   signInWithGoogle ({ commit }) {
            return new Promise((resolve, reject) => {
                auth.signInWithPopup(GoogleProvider).then((result) => {
                    // This gives you a Google Access Token. You can use it to access the Google API.
                    var token = result.credential.accessToken;
                    // The signed-in user info.
                    var user = result.user;
                    return resolve(store.commit(state.user, result.user))
                    // ...
                }).catch((error) => {
                    // Handle Errors here.
                    var errorCode = error.code;
                    var errorMessage = error.message;
                    // The email of the user's account used.
                    var email = error.email;
                    // The firebase.auth.AuthCredential type that was used.
                    var credential = error.credential;
                    // ...
                })
            })
        },

Does anyone have any idea what I could be doing wrong, or some documentation / tutorial I could read?

Thanks in advance.

like image 459
jonas Avatar asked Sep 21 '18 21:09

jonas


1 Answers

You need to init your user on server in nuxtServerInit. See this repo for example implementation https://github.com/davidroyer/nuxt-ssr-firebase-auth.v2

like image 188
Aldarund Avatar answered Sep 30 '22 04:09

Aldarund