1 year ago

#376489

test-img

Semih R. Gürel

Firebase Auth not working properly with React 18

My environment

  • Operating System version: macOS Monterey 12.3.1
  • Browser version: Chrome 100.0.4896.60
  • Firebase SDK version: 9.6.10
  • Firebase Product: auth

Relevant Code:

// src/index.js
import React from 'react';
import { createRoot } from 'react-dom/client';
import { BrowserRouter as Router } from 'react-router-dom';
import './styles/styles.scss';
import App from './App/App';
import reportWebVitals from './reportWebVitals';
import { ThemeContextProvider } from './contexts/themeContext';
import './i18n';
import { AppContextProvider } from './contexts/appContext';
import { AuthContextProvider } from './contexts/authContext';

const container = document.getElementById('root');
const root = createRoot(container);
root.render(
    <Router>
        <React.StrictMode>
            <AppContextProvider>
                <ThemeContextProvider>
                    <AuthContextProvider>
                        <App />
                    </AuthContextProvider>
                </ThemeContextProvider>
            </AppContextProvider>
        </React.StrictMode>
    </Router>,
);

reportWebVitals();
import {
    initializeAuth,
    GoogleAuthProvider,
    browserLocalPersistence,
    indexedDBLocalPersistence,
    browserSessionPersistence,
    browserPopupRedirectResolver,
} from 'firebase/auth';
import app from './firebaseApp';

const auth = initializeAuth(app, {
    persistence: [indexedDBLocalPersistence, browserLocalPersistence, browserSessionPersistence],
    popupRedirectResolver: browserPopupRedirectResolver,
});
const googleProvider = new GoogleAuthProvider().setCustomParameters({
    prompt: 'select_account',
});

const actionCodeSettings = {
    url: process.env.REACT_APP_URL,
};

export { googleProvider, actionCodeSettings };
export default auth;

There were no problems when using React 17. Upgrading to React 18, every time we open the page, the user acts as if he is not logged in. Refreshing the page logs out directly.

firebase

firebase-authentication

react-dom

react-18

0 Answers

Your Answer

Accepted video resources