0

I developed a multi-tenant database schema. Each subdomain connects to its respective database. Each schema has, for example, its own user table, therefore, when the user accesses the url of his own subdomain, Laravel authentication uses respective user table.

enter image description here

Everything is going very well, but the 'challenge' now is, I need a form where the user can select which domain he wants to connect to, but in that same form he already gives his username and password.

enter image description here

The point is:

  • You can authenticate to a particular subdomain (example: AAA), using the credentials for that subdomain (AAA) but with the form open on another subdomain (COMPANY NAME CCC), and if redirected, with authentication done to AAA subdomain?
  • Should I extend the class 'AuthenticatesUsers' and/or LoginController? I know that in the Laravel documentation there is a section 'Manually Authenticating Users'.

But if user is in the URL of CCC subdomain but user wants to be authenticated to AAA subdomain, I 'imagine' that AUTH facade, this will use the USER model of CCC subdomain, if this is true, so I think, I would need to be redirected first to the AAA subdomain and do an authentication after redirection, or I can authenticate first, create the authentication session from another subdomain, before redirection?

Magno Alberto
  • 628
  • 14
  • 27
  • Are you trying to authenticate a user in B if they are authenticated in A? – PHPer Nov 15 '19 at 14:50
  • Not exactly that...because if I try, the AUTH (I think...) will try to authenticate the USER model in B database, but the user record is at A database. I thought, if it's functional, to redirect first, and maybe keep the credentia.ls in session, and simulate authentication after redirection. But I don't know if it's a good idea, if it's functional, or if there's something "smarter" to do it – Magno Alberto Nov 15 '19 at 15:02
  • It will all depend on your codebase. Do all the schemas share the same Controllers, or your controllers are contained to the subdomain level ? I am trying to understand the architecture in place. Because I think you could do exactly the same thing with 1 specific schema, and introduce a differentiating column for the subdomain – PHPer Nov 15 '19 at 16:00
  • Thanks for the feedback, I don't know if it's relevant or if that's what you would like to know, but I have a middleware (TenantMiddleware) in \App\Http\Kernel, that takes care of the database connection, based on the subdomain url, if the user is, for example, https://aaa.domain.com, middleware checks url and switches connection to for its schema database. – Magno Alberto Nov 15 '19 at 16:10
  • Hypothetically, if I'm in domain B, I look up the user table from domain A users, and since this query would be a collection, somehow use this collection together with `AUTH` facade or `AUTH` always needs the USER model? – Magno Alberto Nov 15 '19 at 16:18
  • 1
    How I love when I keep beating my head on a problem, and after days, out of nowhere, the solution appears, actually I already had my own solution... was simply toggle the connection "again" using the select dropdown value, but everything being done in LoginController, follows code snippet: `$company = Company::find($request->only('dropdown_unidade_id')); app(ManagerTenant::class)->setConnection($company); $this->attemptLogin($request)` – Magno Alberto Nov 16 '19 at 13:56
  • Glad you found a way out – PHPer Nov 16 '19 at 16:18

0 Answers0