Guest User

Laravel CustomAuth Boilerplate?!

a guest
Oct 11th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.40 KB | Source Code | 0 0
  1. Ok, one more parent thread reply:
  2. I think I got it!
  3. I'm posting the full skeliton in case others are stumbling on this...
  4.  App\Providers\MyUserProvider.php:
  5. namespace App\Providers;
  6.  
  7.  
  8. use Closure;
  9. use Illuminate\Contracts\Auth\Authenticatable as UserContract;
  10. use Illuminate\Contracts\Auth\UserProvider;
  11. use Illuminate\Contracts\Hashing\Hasher as HasherContract; //Currently not used
  12. use Illuminate\Contracts\Support\Arrayable;
  13. use Illuminate\Database\ConnectionInterface; //Currently not used. Likely will be yanked.
  14. use Illuminate\Auth\GenericUser;
  15.  
  16. class MyUserProvider implements UserProvider
  17. {
  18.  
  19.     //Configuration (WILL BE?) stored in config/MyApp.php
  20.     private $tenantShortname;
  21.     private $apiUser;
  22.     private $apiToken;
  23.     private $apiKey;
  24.     private $apiHost;
  25.  
  26.  
  27.  
  28.  
  29.     public function __construct()
  30.     {
  31.         //Lets get the config values
  32.         $this->tenantShortname = config('MyApp.tenantShortname');
  33.         $this->apiUser = config('MyApp.apiuser');
  34.         $this->apiToken = config('MyApp.apitoken');
  35.         $this->apiKey = config('MyApp.apikey');
  36.         $this->apiHost = config('MyApp.host');
  37.  
  38.  
  39.         $fh = fopen('/tmp/MyApp.log', 'a');
  40.         fwrite($fh, "MyAppUserProvider initialized in auth\n");
  41.         fclose($fh);
  42.  
  43.  
  44.         //Database connection not needed, so we'll comment these out for now.
  45.         // $this->connection = $connection;
  46.         // $this->table = $table;
  47.     }
  48.  
  49. Next up:  App\ProvidersMyServiceProvider.php
  50. namespace App\Providers;
  51.  
  52. use App\Providers\MyAppUserProvider;
  53. use Illuminate\Contracts\Foundation\Application;
  54. use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
  55. use Illuminate\Support\Facades\Auth;
  56.  
  57. class MyServiceProvider extends ServiceProvider
  58. {
  59.     public function boot()
  60.     {
  61.         $this->registerPolicies();
  62.         // Register the custom user provider
  63.         $fh=fopen('/tmp/MyApp.log', 'a');
  64.         fwrite($fh, "In MyServiceProvider::boot()\n");
  65.         fclose($fh);
  66.  
  67.  
  68.         Auth::provider('MyAppServiceProvider', function (Application $app, array $config) {
  69.             $config = $app->make('config');
  70.             // I will make sure it's accepting the configuration from injection.
  71.             return new MyUserProvider ();
  72.         });
  73.     }
  74. }
  75.  
  76.  
  77. App\config\auth.php:
  78.  
  79. return [
  80.  
  81.     'defaults' => [
  82.         'guard' => env('AUTH_GUARD', 'web'),
  83.         'passwords' => env('AUTH_PASSWORD_BROKER', 'users'),
  84.     ],
  85.  
  86.     'guards' => [
  87.         'web' => [
  88.             'driver' => 'session',
  89.             'provider' => 'users',
  90.         ],
  91.           'myappapi' => [
  92.             'driver' => 'session',
  93.             'provider' => 'myAppServiceProvider',
  94.         ],
  95.     ],
  96.  
  97.     'providers' => [
  98.         'users' => [
  99.             'driver' => 'eloquent',
  100.             'model' => env('AUTH_MODEL', App\Models\User::class),
  101.         ],
  102.         'myAppServiceProvider' => [
  103.             'driver' => 'myAppServiceProvider',
  104.             'model' => App\Providers\myAppUserProvider::class,
  105.         ],
  106.     ],
  107.  
  108.     'passwords' => [
  109.         'users' => [
  110.             'provider' => 'users',
  111.             'table' => env('AUTH_PASSWORD_RESET_TOKEN_TABLE', 'password_reset_tokens'),
  112.             'expire' => 60,
  113.             'throttle' => 60,
  114.         ],
  115.     ],
  116.  
  117.     'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800),
  118.  
  119. ];
  120.  
  121. And finally the routes (Web.php):
  122. <?php
  123.  
  124. use Illuminate\Support\Facades\Route;
  125. use App\Http\Controllers\loginController;
  126.  
  127. Route::get('/', function () {
  128.     return view('welcome');
  129. });
  130.  
  131. Route::get('/login', function () {
  132.     return view('login');
  133. }) -> name('login');
  134.  
  135. Route::post('/login', [LoginController::class, 'login']) ->name('doLogin');
  136. //Route::post('/login', [LoginController::class, 'loginSwitch']);
  137.  
  138. Route::get('/dashboard', function () {
  139.     return view('dashboard');
  140. });
  141.  
  142. //Todo: Link this with the loginController.
  143.  
  144. Route::get('/test', function (Request $request) {
  145.     return view('login');
  146. }) ->middleware('auth:MyAppapi');
  147. I hastily scrubbed a few things, so it might not be perfect, but the output log file is populating. My next step will be to test that the model is functioning as expected and to ensure that the methods work as expected.
  148. Thank you for patiently taking the time to look over this stuff.
Advertisement
Add Comment
Please, Sign In to add comment