Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update firebase in with-firebase #29581

Merged
merged 3 commits into from Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions examples/with-firebase/context/userContext.js
@@ -1,5 +1,6 @@
import { useState, useEffect, createContext, useContext } from 'react'
import firebase from '../firebase/clientApp'
import { createFirebaseApp } from '../firebase/clientApp'
import { getAuth, onAuthStateChanged } from 'firebase/auth'

export const UserContext = createContext()

Expand All @@ -9,7 +10,9 @@ export default function UserContextComp({ children }) {

useEffect(() => {
// Listen authenticated user
const unsubscriber = firebase.auth().onAuthStateChanged(async (user) => {
const app = createFirebaseApp()
const auth = getAuth(app)
const unsubscriber = onAuthStateChanged(auth, async (user) => {
try {
if (user) {
// User is signed in.
Expand Down
48 changes: 22 additions & 26 deletions examples/with-firebase/firebase/clientApp.js
@@ -1,30 +1,26 @@
import firebase from 'firebase/app'
import 'firebase/auth' // If you need it
import 'firebase/firestore' // If you need it
import 'firebase/storage' // If you need it
import 'firebase/analytics' // If you need it
import 'firebase/performance' // If you need it

const clientCredentials = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
}
import { initializeApp, getApps } from 'firebase/app'
import { getAnalytics } from 'firebase/analytics'
export const createFirebaseApp = () => {
const clientCredentials = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
}

if (!firebase.apps.length) {
firebase.initializeApp(clientCredentials)
// Check that `window` is in scope for the analytics module!
if (typeof window !== 'undefined') {
// Enable analytics. https://firebase.google.com/docs/analytics/get-started
if ('measurementId' in clientCredentials) {
firebase.analytics()
firebase.performance()
if (getApps().length <= 0) {
const app = initializeApp(clientCredentials)
// Check that `window` is in scope for the analytics module!
if (typeof window !== 'undefined') {
// Enable analytics. https://firebase.google.com/docs/analytics/get-started
if ('measurementId' in clientCredentials) {
getAnalytics()
}
}
return app
}
}

export default firebase
4 changes: 2 additions & 2 deletions examples/with-firebase/package.json
Expand Up @@ -6,8 +6,8 @@
"start": "next start"
},
"dependencies": {
"firebase": "7.17.0",
"firebase-admin": "9.0.0",
"firebase": "9.1.1",
"firebase-admin": "9.12.0",
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
Expand Down
10 changes: 6 additions & 4 deletions examples/with-firebase/pages/index.js
@@ -1,8 +1,8 @@
import { getFirestore, setDoc, doc } from 'firebase/firestore'
import Head from 'next/head'
import Link from 'next/link'
import { useEffect } from 'react'
import { useUser } from '../context/userContext'
import firebase from '../firebase/clientApp'

export default function Home() {
// Our custom hook to get context values
Expand All @@ -16,12 +16,14 @@ export default function Home() {
console.log(user)
}
// You also have your firebase app initialized
console.log(firebase)
}, [loadingUser, user])

const createUser = async () => {
const db = firebase.firestore()
await db.collection('profile').doc(profile.username).set(profile)
const db = getFirestore()
await setDoc(doc(db, 'profile', profile.username), {
profile,
})

alert('User created!!')
}

Expand Down