masanos note

📝Firebase9 Google Authentication with Nuxt3.

2022-11-14
icon

Install

npm install firebase

package.json

{
  "private": true,
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "nuxt": "3.0.0-rc.13"
  },
  "dependencies": {
    "firebase": "^9.13.0"
  }
}

nuxt.config.json

export default defineNuxtConfig({
  ssr: true,
  runtimeConfig: {
    public: {
      FIREBASE_API_KEY: process.env.FIREBASE_API_KEY || "",
      FIREBASE_AUTH_DOMAIN: process.env.FIREBASE_AUTH_DOMAIN || "",
      FIREBASE_PROJECT_ID: process.env.FIREBASE_PROJECT_ID || "",
      FIREBASE_STORAGE_BUCKET: process.env.FIREBASE_STORAGE_BUCKET || "",
      FIREBASE_MESSAGING_SENDER_ID: process.env.FIREBASE_MESSAGING_SENDER_ID || "",
      FIREBASE_APP_ID: process.env.FIREBASE_APP_ID || "",
      FIREBASE_MEASUREMENT_ID: process.env.FIREBASE_MEASUREMENT_ID || "",
    },
  }
})

.env

FIREBASE_API_KEY=*
FIREBASE_AUTH_DOMAIN=*.firebaseapp.com
FIREBASE_PROJECT_ID=*
FIREBASE_STORAGE_BUCKET=*.appspot.com
FIREBASE_MESSAGING_SENDER_ID=*
FIREBASE_APP_ID=*
FIREBASE_MEASUREMENT_ID=*

plugins/firebase.client.ts

import { initializeApp } from 'firebase/app'


export default defineNuxtPlugin((nuxtApp) => {
  const config = useRuntimeConfig()
  const firebaseConfig = {
    apiKey: config.FIREBASE_API_KEY,
    authDomain: config.FIREBASE_AUTH_DOMAIN,
    projectId: config.FIREBASE_PROJECT_ID,
    storageBucket: config.FIREBASE_STORAGE_BUCKET,
    messagingSenderId: config.FIREBASE_MESSAGING_SENDER_ID,
    appId: config.FIREBASE_APP_ID,
    measurementId: config.FIREBASE_MEASUREMENT_ID,
  }
  initializeApp(firebaseConfig)
  const { auth, user } = useAuth()


  nuxtApp.vueApp.provide('auth', auth)
  nuxtApp.vueApp.provide('user', user)
})

composables/useAuth.ts

import type { User } from "firebase/auth";
import {
  getAuth,
} from "firebase/auth";


export function useAuth() {
  const auth = getAuth()
  const user = ref<User | null>(auth.currentUser);
  auth.onIdTokenChanged((authUser) => (user.value = authUser));


  return { auth, user };
}

pages/login.vue

<script setup>
import {
  signOut,
  signInWithPopup,
  GoogleAuthProvider
} from "firebase/auth";

const auth = inject('auth')
const user = inject('user')

async function login() {
  try {
    const provider = new GoogleAuthProvider();
    await signInWithPopup(auth, provider);
  } catch (error) {
    throw error;
  }
}

async function logout() {
  try {
    await signOut(auth);
    user.value = null;
    location.reload()
  } catch (error) {
    throw error;
  }
}
</script> 


<template>
  <button v-if="!user" @click="login()">
    Google Login user: {{ user }}
  </button>
  <button v-else @click="logout()">
    Google Logout {{ user }}
  </button>
</template>
Related Notessupabase with Vue3 Vue3 with bootstrap-icons[vue3]Install bootstrap5[vue3] install[Nuxt3]The first thing I do when launching a nuxt3 project. (^3.5.2)[Nuxt3][Bootstrap]Use Bootstrap icons with Nuxt3.[Node.js] Storing API results in js🔧[GA4][GTM]Configure GA4 in GTM🔧[Nuxt3]Using Google Tag manager with NUXT3🔧[GA4][BigQuery]Linking GA4 and BigQuery🔧When you want to scrape a SPA site, PhantomJsCloud is solution.🐛Error brew -v | update-reset🔧Use Google Spreadsheet as API with Nuxt3.🔧 Get json from a spreadsheet using GoogleSheetsAPI v4.🔧[Python]Install Python to Mac[Nuxt3]Install stable version of Nuxt 3.0.0. | npx nuxi init nuxt3-appMake Ranking with MySQLwatch & v-model | Vue3 (Nuxt3)window & document | Nuxt3Using custom domain, Hosting to GitHub Pages with Nuxt3GA4 with Nuxt3📝MySQL - Date Function - Tips Use Nuxt3 props🔧Use MicroCMS with Nuxt3🔧Using GoogleFont with Nuxt3📝Error - Deprecation Warning: $weight: Passing a number without unit % (100) is deprecated. - Bootstrap5 📝using sass with nuxt📝Set favicon in Nuxt3📝Use bootstrap5 with Nuxt3🐛Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement[Nuxt3] How to separate source directoriesmicroCMS & GitHub Actions & Nuxt3Using highlight in Nuxt3.use package.json value🔧frontmatter-markdown-loader & highlight.js🔧Install Font Awesome on Nuxt2 via npm.Github pages with GitHub ActionsCannot find module '~/*/*.vue' or its corresponding type declarations.Vetur(2307)🐛Cannot find module. Consider using '--resolveJsonModule' to import module with '.json' extension.ts(2732)TypeScript Object.🔧Bootstrap5 with Nuxt2processmd with Nuxt2🔧[MySQL]Install MySQL Workbench🔧Convert Markdown to HTML. convert frontmatter to json🔧Install homebrew, nvm, node to Mac🔧[MySQL]Record of installing and starting mysql with homebrew.🎨 Display the photo full screen and overlay the header and footer on top.🔧Set git repository to created project.[Nuxt3] Make Header & Footer
A record of the development is left in a web note.
Masanos
I want to make the world I see happy. Little by little, I am preparing to start a business. Thank you for your support.
Buy Me A Coffee
Copyright© masanos All Rights Reserved.