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

140
Visualizações
How do I keep a user logged in my website?

I am using Facebook API to log in a user. It logs in a user successfully.

But whenever I refresh the website or close it then reopen it logs out automatically. I don't need that I want a user logged in until he himself logs out of my website. How do I do it?

This is my code:

function statusChangeCallback(response) {


    if (response.status === 'connected') {
        setElements(true);
        console.log('Logged in');
        instaInfo();
    } else {
        setElements(false);
        console.log('not logged in');
    }
}

function checkLoginState() {
    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });
}

function setElements(isLoggedIn) {
    if (isLoggedIn) {
        document.querySelector('#fb-btn').style.display = 'none';
        document.querySelector('.logout').style.display = 'block';
        document.querySelector('.navigation').style.display = 'block';
        document.querySelector('.nav-main').style.visibility = 'visible';
        document.querySelector('#container').style.display = 'block';
    } else {
        document.querySelector('#fb-btn').style.display = 'block';
        document.querySelector('.logout').style.display = 'none';
        document.querySelector('.navigation').style.display = 'none';
        document.querySelector('.nav-main').style.visibility = 'hidden';
        document.querySelector('#container').style.display = 'none';
    }
}

document.querySelector('.logout').addEventListener('click', () => {
    FB.logout(function() {
        setElements(false);    
        console.log('Logged Out!');
    });
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

According to the facebook documentation:

While you can call FB.getLoginStatus any time (for example, when the user tries to take a social action), most social apps need to know the user's status as soon as possible after the page loads. In this case, rather than calling FB.getLoginStatus explicitly, it is possible to check the user's status by setting status: true when you call FB.init.

To receive the response of this call, you must subscribe to the auth.statusChange event. The response object passed by this event is identical to that which would be returned by calling FB.getLoginStatus explicitly.

https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

It might just be that you're running into a problem with the asynchronous nature of javascript in your code example. Have you checked what your call on getLoginStatus actually returns?

about 4 years ago · Juan Pablo Isaza Relatório

0

You can take advantage of the browser's localStorage (it' s a method of the window object) to keep the data even after reloading the page or even after closing the window.

Take a look here: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

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