0

I have rerouted all the traffic to my main domain let's say www.example.com and my first page is www.example.com/login so any entry in the subdomain will be redirected to main domain so something.example.com/login will end up on the same page as www.example.com/login (but the subdomain will stay in the url). The reason I am doing this is that i have multi 'clients' and 'users' every user is responsible for one or more client and its all set in the database and working perfectly so i can login with user1 i will see some tasks for user1 on client1 and when i login with user2 i will see tasks from user2 on client2 etc...

Now i need to do one more thing to make it look a bit better, when someone opens example.com and login with user1 credentials i want him to be redirected to client1.example.com and at the same time when someone opens client1.example.com i want him to see the logo from that client.

All the database queries and other login issues are handled but i am facing couple of issues:

  • how to redirect to the correct client (subdomain) ?
  • and vice versa if a (super admin) user which responsible for managing clients and users logs in how to redirect him to main domain (example.com) without?

One more issue but i think it will be solved when i can solve the other issues is when a user manages more than one client, i want to give him the ability to switch clients something like user1 have a menu to switch to client1 or client2 but any redirection i make is logging the user out. how can i maintain the session with this feature ?

p.s when different users logs in the (theme) colors and logos of the application are being called from database according to the client and thats why i need to read the subdomain on the first page so i can change the login logo according to the subdomain

this is a piece of my code to see how i am achieving this if anyone is interested

$currentUrl =  $request->getHttpHost();
$baseUrl = $this->container->getParameter('base_url');
$subdomain = str_replace('.'.$baseUrl, '',$currentUrl);
if (sizeof($user->getClients()) > 0) {
                $filter = $this->em->getFilters()->enable(Utils::CLIENT_FILTER_NAME);
                if ($user->getLastLoggedInClient() !== null) {
                    $client = $user->getLastLoggedInClient();
                } else {
                    $client = $user->getClients()->first();
                }
                if ($client == null) {
                    throw new ClientNotFoundException();
                }
                if(!$client->isActive()){
                    throw new ClientNotActiveException();
                }
                $this->session->set(Utils::CLIENT_ID_NAME, $client->getId());
                $this->session->set('client', $client);
                $filter->setParameter(Utils::CLIENT_ID_NAME, $client->getId());
                $user->setLastLoggedInClient($client);
                $this->em->persist($user);
                $this->em->flush();
            }
            else{
                return new RedirectResponse('/logout');
            }
            return new RedirectResponse('/');

so i think somewhere around here return new RedirectResponse('/'); i need to redirect to the correct subdomain.

Thanks!

Yamen Nassif
  • 2,416
  • 2
  • 24
  • 48

1 Answers1

1

When you first realise that a user needs to login (from client1.example.com), put the URL, or 'client1' client-name into a session, readable on www.example.com - or add it to the URL (like https://www.example.com/login/to/client1 - the route would be '/login/to/{clientname}'), or more simply /login?clientname=client1.

When a user has logged in, and been verified to be a member of 'client1', then the redirection would be to a route like 'client_app_dashboard', ['clientname' => 'client1'] - and a route definition of @Route("/", name="client_app_dashboard", host="{clientname}.example.com")

The Symfony docs have information on How to Match a Route Based on the Host.

As for a logo - that would be fetched and displayed based on the $clientname on the www. homepage.

Alister Bulman
  • 34,482
  • 9
  • 71
  • 110