Verify that the user is logged in

0

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

PHP function that checks whether it is 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');
                    }
        }
    })
};
    
asked by anonymous 11.09.2017 / 18:11

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');
                    }
        }
    })
});
    
11.09.2017 / 18:56