Multi-site panel: How to make preview links work?

I was able to successfully set up a 1-panel, multi-site environment like showed in this thread. I am only missing one feature, and that is working preview links.

I was able to modify the base URL of the preview links depending on the site they’re for. But unfortunately, the preview does not work (I always get the live version of each page).

I assume this is because the Kirby backend session is bound to the (sub-)domain of the panel and does not have any effect on the different domains of the websites. Am I correct? Any ideas how to work around this?

Could you please tell me what exactly you tried and what the resulting links looked like?

Sure. My Panel runs at https://panel.mycompany.tld while one of my websites (served by the single Panel) runs at https://myproduct.tld.

If I click on the preview button in the panel, the resulting link looks like this:

https://panel.mycompany.tld/myproduct/my-page?_token=abcdefghij

This will not show the actual website, because my web server and Kirby only serves it at https://myproduct.tld/my-page

So I created a redirection rule in .htaccess redirecting everything from https://panel.mycompany.tld/myproduct/* to https://myproduct.tld/*

So the preview link actually ends up like this:

https://myproduct.tld/my-page?_token=abcdefghij

This works. But not for pages in draft, since the _token generated by the Panel was created using the panel URL and not the website URL. So the tokens do not match, and I am not authenticated to view the preview even if I am logged into the backend.

Edit:

Turns out the preview token mechanism has changed from Kirby 4 to Kirby 5. I V4 I made it work with this custom Page model override:

use Kirby\Cms\Page;

class DefaultPage extends Page
{
    /**
     * Returns a verification token, which
     * is used for the draft authentication
     *
     * We replaced $this->id() wich includes the parent site name with $this->uid().
     * This results in the same token for Panel and frontend and makes preview work again.
     */
    protected function token(): string
    {
        return $this->kirby()->contentToken(
            $this,
            $this->uid() . $this->template()
        );
    }
}

However, Page::token() is gone in V5. Now it’s being handled by Version::previewTokenFromUrl() which seems harder to override.