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.
If you can use CustomRequest method for validation then make sure to your authorize() return true. If you can set false then its never call your function as well throw the error This action is unauthorized
Solution
class CopyRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true; //Default false .Now set return true;
}
}
Default function return false so change it as shown below
public function authorize()
{
return true;
}
or also can use Auth in the Request
use Illuminate\Support\Facades\Auth;
public function authorize()
{
return Auth::check();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With