Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpException in Handler.php line 133: This action is unauthorized

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.

like image 576
Kvnamo Avatar asked Feb 13 '17 22:02

Kvnamo


3 Answers

Check that your AnnouncementRequest file is set to return true from authorize function. The default is to return false.

like image 79
user7830484 Avatar answered Oct 29 '22 09:10

user7830484


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;
    }
}
like image 4
Kaushik shrimali Avatar answered Oct 29 '22 07:10

Kaushik shrimali


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();
    }
like image 2
Rashi Goyal Avatar answered Oct 29 '22 09:10

Rashi Goyal