How to use Firebase authentication in Vue

Firebase authentication provides comprehensive user management for Vue applications with built-in support for multiple providers and enterprise-grade security. As the creator of CoreUI with extensive Vue development experience since 2014, I’ve integrated Firebase auth in numerous production applications for rapid authentication setup. The most efficient approach uses Firebase SDK v9 with Vue 3 Composition API for modern, tree-shakable authentication. This solution eliminates custom backend authentication complexity while providing robust security features and social login options.

Set up Firebase authentication with Vue 3 Composition API using the modular Firebase v9 SDK.

// firebase.js
import { initializeApp } from 'firebase/app'
import {
    getAuth,
    signInWithEmailAndPassword,
    createUserWithEmailAndPassword,
    signOut,
    onAuthStateChanged
} from 'firebase/auth'

const firebaseConfig = {
    apiKey: "your-api-key",
    authDomain: "your-project.firebaseapp.com",
    projectId: "your-project-id"
}

const app = initializeApp(firebaseConfig)
export const auth = getAuth(app)

// composables/useAuth.js
import { ref, onMounted } from 'vue'
import { auth } from '@/firebase'

export function useAuth() {
    const user = ref(null)
    const loading = ref(true)

    const login = async (email, password) => {
        try {
            const result = await signInWithEmailAndPassword(auth, email, password)
            user.value = result.user
            return result
        } catch (error) {
            throw error
        }
    }

    const signup = async (email, password) => {
        try {
            const result = await createUserWithEmailAndPassword(auth, email, password)
            user.value = result.user
            return result
        } catch (error) {
            throw error
        }
    }

    const logout = async () => {
        try {
            await signOut(auth)
            user.value = null
        } catch (error) {
            throw error
        }
    }

    onMounted(() => {
        onAuthStateChanged(auth, (authUser) => {
            user.value = authUser
            loading.value = false
        })
    })

    return {
        user,
        loading,
        login,
        signup,
        logout
    }
}

This code sets up Firebase authentication using the modern v9 SDK with a Vue 3 composable. The composable provides reactive user state, authentication methods, and automatic state synchronization through onAuthStateChanged. The modular approach ensures optimal bundle size while providing full authentication functionality.

Best Practice Note:

This is the Firebase authentication setup we recommend in CoreUI Vue applications for rapid deployment with enterprise-grade security. Firebase handles password security, email verification, and social providers automatically, making it ideal for both MVP and production applications.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author