Iām new to Frontity, so apologies if this is obvious. I tried searching in both the community and on Google for a potential solution first, but to no avail. Iām curious to know if thereās supported package to render the same password protection features on Wordpress.com to a Frontity project. As a reference this is the UX for password protected pages and posts on wordpress:
(yes, Iām using my wedding as an excuse to learn about Frontity )
So, I was able to add a simple block in the post of the Mars Theme to give the user an interface to type in a password. This is the snippet:
// packages/mars-theme/src/components/post.js LN:58
{ post.content.protected && !post.content.rendered && (
<div className="protected">
<p>
This is a protected post.
</p>
<form onSubmit={ submitPassword }>
<input type="password" ref={ input } />
<input type="submit" value="Submit" />
</form>
</div>
) }
And, I was able to see that you simply add ?password=PASSWORD to the Wordpress REST API in order to get the JSON populated with the password protected post/page content.
But, I donāt understand how actions work from the @frontity/tiny-router. In the submitPassword handler if I try to force an action on the same link with the added query parameters the information is not updated.
function submitPassword(e) {
e.preventDefault();
const uri = `${state.router.link}?password=${input.current.value}`;
actions.source.fetch(uri, { force: true }).then(() => {
var data = state.source.get(state.router.link);
var post = state.source[data.type][data.id];
// Unfortunately the post isn't populated with new data...
});
}
My intention was to cause either a page refresh or a react flush once I knew the information was correct. Is this how actions should be used? Or should I be overriding the autoFetch from the router?
Firstly, welcome to the community, and secondly congratulations on the upcoming wedding.
Youāve made great progress on your issue so far, well done. To fetch the protected data you need to use useEffect and to populate the state with data that Frontity thinks has already been fetched you need to use { force: true } with the fetch call.
See our docs here for more detailed info on this. The last two paragraphs and code samples in that section should give you what you need.
Hope this helps, and do let us know how you get on with it.
Thanks! Excited to be using it as an opportunity to learn Frontity.
I redid my snippet to make sure the fetch was called within a useEffect. While this code runs and the promise on the fetch call resolves, the data does not seem to be updated. The console.log("Data changed"); only fires on page load, not when the fetch call resolves.
I would love to get advice from the community on a more effective / efficient way to set the state within actions. I could not figure out how to directly add to state.source.params via packages/mars-theme/src/components/post.js. This was the best I could figure out.
Iām also going to file an issue so that the force parameter passes through and actually fetches new data when forced. Hope this helps someone get password protected pages / posts working on their sites.
Hi, Iām curious to how you were able to modify wp-source? Because Iām trying to access password protected posts in frontity, but am not sure how. But this seems to look like exactly what Iām trying to do that you were able to accomplish. It would be great if you can share to how you did it. Thanks in advance.
Thanky for the reply
I decided to try it out with the Mars theme, but it doesnāt seem to work, it just displays an empty content below. Is there maybe something Iām missing, that also needs to be added?
I added it like this:
<Html2React html=
{ post.content.protected && !post.content.rendered && (
<div className="protected">
<p>
This is a protected post.
</p>
<form onSubmit={ submitPassword }>
<input type="password" ref={ input } />
<input type="submit" value="Submit" />
</form>
</div>
) }
/>
Hi again,
I was actually able to get it to work, but nothing happens when pressing the Submit button next to the password field. Do you have an idea what Iām missing to make it work?
Itās been a long time so I donāt know off the top of my head. But, looking at my git history I think I only changed the post.js file I shared above.