nuxt-community / firebase-module

🔥 Easily integrate Firebase into your Nuxt project. 🔥
https://firebase.nuxtjs.org
MIT License
642 stars 98 forks source link

Where is authUser? #244

Closed ottz0 closed 4 years ago

ottz0 commented 4 years ago

It's like the chicken or the egg....Where does authUser and claims come from and how do I initialise it?

I'm expecting it on my parameters but where does it come from and how do I access it? Is it coming back as a callback from firebase? from $firestore? from auth? from $fireAuth? So therefore it's undefined. my res.locals.user is empty

Example (I'm assigning a Const to authUser) so it exists....but where is it?

const { uid, email, emailVerified, displayName, photoURL } = authUser

Example Action

onAuthStateChangedAction: (ctx, { authUser, claims }) => {
  if (!authUser) {
    // claims = null

    // Perform logout operations
  } else {
    // Do something with the authUser and the claims object...
  }
}

Example Mutation

ON_AUTH_STATE_CHANGED_MUTATION(state, { authUser, claims }) {
  // you can request additional fields if they are optional (e.g. photoURL)
  const { uid, email, emailVerified, displayName, photoURL } = authUser

  state.authUser = {
    uid,
    displayName,
    email,
    emailVerified,
    photoURL: photoURL || null, // results in photoURL being null for server auth
    // use custom claims to control access (see https://firebase.google.com/docs/auth/admin/custom-claims)
    isAdmin: claims.custom_claim
  }
}
ottz0 commented 4 years ago

Ok....I got it...Might just recommend just to update and make the docs clearer as I got so confused with SSR and local mode as my universal is server side and client side and I thought nuxtServerInit would run the action

I'm using Universal mode and using the pre-rendered. If you want to use the this version set the auth to

auth: {
            persistence: 'local',
            initialize: {
              onAuthStateChangedAction: 'firebaseStoreModule/actionInTheFirebaseStoreModule',
            },
        },
lupas commented 4 years ago

Hi @ottz0 Thanks for the input, glad it worked out in the end.

Yep, will make the docs clearer at some point. SSR is just experimental, we'll probably move that away to a seperate module anyway.