social-poster icon indicating copy to clipboard operation
social-poster copied to clipboard

Facebook Provider error when no `cpTrigger` is defined (headless)

Open Tam opened this issue 3 years ago • 6 comments

Description The Facebook provider throws Undefined array key "host" when the cpTrigger is not set (when Craft is in headless mode).

Caused by this line in SocialPosterHelper.php:25

$redirectUri = str_replace(Craft::$app->getConfig()->getGeneral()->cpTrigger . '/', '', $redirectUri);

https://github.com/verbb/social-poster/blob/craft-3/src/helpers/SocialPosterHelper.php#L25

Tam avatar Jan 27 '22 14:01 Tam

So this helper function is really only there to generate the redirect URI for OAuth. I'm not sure why the headless config setting has any issue here, as you still need to access the control panel for Craft.

When are you seeing this error exactly?

engram-design avatar Jan 28 '22 02:01 engram-design

@engram-design It's when we try to access the providers index

Tam avatar Jan 28 '22 11:01 Tam

Strange, I've got headlessMode => true enabled on my install. Are you able to send through the full error with devMode on?

And just to verify headlessMode was working, my front-end is offline (in a traditional Twig setup).

Are you setting baseCpUrl in your config?

engram-design avatar Jan 28 '22 21:01 engram-design

The issue seems to be caused by cpTrigger being set to null (since we're running craft headless on a sub-domain). This then causes the str_replace to see only the / and goes ahead and removes all slashes. This means the redirectUri goes from something like https://github.com/verbb/social-poster/issues/36 to https:github.comverbbsocial-posterissues36

Tam avatar Jan 31 '22 11:01 Tam

Right, gotcha. Setting cpTrigger => null will do this, and not something I've ever seen before. I'll get a setup organised to test that (I get all sorts of errors trying to access the CP with this set to null)

engram-design avatar Feb 01 '22 00:02 engram-design

We normally stick this at the top of our web/index.php if it helps!

if (
    $_SERVER['REQUEST_URI'] !== '/graph' &&
    strpos($_SERVER['REQUEST_URI'], '.xml') === false
) {
    define('CRAFT_CP', true);
}

Tam avatar Feb 01 '22 15:02 Tam