Search code examples
ajaxlaravelauthorizationdymo

Laravel `Auth` not compatible with 3rd party library ajax call?


I have a Laravel Controller that returns an image when the user is logged in:

public function show($file)
{

   $path = storage_path('app/files/' . basename($file));

   if(!\Auth::check() || !\File::exists($path) return back();

   return response()->file($path);
}

When I enter my-domain.com/show/image.png in my browser, the image is shown when I am logged in.

When I am logged in and use a simple jQuery ajax call, it also works:

$.get('https://my-domain.com/show/image.png', '', function(data){
   console.log('success');
   console.log(data);
}

However, when I am logged it and use an ajax call through the DYMO JS Framework 3.0 (Docs) it suddenly does not work anymore.

To be more precise, when I call

 dymo.label.framework.loadImageAsPngBase64('https://my-domain.com/show/image.png');

I get an error, that no image is returned. When I remove !\Auth::check() in the controller, then it works.

Why is Auth not working with a 3rd party library ajax call? Is there anything I can do to fix it?


Solution

  • The issue is likely that the dymo framework isn't sending the session cookie as part of the request, and as such Laravel can't determine the logged in user.

    If the dymo framework doesn't support adding additional headers or data to the request (which it doesn't look like it does) you'll need to load the image elsewhere in your javascript.

    For example, you could use axios or another HTTP client to fetch the image, and then base64 encode it and pass it to dymo.label.framework.loadImageAsPngBase64()