1

Im im using craft as a headless CMS to power a Nuxt frontend.

I have a situation where i need the user to login on the frontend and then be redirected to a url within the craft CMS admin.

My question is; how do i persist login from a subdomain (my Nuxt front-end login form) to to the root domain where my CMS admin is located? Or how can i log the user in to the CMS dashboard using the response from an axios login?

More info:

Frontend: mydomain.com

CMS install: my-craft-install.mydomain.com

Frontend:

My frontend login form is using axios e.g:

const data = {
  email,
  password
};
const response = await axios.post("myfrontend.mydomain.com/api/user/login", qs.stringify(data))

SAML Redirect:

After successful login the user will be redirected to a url required by the plugin "SAML Identity Provider" which is under the url

https://my-craft-install.mydomain.com/sso/login/request/[key]

From here the user will be authenticated by the plugin and redirected again to our 'Brilliant Assessment' sub domain.

Any help would be greatly appreciated. Cheers, Jamie

1

You can use defaultCookieDomain config setting to make all Craft cookies use your root domain:

// config/general.php

// mind the dot at the start
'defaultCookieDomain' => '.mydomain.com',

This ensures that Craft's cookies apply to both the root domain and the subdomain, so the login should persist.

3
  • Thanks for the response, unfortunately this doesn't work. After login into the front end form and navigating to the subdomain i am still presented with the craft login screen. fyi i cleared caches and hard reloaded browser etc – Jamie Nuom Jul 21 at 10:04
  • 1
    @JamieNuom After logging in, can you check the cookies set in your browser? Do they use the correct domain? There are also other possibilities why it's not working. Does axios use the browser's user agent string? Try turning off requireMatchingUserAgentForSession and requireUserAgentAndIpForSession in the general config. Maybe try setting sameSiteCookieValue to Lax to make sure cookies are sent during initial navigation. There are many reasons why the session might not be accepted cross-domain, make sure to check the cookies in your browser and your config to find potential problems. – MoritzLost Jul 21 at 10:25
  • I've decided to disable headless mode for this project and create a frontend login from within the craft framework rather than on a separate url as the project needs wrapping up and i cant find a time efficient solution. Thanks for your thoughts on this. I did try what you suggested but to no avail unfortunately – Jamie Nuom 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.