Check if the user is logged in

Asked

Viewed 1,060 times

0

I would like to do a check to know if the user is logged in, I am trying to pass this on to an ajax request but am not succeeding.

PHP function that checks if you are logged in

public function verificalogin(){

        umask(0);

        Mage::app('default');

        Mage::getSingleton('core/session', array('name' => 'frontend'));

        $sessionCustomer = Mage::getSingleton("customer/session");

        if($sessionCustomer->isLoggedIn()) {
          echo json_encode('retorno' => true);
        } else {
          echo json_encode('retorno' => false);
        }
    }

Ajax

    function verificalogin(data){
        $j.ajax({ 
                url: 'verificalogin.php', 
                type: 'POST',
                data:{"retorno" : retorno}, 
                    success: function(data){
                    data = $j.parseJSON(data);
                    if (data == true ) {
                        console.log('Logado');
                    } else {
                        console.log('Deslogado');
                    }
        }
    })
};
  • In the file 'verificalogin.php' there is only the excerpt shown here?

  • 1

    Yes, I managed to tidy up now, I will post the code as an answer to anyone who has the same question

1 answer

0


Controller

    public function verificaloginAction()
    {
    if(Mage::getSingleton('customer/session')->isLoggedIn()){
        echo json_encode(array('retorno' => true)); 
    }else{
        echo json_encode(array('retorno' => false)); 
        }
    }
}

Ajax

    $j(document).ready(function() {
        $j.ajax({ 
                url: '<?php echo Mage::getUrl('contacts/login/verificalogin') ?>', 
                type: 'POST',
                data: 'retorno',
                dataType: 'json', 
                    success: function(data){
                    if (data['retorno'] == true ) {
                        console.log('Logado');
                    } else {
                        console.log('Deslogado');
                    }
        }
    })
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.