Alter domain after login


#1

Hi All,

I am looking for the best way to change the domain component of the url once the site visitor logs in. (ie set a new FULL_BASE_URL for the application)

eg if the site visitor arrives at the generic www.domain.com/login page and they successfully login, I want to check their details and see it they belong to a specific subdomain (subdomain.domain.com/afterlogin) and then automatically redirect them to the new subdomain with the same loginAction there were going to use as per AuthComponent.

I have tried injecting a ‘_host’ => ‘subdomain.domain.com’ into the array when I generate a route via router::url and it correctly forms the full text url with the new subdomain

eg. $newsubdomainurl = Router::url([’_host’ => ‘subdomain.domain.com’,
‘controller’ => ‘articles’,
‘action’ => ‘index’]);

When I use $this->redirect($newsubdomainurl) the domain component of the url remains unchanged.

So the short question is how do I go about this?


#2

Hi Again,

So it looks like it is working but I am running into a CORS issue (appearing in Chrome developer tools console)

Access to XMLHttpRequest at ‘https://subdomain.domain.com/index’ (redirected from ‘https://domain.com/’) from origin ‘https://domain.com’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: Redirect is not allowed for a preflight request.

Anyone know how to solve this problem. I have started to look at the docs and see a section on CORS but there isn’t much in the way of info that I understand lol.

Thanks in advance.


#3

Hi Me again,

Ok found a CORS Middleware plugin which did the trick.

Now it looks like a CakePHP Auth/Cookie error because Cake ends up routing me to a login page for the new domain.

Anyone know how to make Cake Auth work across subdomains and maybe even restrict auth to multiple specific subdomains???

Thanks in advance


#4

Looks like I have it all sorted out now.

Needed to add some session config into app.php

'Session' => [
    'defaults' => 'php',
    'ini' => [
        // To Allow access to cookie from HTTP and HTTPS
        // https://book.cakephp.org/3.0/en/development/sessions.html#session-configuration
        'session.cookie_secure' => false,
        // To Allow session cookies to work across subdomains
        'session.cookie_path' => '/',
        'session.cookie_domain' => 'domain.com'            
    ]
],

Cheers