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

Discussion (1)

Collapse
whiterid profile image
Clément Gaal

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