Jump to content

Rendering unpublished pages to unauthenticated user


ZGD
 Share

Recommended Posts

TL;DR: Is it possible to render unpublished pages even if the request comes from an unauthenticated user?

I'm currently building a website that consists of a two parts: 

  • A front-end 'app' built using Vue.js served by a node.js/express server (necessary for server-side rendering).
  • A ProcessWire back-end that serves up a JSON API, this is running on an Apache server.

I've added hooks so the "View" page links/tabs in ProcessWire direct to the front-end app's URLs, rather than just showing a page of the raw JSON data exposed by ProcessWire. So for example if my PW page lives at https://api.example.com/pages/foo, clicking View directs to a URL pointing to the front-end app like https://example.com/foo.

The problem I now have is that I need to be able to view unpublished pages. As sessions are not shared between ProcessWire and the front-end as they run on different servers, this means if I redirect to the app from PW as above, the app then makes a request to PW and obviously returns a 404. PW rightly sees the request from the app as coming from an unauthenticated user.

My proposed solution is that I edit the page preview hook to include a query in the URL like so https://example.com/foo?session=a1b2c3d4e5. The front-end server then passes this query string to any requests it makes to the PW API (e.g. https://api.example.com/pages/foo?session=a1b2c3d4e5), which in turn checks it against a list of valid session variables (even better if I could somehow check this against a session_id of logged in users). If the query was valid, then PW should render the page even if it is unpublished. Is this possible?

Link to comment
Share on other sites

7 minutes ago, szabesz said:

Hi

https://processwire.com/api/ref/pages/get/

Quote: "Use this method when you need to retrieve a specific page without exclusions for access control or page status."

Is it what you are looking for?

This is similar to what I'm looking for but I don't think it would work in this case. I would essentially do...

$page = $pages->get($page);

... in my template. But if I understand correctly ProcessWire will redirect to the 404 page so I can't actually run this code in my template. I guess I would need to somehow hook into the page render method and prevent this 404 redirect?

Link to comment
Share on other sites

Why would a page query redirect to 404? Am I missing something? We are not talking about accessing those pages directly via the browser, are we?

BTW, $pages->get()returns one page only, if you need a PageArray with more than one Page in it, you can use find() with  include=all, eg:

$pages->find("template=product, include=all");

Edited by szabesz
typo
Link to comment
Share on other sites

14 minutes ago, szabesz said:

Why would a page query redirect to 404? Am I missing something? We are not talking about accessing those pages directly via the browser, are we?

Basically yes, I'm trying to access an unpublished page via an asynchronous request from another server, so it's effectively the same as trying to access it from a browser. My JS app running on the node.js server makes an AJAX request to the ProcessWire specific URL (an unpublished page), but as that session is not authenticated, PW responds with 404.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...