npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nuxt-flame

v1.0.0-beta.9

Published

🚀 Google Firebase integration for Nuxt

Downloads

13

Readme

Nuxt Flame logo

🔥 Nuxt Flame

npm version npm downloads License

Easily integrate Google Firebase into your Nuxt 3 application

🦾 Features

  • ⛰  Firebase Auth, Firestore, Realtime Database, Cloud Functions and Cloud Storage
  • 🧪  Emulators support
  •  TypeScript support
  • 🔋  SSR Friendly
  • 🔐  Safe
  • 🪶  Light

🏗️ Installation

  1. Add nuxt-flame dependency to your project
# Using pnpm
pnpm add -D nuxt-flame firebase firebase-admin

# Using yarn
yarn add --dev nuxt-flame firebase firebase-admin

# Using npm
npm install --save-dev nuxt-flame firebase firebase-admin
  1. Add nuxt-flame to the modules section of nuxt.config.ts and specify Firebase credentials in runtimeConfig
export default defineNuxtConfig({
  modules: [
    "nuxt-flame"
  ],

  runtimeConfig: {
    firebaseAdminCredentials: {
      projectId: process.env.FIREBASE_PROJECT_ID,
      clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
      privateKey: process.env.FIREBASE_PRIVATE_KEY,
    },

    public: {
      firebaseCredentials: {
        apiKey: process.env.FIREBASE_API_KEY,
        authDomain: process.env.FIREBASE_AUTH_DOMAIN,
        projectId: process.env.FIREBASE_PROJECT_ID,
        storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
        messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
        appId: process.env.FIREBASE_APP_ID,
        measurementId: process.env.FIREBASE_MEASUREMENT_ID,
      },
    },
  },
})
  1. Create .env file and put your credentials there
// Web credentials from code snippet provided by Google Firebase
FIREBASE_API_KEY=********
FIREBASE_AUTH_DOMAIN=********
FIREBASE_PROJECT_ID=********
FIREBASE_STORAGE_BUCKET=********
FIREBASE_MESSAGING_SENDER_ID=********
FIREBASE_APP_ID=********
FIREBASE_MEASUREMENT_ID=********

// Admin credentials from service account key (JSON)
FIREBASE_CLIENT_EMAIL=********
FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----
****************************************************
****************************************************
****************************************************
****************************************************
****************************************************
****************************************************
**************************
-----END PRIVATE KEY-----"
  1. (Optional) Feel free to configure Nuxt Flame in nuxt.config.ts
export default defineNuxtConfig({
  // ...

  /**
   * Default Nuxt Flame settings
   */
  flame: {
    authApiEndpoint: "/api/__session",
    authCookieName: "__session",

    emulators: {
      enabled: false,

      auth: {
        enabled: false,
        url: "http://127.0.0.1:9099",
        options: {
          disableWarnings: false,
        },
      },

      firestore: {
        enabled: false,
        host: "127.0.0.1",
        port: 8080,
      },

      database: {
        enabled: false,
        host: "127.0.0.1",
        port: 9000,
      },

      functions: {
        enabled: false,
        host: "127.0.0.1",
        port: 5001,
      },

      storage: {
        enabled: false,
        host: "127.0.0.1",
        port: 9199,
      },
    },
  },

  // ...
})

That's it! You can now use Nuxt Flame in your Nuxt app ✨

Usage

Nuxt Flame exports bunch of Firebase helpers (Vue composables) that available both client and server side.

Firebase Apps

const app = useFirebaseApp()        // client-side
const admin = useFirebaseAdminApp() // server-side

Firebase Auth

Basic usage:

// Firebase Auth instance (client only)
const auth = useAuth()

// Firebase Auth instance with admin credentials (server only)
const auth = useServerAuth()

// Get current user
// ❗️ Client returns `User` object when server returns `DecodedIdToken` object
const currentUser = useCurrentUser()

Authentication example using Google provider:

import { GoogleAuthProvider, signInWithPopup, signOut } from "@firebase/auth"
import { useAuth, useCurrentUser } from "#imports"

const auth = useAuth()
const currentUser = useCurrentUser()

const login = async () => {
  if (!auth) return

  await signInWithPopup(auth, new GoogleAuthProvider())
}

const logout = async () => {
  if (!auth) return

  await signOut(auth)
}

Firestore

Get Firebase Firestore instance:

const db = useFirestore()

Documents

// Get single document (SSR friendly)
const post = await useAsyncDocument("posts", "1")

// Get single document (async)
const { data, loading, error, refresh } = useDocument("posts", "1")

// Subscribe to document changes
const { data, loading, error, unsubscribe } = useDocumentSubscribe("posts", "1")

// Don't forget to unsubsribe
onUnmounted(() => unsubscribe())

Collections

// Get collection documents (SSR friendly)
const posts = await useAsyncCollection("posts")

// Get collection documents (async)
const { data, loading, error, refresh } = useCollection("posts")

// Subscribe to collection changes
const { data, loading, error, unsubscribe } = useCollectionSubscribe("posts")

// Don't forget to unsubsribe
onUnmounted(() => unsubscribe())

Collection Queries

import { where, orderBy } from "firebase/firestore"

// Get collection documents with query (SSR friendly)
const posts = await useAsyncCollection("posts", {
  conditions: [
    where("author", "==", "Andrew Kodkod"),
    orderBy("createdAt", "desc"),
  ],
})

// Get collection documents with query (async)
const { data, loading, error, refresh } = useCollection("posts", {
  conditions: [
    where("author", "==", "Andrew Kodkod"),
    orderBy("createdAt", "desc"),
  ],
})

Cloud Functions

Get Firebase Cloud Functions instance:

const functions = useFunctions()

Call https callable Cloud Function:

const archivePost = useFunction("archivePost")

const onArchive = async () => {
  const result = await archivePost.performAsync({ postId: "1", reason: "Spam" })
}

Realtime Database

Get Firebase Realtime Database instance:

const db = useDatabase()

Cloud Storage

Get Firebase Cloud Storage instance:

const storage = useStorage()

Enable Emulators

// nuxt.config.ts

export default defineNuxtConfig({
  // ...

  flame: {
    emulators: {
      enabled: process.env.NODE_ENV !== "production",
    }
  }

  // ...
})

🛣️ Roadmap

  • [ ] Tests
  • [ ] TypeScript examples
  • [ ] Advanced usage examples
  • [x] Helpers for Firestore
  • [x] Helpers for Functions
  • [ ] Helpers for Storage
  • [ ] Helpers for Realtime Database

👩‍💻 Development

# Install dependencies
pnpm install

# Generate type stubs
pnpm run dev:prepare

# Develop with the playground
pnpm run dev

# Build the playground
pnpm run dev:build

# Run ESLint
pnpm run lint

# Run Vitest
pnpm run test
pnpm run test:watch

# Release new version
pnpm run release