Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
how to keep session enabled in firebase/auth js?

When I log in or register everything works perfectly. But if I reload the page I have to log in again. Is there any way around this? I have tried setting up persistence myself but it doesn't work. This is my code

const iniciarSesion = (e) => {
    const auth = getAuth();
    setPersistence(auth, browserLocalPersistence)
    .then(() => {
        signInWithEmailAndPassword(auth, email, password)
            .then((userCredential) => {
                setVentana("Tienda")
            })
            .catch((error) => {
                console.log(error)
            });
    })
}
const registar = (e) => {
    const auth = getAuth();
    createUserWithEmailAndPassword(auth, email, password)
        .then((userCredential) => {
            setVentana("Tienda")
        })
        .catch((error) => {
            console.error(error)
        });
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

In most browser-based environments Firebase already persists and restores the user credentials upon reloading the page. From the Firebase documentation on auth persistence:

For a web application, the default behavior is to persist a user's session even after the user closes the browser. This is convenient as the user is not required to continuously sign-in every time the web page is visited on the same device.

So if your code runs in a browser, you shouldn't need to call setPersistence(auth, browserLocalPersistence). What you do need however, it so detect in your code when Firebase has restored the signed in user.

Restoring the user upon reloading the page requires that Firebase call to the server, to amongst others check if the account has been disabled. While this call is being made, the currentUser will be null. To get notified when the user has been restored, and for other changes in authentication state, you'll want to use an auth state listener as shown in the first code snippet in the documentation on getting the current user:

import { getAuth, onAuthStateChanged } from "firebase/auth";

const auth = getAuth();
onAuthStateChanged(auth, (user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
    const uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});

In this code you'll then call setVentana("Tienda") to navigate to the correct window.

about 4 years ago · Juan Pablo Isaza Relatório

0

you can use redux-persist so if reload page , redux store will persist. this document my help you https://react-redux-firebase.com/docs/integrations/redux-persist.html

about 4 years ago · Juan Pablo Isaza Relatório

0

Implement redux w/ toolkit (easiest way to set up redux IMO) - you can follow this tutorial

Important tip is to update the state with the onAuthStateChanged method available in the firebase package, instead of changing it directly during the sign-in/ sign-up function

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda