I have created an application using laravel 5.3 and it is working fine on localhost but after I uploded all my code on a server I have this error:
Symfony\Component\HttpKernel\Exception\HttpException in /home/project/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php line 133: This action is unauthorized.
This is happening when I try to call functions whithin my controllers using post.
This is one example:
Route
Route::group(['middleware' => 'auth'], function () {
Route::group(['middleware' => 'admin'], function () {
Route::post('admin/store/', 'Admin\AnnouncementController@store');
});
});
Controller
protected function store(AnnouncementRequest $request) {
return Auth::user()->id;
}
How can I fix this? Why is this not happening on my localhost?
Thanks in advance.
Check that your AnnouncementRequest
file is set to return true from authorize function. The default is to return false.
Well, for what I saw, there can be a lot of situations for this scenario. In my case, I was using a custom FormRequest named AnnouncementRequest. Inside that class I was checking for a role property on the auth user.
// before
public function authorize() {
if(Auth::user()->role_id === 1) {
return true;
}
return false;
}
My mistake was to use === instead == for validation. So after fixing that everything is working just fine.
// after
public function authorize() {
if(Auth::user()->role_id == 1) {
return true;
}
return false;
}
Anyway why did it worked on localhost but did not on the server remains a mystery for me...
The authorize() function by default it returns false,
return true, your issue will be resolved
In your Request file default not enable authorisation
public function authorize()
{
return false;
}
if you enable your request file here showing that code.
public function authorize()
{
return true;
}