I would like images on html to be downloaded only for authenticated users. So, for example, I have written the following code.
[Client-side]
<img src="/image/showWorkImg?fileName=ed296da987c8ab75c42dce07e.jpg" alt="undefined" style="float:left;height: auto;width: auto"/>
[ImageController]
class ImageController extends Controller
{
public function showWorkImg(Request $request)
{
try {
$fileFullName = config('app.image_path') . '/' . $request->fileName;
return \Image::make($fileFullName)->response();
} catch (\Exception $e) {
return "";
}
}
}
And definitely, an authentication process is on the Middleware between the Client-side and ImageController. However, as you know, the image html tag is not able to send a request with header-based tokens. It sends a request only with cookies. So I have decided to synchronize a header auth token with a cookie auth token like this.
[Client-side]
localStorage.setItem(keyName, response.data.token);
cookies.set(keyName, response.data.token, {path: '/'});
I wonder if there are no problems with that sort of strategy.