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

firebase upgrade to version 9.*.* #1406

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions components/AuthContext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import firebase from '../lib/client'
import { getApps} from "firebase/app";
import { getAuth,onAuthStateChanged} from 'firebase/auth';
// IDEA: just read from firebase store at request time?
import { client } from '../lib/api'

Expand All @@ -13,9 +14,12 @@ function AuthContext(props) {
const [user, setState] = React.useState(null)

React.useEffect(() => {
if (firebase) {
firebase.auth().onAuthStateChanged(newUser => setState(newUser))

if(getApps().length > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as below.

{
onAuthStateChanged(getAuth() , newUser => setState(newUser))
}

}, [])

React.useEffect(() => {
Expand Down
5 changes: 3 additions & 2 deletions components/LoginButton.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import Link from 'next/link'
import firebase, { logout, loginGitHub } from '../lib/client'
import { getApps } from 'firebase/app'
import { logout, loginGitHub } from '../lib/client'

import Button from './Button'
import Popout, { managePopout } from './Popout'
Expand Down Expand Up @@ -48,7 +49,7 @@ function Drawer(props) {
function LoginButton({ isVisible, toggleVisibility }) {
const user = useAuth()

if (!firebase) {
if (getApps().length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make the other change suggested below, we don't need to make this change

return null
}

Expand Down
6 changes: 3 additions & 3 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import debounce from 'lodash.debounce'
import ms from 'ms'

import { fileToDataURL } from './util'
import firebase from './client'
import { DEFAULT_CODE } from './constants'
import { getAuth } from 'firebase/auth'

export const client = axios.create({
baseURL: `${process.env.NEXT_PUBLIC_API_URL || ''}/api`,
Expand Down Expand Up @@ -104,8 +104,8 @@ function getSnippet(uid = '', { host, filename } = {}) {

function listSnippets(page) {
// IDEA: move into axios interceptor
return firebase
.auth()

return getAuth()
.currentUser.getIdToken()
.then(authorization =>
client
Expand Down
54 changes: 32 additions & 22 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
import firebase from 'firebase/app'
import 'firebase/auth'

if (firebase.apps.length === 0) {
if (process.env.NEXT_PUBLIC_FIREBASE_API_KEY && process.env.NEXT_PUBLIC_FIREBASE_FE_APP_ID) {
firebase.initializeApp({
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: `${process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID}.firebaseapp.com`,
databaseURL: `https://${process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID}.firebaseio.com`,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: `${process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID}.appspot.com`,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_FE_APP_ID,
})
}
import { getApps, initializeApp } from "firebase/app";
import { getAuth, signOut , setPersistence , browserLocalPersistence , signInWithPopup , GithubAuthProvider , onAuthStateChanged} from 'firebase/auth';


const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: `${process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID}.firebaseapp.com`,
databaseURL: `https://${process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID}.firebaseio.com`,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: `${process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID}.appspot.com`,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_FE_APP_ID,
};

let FirebaseApp , auth

if(getApps().length === 0 )
{
if(typeof process.env.NEXT_PUBLIC_FIREBASE_API_KEY !== "undefined" && typeof process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID !== "undefined" )
{
FirebaseApp = initializeApp(firebaseConfig)
auth = getAuth(FirebaseApp)
}
}

export function logout() {
return firebase.auth().signOut().catch(console.error)
return signOut(auth).then(() => {
// Sign out successful
}).catch((error) => {
console.log(error)
});
}

export function login(provider) {
return firebase
.auth()
.setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(() => firebase.auth().signInWithPopup(provider))
return setPersistence(auth, browserLocalPersistence)
.then(() => signInWithPopup(auth, provider))
.catch(console.error)
}

export function loginGitHub() {
const provider = new firebase.auth.GithubAuthProvider()
const provider = new GithubAuthProvider()
provider.setCustomParameters({
allow_signup: 'true',
})
return login(provider)
}

export default firebase.apps.length ? firebase : null
export default FirebaseApp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we still make this:

getApps().length ? firebase : null

so that we don't need to call getApps() in other files?

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"eitherx": "^1.0.3",
"email-validator": "^2.0.4",
"escape-goat": "^4.0.0",
"firebase": "^8.9.1",
"firebase": "9.9.3",
"graphql": "^16.5.0",
"highlight.js": "^10.7.2",
"lodash.debounce": "^4.0.8",
Expand Down
Loading