Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ok, one more parent thread reply:
- I think I got it!
- I'm posting the full skeliton in case others are stumbling on this...
- App\Providers\MyUserProvider.php:
- namespace App\Providers;
- use Closure;
- use Illuminate\Contracts\Auth\Authenticatable as UserContract;
- use Illuminate\Contracts\Auth\UserProvider;
- use Illuminate\Contracts\Hashing\Hasher as HasherContract; //Currently not used
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Database\ConnectionInterface; //Currently not used. Likely will be yanked.
- use Illuminate\Auth\GenericUser;
- class MyUserProvider implements UserProvider
- {
- //Configuration (WILL BE?) stored in config/MyApp.php
- private $tenantShortname;
- private $apiUser;
- private $apiToken;
- private $apiKey;
- private $apiHost;
- public function __construct()
- {
- //Lets get the config values
- $this->tenantShortname = config('MyApp.tenantShortname');
- $this->apiUser = config('MyApp.apiuser');
- $this->apiToken = config('MyApp.apitoken');
- $this->apiKey = config('MyApp.apikey');
- $this->apiHost = config('MyApp.host');
- $fh = fopen('/tmp/MyApp.log', 'a');
- fwrite($fh, "MyAppUserProvider initialized in auth\n");
- fclose($fh);
- //Database connection not needed, so we'll comment these out for now.
- // $this->connection = $connection;
- // $this->table = $table;
- }
- Next up: App\ProvidersMyServiceProvider.php
- namespace App\Providers;
- use App\Providers\MyAppUserProvider;
- use Illuminate\Contracts\Foundation\Application;
- use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
- use Illuminate\Support\Facades\Auth;
- class MyServiceProvider extends ServiceProvider
- {
- public function boot()
- {
- $this->registerPolicies();
- // Register the custom user provider
- $fh=fopen('/tmp/MyApp.log', 'a');
- fwrite($fh, "In MyServiceProvider::boot()\n");
- fclose($fh);
- Auth::provider('MyAppServiceProvider', function (Application $app, array $config) {
- $config = $app->make('config');
- // I will make sure it's accepting the configuration from injection.
- return new MyUserProvider ();
- });
- }
- }
- App\config\auth.php:
- return [
- 'defaults' => [
- 'guard' => env('AUTH_GUARD', 'web'),
- 'passwords' => env('AUTH_PASSWORD_BROKER', 'users'),
- ],
- 'guards' => [
- 'web' => [
- 'driver' => 'session',
- 'provider' => 'users',
- ],
- 'myappapi' => [
- 'driver' => 'session',
- 'provider' => 'myAppServiceProvider',
- ],
- ],
- 'providers' => [
- 'users' => [
- 'driver' => 'eloquent',
- 'model' => env('AUTH_MODEL', App\Models\User::class),
- ],
- 'myAppServiceProvider' => [
- 'driver' => 'myAppServiceProvider',
- 'model' => App\Providers\myAppUserProvider::class,
- ],
- ],
- 'passwords' => [
- 'users' => [
- 'provider' => 'users',
- 'table' => env('AUTH_PASSWORD_RESET_TOKEN_TABLE', 'password_reset_tokens'),
- 'expire' => 60,
- 'throttle' => 60,
- ],
- ],
- 'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800),
- ];
- And finally the routes (Web.php):
- <?php
- use Illuminate\Support\Facades\Route;
- use App\Http\Controllers\loginController;
- Route::get('/', function () {
- return view('welcome');
- });
- Route::get('/login', function () {
- return view('login');
- }) -> name('login');
- Route::post('/login', [LoginController::class, 'login']) ->name('doLogin');
- //Route::post('/login', [LoginController::class, 'loginSwitch']);
- Route::get('/dashboard', function () {
- return view('dashboard');
- });
- //Todo: Link this with the loginController.
- Route::get('/test', function (Request $request) {
- return view('login');
- }) ->middleware('auth:MyAppapi');
- 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.
- Thank you for patiently taking the time to look over this stuff.
Advertisement
Add Comment
Please, Sign In to add comment