I'm having problems creating middleware that checks to see if the user owns the requested resource.
For example, if a user navigates to /playlists/1/editand they don’t have their own playlist 1, they should display error 401.
Here is what I still have:
class CheckOwnership {
public function handle(Request $request, Closure $next)
{
if (Playlist::find($request->route()->parameters()['playlists'])->user_id !== $request->user()->id)
{
return response('Unauthorized.', 401);
}
return $next($request);
}
}
This is terrible and only works for the Playlist resource, but I cannot find a better way to do this.
thank
source
share