archtechx / tenancy

Automatic multi-tenancy for Laravel. No code changes needed.
https://tenancyforlaravel.com
MIT License
3.59k stars 425 forks source link

laravel/ui Authentication #427

Closed epirir closed 4 years ago

epirir commented 4 years ago

I have a problem.

in tenant.php

Route::middleware([ 'auth', 'web', InitializeTenancyByDomain::class, PreventAccessFromCentralDomains::class, ])->group(function () {

Route::get('/', 'DashboardController@dashboard')
->name('index');

Route::get('dashboard', 'DashboardController@dashboard')
->name('dashboard');

});

error:

Symfony\Component\Routing\Exception\RouteNotFoundException Route [login] not defined.

Help Please.!

epirir commented 4 years ago

update:

Route::middleware([ // 'auth', 'web', InitializeTenancyByDomain::class, PreventAccessFromCentralDomains::class, ])->group(function () { Route::middleware(['universal'])->group(function () { Auth::routes(); });

Route::get('/', 'DashboardController@dashboard')
->name('index');

Route::get('dashboard', 'DashboardController@dashboard')
->name('dashboard');

});

This worked for me, any other better restraint? Thank you :)

epirir commented 4 years ago

my problem now is that it does not validate the sessions of the tenants it takes me the same :(

epirir commented 4 years ago

suggestions ??? :(

stancl commented 4 years ago

So what's the issue? Session scoping is covered in documentation.

hardikdangar commented 4 years ago

@epirir are you suggesting both tenants now use same session? because in my setup i am not able to replicate your issue.

iizno commented 4 years ago

Do we now need to add Auth::routes(); in the routes/tenant.php ?

iizno commented 4 years ago

Seems I have the same kind of errors as @epirir.

I need to manually insert the Auth::routes() in tenant.php in order to have the login route available on the tenant app but I have some odd error then :

Undefined variable: errors $errors is undefined

It seems the ShareErrorsFromSession::class middleware is not loaded ? :/

Here is my tenant.php

image

stancl commented 4 years ago

Undefined variable: errors $errors is undefined

I can't help when I don't see a stack trace.

Also, your Auth::routes() being in tenant.php doesn't do anything. They need to be inside the GROUP with web and the tenancy middlewares.

Then, inside that group, create another group with auth MW.

iizno commented 4 years ago

Thank you @stancl , that's working perfectly !

@epirir Here is an exemple of my routes/tenant.php in order to get the Auth working as in the v2 :

image

stancl commented 4 years ago

Glad to hear that

epirir commented 4 years ago

thanks to all, I was just going to discuss a similar solution :)

Route::middleware([ 'web', InitializeTenancyByDomain::class, PreventAccessFromCentralDomains::class, ])->group(function () { Route::middleware(['universal'])->group(function () { Auth::routes(); });

Route::middleware(['auth'])->group(function () {
    //Routes of the app.
});

});

hardikdangar commented 4 years ago

so if this is resolved @epirir can you close this one ?

iizno commented 4 years ago

@hardikdangar Stancl closed it yesterday :)

mwikala commented 3 years ago

I'm currently using v3.2 of the package with Laravel 8 and I am experiencing similar issues to what's discussed here but following the suggestions here, it doesn't seem to have fixed the issue.

My issue; In Laravel 8 when trying to authenticate inside a Tenant instance. It authenticates just fine but the 'auth' middleware seems to NOT recognise that the user is authed and keeps redirecting to the login route. I have no clue how to even go about fixing this, would I have to make the Laravel 'auth' middleware aware of the tenant or something? It's taken up some hours of my day so far so I'll kick myself if the answer is really simple :smile:

Auth Routes setup inside tenant.php

Route::middleware([
    'web',
    'universal',
    InitializeTenancyByDomain::class,
])->group(function () {
    Auth::routes();
});

// All routes in this next group just keep throwing back to the login routes inside the 'auth' middleware
Route::middleware([
    'auth',
    'web',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {

I'm not sure if this would be a separate issue but seemed relevant.

alsahlawi commented 3 years ago

@mwikala reading your issue, i think what we have is similar. I am using laravel 8 as well and having trouble in Auth. Referance: #510

mwikala commented 3 years ago

@mwikala reading your issue, i think what we have is similar. I am using laravel 8 as well and having trouble in Auth. Referance: #510

Hello @alsahlawi so after debugging this with @stancl we able to figure out that it was an issue with my Routes setup inside tenant.php. You need make sure that you only have ONE parent route group which initializes tenancy, then place all other groups inside.

Here is an example of what that looked like in my case;

Route::middleware([
    'web',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {

    Auth::routes();

    // management experience
    Route::middleware(['auth'])->prefix('/')->group(function () {

        // All the routes that require the user to authorized

    });

});

I hope that helps if it doesn't and you can't find an issue in this repository that resolves your issue, then please feel free to reach out in the package's Discord and you can get some help from the community there.

mvksastry commented 3 years ago

Hello @mwikala, @alsahlawi and @stancl,

I am desperate for help. I have setup this tenancy on Laravel 8x, with Jetstream/livewire scaffold.

I can see the dd(\App\User::all()) as suggested in docs. Now I just cant login as a tenant when using Auth.

Set-up My my central-domain does not have any users table. My two test tenants have their own users tables.

When I go to the tenant page, I can see the dump (without any login).

Now when I try to login with Auth, I get this error

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'best.users' doesn't exist (SQL: select * from users where email = abcdef@test.res.in limit 1) error.

This means to me that it is looking for users table in centra_domain than the tenant's users table. How do I solve it?

I have exactly configured the tenant routes as stated in the post above by @mwikala.

ps: when i request the login page, the sessions table of the centra_domain makes an entry and no entries in the tenants sessions table. Only manually login using login::attempt makes entries in tenants session table but does not maintain the session status as I can't see authenticated users name or other info.

Thanks in advance for all the help.

stancl commented 3 years ago

if you went on discord, which is the place where support questions belong, you'd see that I sent @mwikala an entire working step by step solution

mvksastry commented 3 years ago

can you please give any link @mwikala on this topic, i am already logged in discord

mwikala commented 3 years ago

Hello @mwikala, @alsahlawi and @stancl,

I am desperate for help. I have setup this tenancy on Laravel 8x, with Jetstream/livewire scaffold.

I can see the dd(\App\User::all()) as suggested in docs. Now I just cant login as a tenant when using Auth.

Set-up My my central-domain does not have any users table. My two test tenants have their own users tables.

When I go to the tenant page, I can see the dump (without any login).

Now when I try to login with Auth, I get this error

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'best.users' doesn't exist (SQL: select * from users where email = abcdef@test.res.in limit 1) error.

This means to me that it is looking for users table in centra_domain than the tenant's users table. How do I solve it?

I have exactly configured the tenant routes as stated in the post above by @mwikala.

ps: when i request the login page, the sessions table of the centra_domain makes an entry and no entries in the tenants sessions table. Only manually login using login::attempt makes entries in tenants session table but does not maintain the session status as I can't see authenticated users name or other info.

Thanks in advance for all the help.

Try using the TenantConnection trait on your User model.

// user class
use TenantConnection;

// rest of your code
mvksastry commented 3 years ago

@mwikala , When i have inserted the TenantConnection, first time i am getting a new error: Database connection [tenant] not configured.

stancl commented 3 years ago

Support questions belong to Discord. Please respect the role of the issue tracker.

Reading support discussions on GitHub isn't something I have time for.

mwikala commented 3 years ago

@mwikala , When i have inserted the TenantConnection, first time i am getting a new error: Database connection [tenant] not configured.

That error tells me you're either not on a tenant domain e.g. foo.localhost or that you haven't initialized tenancy on your routes using either of the below middleware;

https://tenancyforlaravel.com/docs/v3/tenant-identification/#domain-identification

Refer to my example below where you see i use the middleware InitializeTenancyByDomain::class

Route​::​middleware​([
    ​'web'​,
    ​InitializeTenancyByDomain​::class, // here is the tenant initiated
    ​PreventAccessFromCentralDomains​::class,
])->​group​(​function​ () {

    ​Auth​::​routes​();

    ​// management experience​
    ​Route​::​middleware​([​'auth'​])->​prefix​(​'/'​)->​group​(​function​ () {

        ​// All the routes that require the user to authorized​

    });

});
mvksastry commented 3 years ago

My routes are exactly as stated by you. In fact after facing the problem two days, I saw this post an hour ago and did exactly as stated. I do feel, the tenancy is not getting initialized. However, when I do dd(\App\User;;all()) in respective tenant domain pages, as suggested in tutorial, I do see respective users in those domains dummy home landing page. Hence, I suppose, tenancy is initialized.

stancl commented 3 years ago

working solution is on discord