DEV Community

trungpv
trungpv

Posted on

Laravel Filament: How to fix 403 error on production environment

After I installed the Laravel Filament on my local env and very impress because It’s to easy to setup, nice UI and document.

And

then I push it on production server. The 403 page show 😄

I have no idea about it and search google. So, we miss one step which is not mention in the v2 document.

So, We need implements Filament\Models\Contracts\FilamentUser in the app/Models/User.php model and add method canAccessFilament.

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Filament\Models\Contracts\FilamentUser;

class User extends Authenticatable implements FilamentUser
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function canAccessFilament(): bool
    {
        return str_ends_with($this->email, '@xxx.com');
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
hacheraw profile image
Hache_raw • Edited

Thanks!!

With Filament 3 this is the needed function:

public function canAccessPanel(Panel $panel): bool
Enter fullscreen mode Exit fullscreen mode

Don't forget to add this to the top:

use Filament\Panel;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
javierparrait profile image
javierparrait

Thank you! I had missed seeing that in the documentation. I wasted several days and headaches trying to solve it.

Collapse
 
ruby232 profile image
Rubisel Prieto Dupeyrón

Thank you very much, it saved me after several hours of errors.

Collapse
 
whiterid profile image
Clément Gaal

Just create account to share some love. Ty a lot. Wasted 3 hours.

Collapse
 
logudotcom profile image
Loganathan Natarajan

Thank you a ton! for me saved hours of time