Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

A Laravel form request answers 403 'This action is unauthorized'

Error403 This action is unauthorized.

Short answer

A form request runs its authorize() method before the rules. If it returns false, Laravel stops with a 403 and the controller never runs. Check what authorize() returns for this request. The usual causes are a hard-coded false, a policy that denies the user, or a guest user that gates and policies reject by default.

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the form request class named in the controller method signature and read its authorize() method. Then dump what it returns for this request.

    // app/Http/Requests/UpdatePostRequest.php
    public function authorize(): bool
    {
        dd($this->user(), $this->route('post'));
    }
  2. 2Match what you see, then jump to its fix

    1. 1The 403 appears on every request, for every user, and the request class has return false;authorize() simply returns falseFix
    2. 2authorize() calls $this->user()->can(...) and the 403 happens only for some users or some recordsA policy or gate denies this userFix
    3. 3403 on a route that is reachable without logging in, or on an API call that sent no valid session or tokenThe request comes from a guest, so $this->user() is nullFix

What does the error look like?

  • 403 This action is unauthorized.
  • Illuminate\Auth\Access\AuthorizationException: This action is unauthorized.
  • The controller method is never reached and no validation errors are shown

Cause 1 of 3

authorize() simply returns false

1Check

Look at the body of authorize(). A request class that was edited or copied can still carry a fixed false.

grep -n -A3 'function authorize' app/Http/Requests/*.php

2Fix

If the route is already protected by middleware or a policy elsewhere, return true. Otherwise return a real check.

public function authorize(): bool
{
    return true;
}

3Verify

Send the same request again. The 403 is gone and validation errors (422, or a redirect with errors) show instead.

php artisan test --filter=UpdatePostTest

Confirmed by laravel.com

Did this fix it?

Cause 2 of 3

A policy or gate denies this user

1Check

Run the same check in tinker with the same user and model. If it prints false, the policy method (or the record's owner) is the cause. Also confirm the policy is found: its name must be the model name plus Policy, inside a Policies directory at or above the models.

php artisan tinker
>>> $user = App\Models\User::find(1);
>>> $user->can('update', App\Models\Post::find(1));

2Fix

Correct the policy method, or register the policy by hand if the naming convention is not followed.

// app/Providers/AppServiceProvider.php, in boot()
Gate::policy(Post::class, PostPolicy::class);

3Verify

Repeat the tinker call; it now prints true for a user who should be allowed.

>>> $user->can('update', $post);

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

The request comes from a guest, so $this->user() is null

1Check

Gates and policies return false by default when the request was not made by an authenticated user. Check whether the route has the auth middleware and whether the client really sends its session or token.

php artisan route:list

2Fix

Put the route behind the auth middleware so the user exists. If guests really may use it, make the user argument of the policy method optional with ?User so the check reaches your code.

public function update(?User $user, Post $post): bool
{
    return $user?->id === $post->user_id;
}

3Verify

Call the route once signed in and once as a guest and compare the two responses.

php artisan test --filter=PostPolicyTest

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Write one feature test per form request: an allowed user gets 200 or 302, a denied user gets 403.
  • Keep authorization in policies and call them from authorize(), so the rule lives in one place.
  • Use Response::deny('message') in policies so the 403 explains itself.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.