skip to Main Content

Hi i use laravel 10 with tenancy for laravel, i have two connection, and for my central database i use users model its fine with default connection, but for tenancy i use second connection to retrive data from server ( but not everytime , here i use both conn ) which second connection have the accounts table ( game database), and i want to make session from account. In reast everything is fine but i dont know how to change the providers model to switch from users to accounts..

I want to use for example in breeze auth package account model not users..

I did this but its work just if i make login ( succesfully ) but when i try to do logout ( just logout i tried ), he tried to select from users model.. So i’m sure i did something wrong.. or maybe everything is wrong

i change the config/auth and i created an middleware (declare in kernel.php for tenant group):

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

        'tenant' => [
            'driver' => 'session', 
            'provider' => 'accounts', 
        ],
    ],

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => AppModelsUser::class,
        ],

        'accounts' => [
            'driver' => 'eloquent',
            'model' => AppModelsTenantAccount::class, 
        ],
    ],

class SwitchAuthModel
{
    /**
     * Handle an incoming request.
     *
     * @param  Closure(IlluminateHttpRequest): (SymfonyComponentHttpFoundationResponse)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        config(['auth.guards.web' => config('auth.guards.tenant')]);
        return $next($request);
    }
}

Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    I fixed..

    in my InitializeTenancyByDomain i put this in __construct

    Auth::setDefaultDriver('tenant'); // Set the default guard to 'tenant_user'
    Auth::shouldUse('tenant');
    

    and you need to create a new auth middleware for tenant.

    Best regards, Alex.


  2. I solve this by making a custom login for the tenant users.

    auth.php

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => env('GUARD_CONNECTION','users'),
        ],
    
        'tenant' => [
            'driver'   => 'session',
            'provider' => 'tenant_users',
        ]
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => AppModelsUser::class,
        ],
    
        'tenant_users' => [
            'driver' => 'eloquent',
            'model'  => AppModelsTenantUser::class,
        ],
    ],
    

    Helper function that returns guard tenant or web

    if (!function_exists('guard')) {
     function guard(): string
     {
        return tenant('id') ? 'tenant' : 'web';
     }
    }
    

    Custom Login Controller Method For Tenant Users

    public function login(Request $request)
    {
        if ( Auth::guard(guard())->attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) {
            dump('tenant user login');
        }
    }
    

    For tenant users just add a guard with auth middleware like auth:tenant

    Route::group(['middleware' => 'auth:tenant', 'prefix' => 'modals'], function () {
      Route::get('{type}/{itemId?}', [ModalController::class, 'show'] )->name('modal.show');
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search