Kvnamo
Kvnamo

Reputation: 157

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.

Upvotes: 11

Views: 34878

Answers (6)

Rashi Goyal
Rashi Goyal

Reputation: 941

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();
    }

Upvotes: 2

Kaushik shrimali
Kaushik shrimali

Reputation: 1228

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;
    }
}

Upvotes: 5

Kumaravel K
Kumaravel K

Reputation: 1

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;
    }

Upvotes: 0

Whisky Dollar
Whisky Dollar

Reputation: 11

The authorize() function by default it returns false, return true, your issue will be resolved

Upvotes: 0

user7830484
user7830484

Reputation: 571

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

Upvotes: 57

Kvnamo
Kvnamo

Reputation: 157

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...

Upvotes: 1

Related Questions