Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bigdogdman1's avatar

Error: Auth guard [inauth] is not defined.

I am trying to integrate a custom auth/user/service provider & guard called InAuth. I have made significant progress, but now I am stuck on this error (which I'm sure is just some stupid config error I'm not seeing) trying to extend/attach user data to the default global Auth object, but I've tried so many different things (indicated by some of the commented-out code) I have no idea where else to look. Almost all the issues I've found in this forum that have gotten me this far are from 5+ years ago; is there something more Laravel 11.5-compatible that I can implement for this? Or is there simply a much easier way to implement a custom auth method (that uses NO database to store data)? I have looked into JWT but it doesn't solve my inherent problem of being in full control of the authorization operations & storing the user data in the session & retrieving the user data from an external API endpoint when necessary. And before you get stuck in, I am fully aware of the security issues this presents, and will take care of them in due time. Thank you for any help you can provide me. :-)

Here is my AppServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Auth;
use App\Providers\InAuthUserProvider;
use App\Guards\InAuthGuard;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        Auth::provider('inauth', function (Application $app) {
             //return new InAuthServiceProvider($app);
             return new InAuthUserProvider(Request());
        });

        $this->app->extend('inauth', function ($existingGuard, $app) {
            return new InAuthGuard($app->make(InAuthUserProvider::class));
        });
        
        $this->app->singleton(InAuthUserProvider::class, function (Application $app) {
            return new InAuthUserProvider(Request());
        });
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Auth::extend('inauth', function (Application $app, array $config) {    // InAuthUserProvider::class ??
            return new InAuthGuard($app->make(InAuthUserProvider::class)); //, Auth::GenericUser($config['provider'])
        });

        /* Auth::extend('auth', function (Application $app, string $name, array $config) {
            return new InAuthServiceProvider($app);  //, Auth::GenericUser($config['provider'])
        }); */

        Blade::if('inauth', function () {
		    /** 
This is the line that is throwing the error. Removing it does not allow user data/login state to persist, but gets rid of the error 
            **/
            return Auth::guard('inauth')->check();
        });
    }
}

Here is my auth.php:

<?php
return [
    'defaults' => [
        'guard' => env('AUTH_GUARD', 'web'),
        'passwords' => env('AUTH_PASSWORD_BROKER', 'users'),
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'inauth',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'inauth',
            'hash' => false,
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'inauth',
            //'model' => env('AUTH_MODEL', App\Models\User::class),
        ],

        'inauth' => [
            'driver' => 'inauth',
        ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'inauth',
            //'table' => env('AUTH_PASSWORD_RESET_TOKEN_TABLE', 'password_reset_tokens'),
            //'expire' => 60,
            //'throttle' => 60,
        ],
    ],

    'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800),

];

Here is my InAuthServiceProvider:

<?php

namespace App\Providers;

use \Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Illuminate\Contracts\Foundation\Application;
use \Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\ServiceProvider;
use App\Providers\InAuthUserProvider;

use App\Guards\InAuthGuard;

class InAuthServiceProvider extends ServiceProvider
{
    //public $user;

    protected $request;
    protected $app;
    
    public function __construct(Application $app) {   //, Authenticatable $user = new \Illuminate\Auth\GenericUser([])
        $this->request = Request();
        $this->app = $app;
        //$this->user = $user;
    }

    public function register() {
        $this->app->singleton(InAuthUserProvider::class, function ($app) {
            return new InAuthUserProvider($this->request);
        });
    
        $this->app->extend('auth.guard.inauth', function ($existingGuard, $app) {
            return new InAuthGuard($this->app->make(InAuthUserProvider::class));
        });
    }
}

Apologies, I don't know what else you need to see to diagnose this. Please let me know and I will include it.

0 likes
2 replies
bigdogdman1's avatar

Possible solution to this problem: Today I just noticed I was missing the inauth guard deifinition in my auth.php file:

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'inauth',
        ],

        'inauth' => [
            'driver' => 'token',
            'provider' => 'inauth',
            'hash' => false,
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'inauth',
            'hash' => false,
        ],
    ],

After that, I stopped getting the error, but my entire login system broke. I then changed the default guard to inauth, but this had no effect. Now I'm thinking there's a more inherent problem in my inauth files somewhere, as Auth::guard('inauth')->user() is still not being populated, but I have no idea where to start looking. Does anyone have any ideas?

Thanks in advance. :-)

bigdogdman1's avatar

I ultimately solved this by extending auth in my AppServiceProvider's boot (and a custom middleware auth check):

        Auth::extend('auth', function () {
            return new InAuthGuard(App()->make(InAuthUserProvider::class));
        });

I have no idea if this is the best way, but it seemed to do the trick (well enough). I am still encountering problems here and there, and I have no idea if they have to do with the authentication override or not, but if anyone has any better ways to do this that I can try, I'm certainly open to trying them! :-)

Please or to participate in this conversation.