Home > Back-end >  How to bypass Socialite in development?
How to bypass Socialite in development?

Time:10-22

I am using a KeyCloak login in production, but in development, I would like to log in with the default user populated in seeders. I tried to bypass the authenticate method with this:

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    protected function redirectTo($request)
    {
        if (config('app.env') !== 'production') {
            \Auth::loginUsingId(1);
            return route('home');
        }
        if (!$request->expectsJson()) {
            return route('login');
        }
    }
}

But, it doesn't work (too many redirect). How would it be possible to simply bypass the Auth in development mode with Laravel Vite Socialite?

In my routes I have:

Route::get('auth/redirect', [KeyCloakController::class, 'redirect'])->name('login');
Route::get('auth/callback', [KeyCloakController::class, 'callback']);

Route::middleware('auth')->group(function () {
    Route::get('/', 'App\Http\Controllers\NameController@index')->name('home');
});

CodePudding user response:

One way is to add this in the boot method of AppServiceProvider.php:

public function boot()
{
    if (config('app.env') === 'local') {
        \Auth::login(\App\Models\User::find(1));
    }
}
  • Related