Last night I built a feature on Ianâs blog so that he can lock certain posts behind a passphrase, so more private posts arenât publicly accessible on the internet.
I wanted to do a quick write-up of how the feature works, mostly for my own future reference but also perhaps for your curiosity.
Ghost, the free software platform that powers this blog and Ianâs, has the concept of Memberships. People can sign up to your site (and optionally be required to pay a subscription), and then they unlock extra posts.
Thatâs the backbone of how this feature works: the specific posts that Ian wants to lock are set to âMembers Onlyâ, so they wonât be viewable unless youâre logged in as a member.
Ghost has default text that it shows to visitors who arenât members yet when they try to view a members-only post (it encourages them to become a member). But we can override that text with our own (specifically by editing the partials/content-cta.hbs
file in the theme).
I edited it to contain a form for unlocking the post:
What a visitor sees when clicking on a locked blog post.
Now the fun part: checking the passphrase.
When you submit this form, it sends the passphrase you entered to a separate server I control, and compares the passphrase. If itâs incorrect, it redirects you back to the blog post and adds ?wrong
to the end of the URL.
This tells the page to add âWrong passphrase, please try againâ under the form.
Incorrect password.
If the passphrase is correct, we need to get you logged into the site. But instead of using one account per person (as Ghost intends), I created one member account on Ianâs blog. This one account is the only account that can view all of these locked posts, since I turned off new signups.
When the correct passphrase is submitted, my server contacts Ghost and gets a temporary URL that logs you in as my own user (this uses the Ghost Admin API; the endpoint isnât documented but you can find it in dev tools when clicking âimpersonateâ on a user).
This means that anyone who has the correct passphrase is redirected to a URL that will log them in as my one member account on Ianâs blog. Itâs not their account; itâs a shared account that everybody uses. However, this all happens in the background, so the user doesnât even really know that theyâve logged into an account.
I also add a redirect
query parameter to the secure URL for logging them in, and then Ianâs blog has JavaScript that knows to send them back to the blog post they were trying to view instead of the homepage.
Lastly, when youâre logged in as my one user, I add a bit of text to the top of the homepage:
This gives confirmation that youâve unlocked the posts (which, behind the scenes, means having logged in to the shared user account), and gives you a button to âre-lockâ the posts (aka log out). That button is just a <button>
with the data-members-signout
attribute, and Ghost handles the rest.
Itâs a bit of a hack to get the experience we want, but not bad at all. All in all, Iâm very happy with the solution.