Skip to content

Instantly share code, notes, and snippets.

@deeja
Created July 13, 2020 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deeja/0af182c789449a9074166e8acc5c325b to your computer and use it in GitHub Desktop.
Save deeja/0af182c789449a9074166e8acc5c325b to your computer and use it in GitHub Desktop.
Nuxt Firebase "@nuxtjs/firebase" plug in alternative for vuex that returns the JWT token and sets in Vuex
// Using the "@nuxtjs/firebase" module
import {mutationNames} from '@/data/StoreNames'
export default async ({ store, app }, inject) => {
// Sets up a listener, mutations and action for every onAuthStateChanged by Firebase.
// AND runs the functions once BEFORE the root Vue.js Application is instantiated.
const unsubscribe = await new Promise(resolve => {
const unsubscribe = app.$fireAuth.onAuthStateChanged(async user => {
const credential = user ? (await user.getIdTokenResult(true)) : null
store.commit(mutationNames.ON_AUTH_STATE_CHANGED_MUTATION, { user, credential })
resolve(unsubscribe)
})
})
inject('fireAuthUnsubscribe', unsubscribe)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment