Skip to content

Commit

Permalink
Replace outdated react-firebaseui package (#694)
Browse files Browse the repository at this point in the history
* Replace outdated react-firebaseui package

* Fix lint errors

* Add one more space

* Add back firebaseui

* Fix all eslint errors

* Remove package and yarn lock file

* Fix remaining eslint errors

* Add new eslint rule
  • Loading branch information
MvRemmerden authored Jan 23, 2024
1 parent 4556701 commit 6f5b0c9
Show file tree
Hide file tree
Showing 5 changed files with 519 additions and 475 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module.exports = {
tsx: 'never',
},
],
'react/require-default-props': 'off',
},
overrides: [
// Set Jest rules only for test files.
Expand Down
2 changes: 1 addition & 1 deletion example/components/FirebaseAuth.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* globals window */
import React, { useEffect, useState } from 'react'
import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth'
import { getApp } from 'firebase/app'
import { getAuth, EmailAuthProvider } from 'firebase/auth'
import StyledFirebaseAuth from './StyledFirebaseAuth'

// Note that next-firebase-auth inits Firebase for us,
// so we don't need to.
Expand Down
72 changes: 72 additions & 0 deletions example/components/StyledFirebaseAuth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React, { useEffect, useRef, useState } from 'react'
import { Auth, onAuthStateChanged } from 'firebase/auth'
import 'firebaseui/dist/firebaseui.css'
import { auth } from 'firebaseui'

interface Props {
// The Firebase UI Web UI Config object.
// See: https://github.com/firebase/firebaseui-web#configuration
uiConfig: auth.Config
// Callback that will be passed the FirebaseUi instance before it is
// started. This allows access to certain configuration options such as
// disableAutoSignIn().
uiCallback?(ui: auth.AuthUI): void
// The Firebase App auth instance to use.
firebaseAuth: Auth // As firebaseui-web
className?: string
}

const StyledFirebaseAuth = ({
uiConfig,
firebaseAuth,
className,
uiCallback,
}: Props) => {
const [firebaseui, setFirebaseui] = useState<
typeof import('firebaseui') | null
>(null)
const [userSignedIn, setUserSignedIn] = useState(false)
const elementRef = useRef(null)

useEffect(() => {
// Firebase UI only works on the Client. So we're loading the package only after
// the component has mounted, so that this works when doing server-side rendering.
async function setUI() {
setFirebaseui(await import('firebaseui'))
}

setUI()
}, [])

useEffect(() => {
if (firebaseui === null) return () => {}

// Get or Create a firebaseUI instance.
const firebaseUiWidget =
firebaseui.auth.AuthUI.getInstance() ||
new firebaseui.auth.AuthUI(firebaseAuth)
if (uiConfig.signInFlow === 'popup') firebaseUiWidget.reset()

// We track the auth state to reset firebaseUi if the user signs out.
const unregisterAuthObserver = onAuthStateChanged(firebaseAuth, (user) => {
if (!user && userSignedIn) firebaseUiWidget.reset()
setUserSignedIn(!!user)
})

// Trigger the callback if any was set.
if (uiCallback) uiCallback(firebaseUiWidget)

// Render the firebaseUi Widget.
firebaseUiWidget.start(elementRef.current!, uiConfig)

return () => {
unregisterAuthObserver()
firebaseUiWidget.reset()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [firebaseui, uiConfig])

return <div className={className} ref={elementRef} />
}

export default StyledFirebaseAuth
6 changes: 3 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"dependencies": {
"firebase": "9.17.1",
"firebase-admin": "^11.9.0",
"firebaseui": "^6.1.0",
"next": "13.4.9",
"next-absolute-url": "^1.2.2",
"next-firebase-auth": "1.0.2",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-firebaseui": "^6.0.0"
"react-dom": "18.2.0"
},
"devDependencies": {
"@next/bundle-analyzer": "^13.4.9",
Expand All @@ -25,4 +25,4 @@
"eslint-config-next": "^13.4.9",
"typescript": "^5.1.6"
}
}
}
Loading

0 comments on commit 6f5b0c9

Please sign in to comment.