Search code examples
laravellaravel-nova

403 forbidden for nova admin on production site with forge


I have a new project up with forge. I changed my admin column in the user table, but I get 403 forbidden when trying to log in. Why do I get this when I didn't on the devsite? Do I have to install nova in the forge command? or is there some files I have forgotten to configurate?


Solution

  • In local application environments, any user can access Laravel Nova.

    However, when accessing non-local application environments, you have to explicitly authorize users to access Laravel Nova. This is all described in the Installation section of the Laravel Nova documentation.

    In your app/Providers/NovaServiceProvider.php file, there is a gate method. You can simply add your email to the in_array function there. Or alternatively, you can create a method like isAdmin() on user, to determine if the user is an admin. You would have to create additional logic for that (either add a is_admin boolean column to your User model, or full Role-Based Access Control).

    NovaServiceProvider.php

    /**
     * Register the Nova gate.
     *
     * This gate determines who can access Nova in non-local environments.
     *
     * @return void
     */
    protected function gate()
    {
        Gate::define('viewNova', function ($user) {
            return in_array($user->email, [
                // add your email here
            ]);
        });
    
        // or alternatively
    
        Gate::define('viewNova', function ($user) {
            return $user->isAdmin();
        });
    }