Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] SaSS multi-tenant #38

Closed
asivaneswaran opened this issue Jun 6, 2020 · 7 comments
Closed

[Question] SaSS multi-tenant #38

asivaneswaran opened this issue Jun 6, 2020 · 7 comments

Comments

@asivaneswaran
Copy link

Hi guys,

I have another question. Let's say I have a multi-tenant SaSS app. I am also using Laravel Nova to have the admin panel. I have a few questions about best practices and general questions:

1- Where do I store the users table? In each tenant? If so how can I access the tenants?
2- As I am using Laravel Nova, I created a resource called Tenant so I can manage the tenants but from where do I access it?
3- Where would you guys suggest that I store the cashier tables?

I am thinking that I need to put the users and cashier tables in the landlord connection, am I missing something?

@masterix21
Copy link
Collaborator

I solved your problems in this way:

  1. .env APP_URL for me is the landlord "domain"
  2. Reserve your Laravel Nova routes to APP_URL
  3. Create a guard for landlord users
  4. Configure nova to use your custom guard

Next, you can keep users and cashier tables in each tenant database.

@uteq
Copy link

uteq commented Jul 30, 2020

@masterix21 Would please elaborate on this? I am very interested.

@masterix21
Copy link
Collaborator

Sure.

The first step is simple.

The second step can be solved by setting .env NOVA_DOMAIN_NAME to your landlord domain.

For the third step, you can change these configurations (I will use the helper config() but feel free to change your config files directly):

config('auth.guards.web-landlord', [ 
    'driver' => 'session', 
    'provider' => 'landlords'
]);

config('auth.providers.landlords', [ 
    'driver' => 'eloquent', 
    'model' => \App\LandlordUser::class 
]);

config('auth.passwords.landlords', [ 
    'provider' => 'landlords', 
    'connection' => 'landlord', 
    'table' => 'password_resets', 
    'expire' => 60, 
    'throttle' => 60 
])

Finally, the fourth step is:

config('nova.guard', 'web-landlord');

config('nova.guard', 'landlords');

If I didn't make a typo mistake, it should works.

@uteq
Copy link

uteq commented Jul 30, 2020

This is awesome, thank you very much :D

@getupkid
Copy link

getupkid commented Sep 30, 2020

Hi there,
I am trying to do a similar thing (not using Nova), but I can't wrap my head around the concept of separate guards and how to implement.

Using Laravel 8.x

I have subdomain routes for tenants e.g. tenant1.network.local. There is an auth guard on a /admin prefix.
So users need to be authenticated when they visit tenant1.network.local/admin.
This would use the users table on each tenant database which uses the default App\Models\User model (implements UsesTenantConnection).
I have managed to set this up without any problems.

The problem I have is that I have also have a landlord administration area at network.local. This uses users table with a different user model App\Models\Network\User (implements UsesLandlordConnection). However, how do I tell my application to use App\Models\Network\User as the authentication model when accessing network.local routes?

I have a separate routes file for my network routes routes/network.php. But I think the way I have set this up is incorrect, for example the middleware.
In my RouteServiceProvider:

Route::domain('network.local')
                ->middleware(['web', 'auth'])
                ->as('network.')
                ->group(base_path('routes/network.php'));

So basically what I want to do is that when the user accesses network.local routes that it's using the App\Models\Network\User model for authentication.

Any help is much appreciated.

Thanks.

@getupkid
Copy link

Nevermind. Total rookie mistake...
Just needed to specify the guard in the middleware syntax ->middleware('auth:my-guard')

Facepalm...

@sys-auditing
Copy link

Hi, @getupkid
can you share with me your code, i have the same issue but i dont find a way to figure out the problem.

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants