prgrm
prgrm

Reputation: 3833

Middleware for public storage in Laravel

I have this structure: laravel/storage/app/public/images/id_of_user/

Where id of user is the id of the user.

This is my middleware:

public function handle($request, Closure $next)
 {
    if($request->user_id == Auth::user()->id{

    return $next($request);
    }
return back();
}

and made a route like this:

Route::group(['middleware' => 'storagemiddleware'], function () {

Route::get('storage/images/{$user_id});

});

This doesn't work, tho. Everyone can still acess to everyones images. I honestly didn't expect this to work since it seems that storage has its own special routes and settings.

What I want is that only the users are allowed to see the content of their folder in their storage.

Any suggestions?

Upvotes: 0

Views: 4224

Answers (1)

user320487
user320487

Reputation:

Read through this section of the documentation: https://laravel.com/docs/5.4/filesystem.

If the idea is to keep files accessible only to those you own them, you'll want to put them somewhere in the storage directory and then access them through a controller. Anything in your public directory is just that, public.

You could setup a directory structure like this storage/user_data/<$user_id>/ to keep things separated, then retrieve them from your route and the specified parameter.

Upvotes: 2

Related Questions