Laravel – How to fire a js event when user logged in?

I want to fire my event when users log on. I use Laravel 5.5. I have some products in localStorage for guests, I want to send these products to their cart when they log on..

I’m trying with setInterval it works fine but I don’t want to run this code per x minute or seconds.. So what I need to do in here ? How can I fire event when the user logged in ?

setInterval(sendGuestProduct, 8000);
function sendGuestProduct() {
    for(let i=0; i<products.length;i++){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
        });
        $.ajax({
            type: 'POST',
            url: "/product/"+products[i]+"/user/"+$.cookie("userinformation"),
            success: function () {
                console.log('successful')
            }
        });
    }

}

Answer

use document function at end

@if(!Auth::guest())
  $(document).ready(function() {
  for(let i=0; i<products.length;i++){
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
    });
    $.ajax({
        type: 'POST',
        url: "/product/"+products[i]+"/user/"+$.cookie("userinformation"),
        success: function () {
            console.log('successful')
        }
    });
   }
  });   
@endif

hope this will help

Leave a Reply

Your email address will not be published. Required fields are marked *