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.