Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit d8ec88c

Browse files
authored
fixed redirects (#290)
1 parent 5089ee7 commit d8ec88c

File tree

6 files changed

+96
-163
lines changed

6 files changed

+96
-163
lines changed

docs/tutorials/front-end/ad-free-experience.md

-56
This file was deleted.

docs/tutorials/front-end/locking-media-content.md

-96
This file was deleted.

docs/tutorials/front-end/locking-page.md

+79-4
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ Important: `​unlockProtocolConfig​` is a global object (it should be defined
5858

5959
## Handle Events
6060

61-
Once loaded the unlock script will trigger events on the page’s ​`window`​ object.
62-
These events let your web application adjust its behaving or the content it
61+
Once loaded the unlock script will trigger events on the page’s ​`window`​ object.
62+
These events let your web application adjust its behaving or the content it
6363
displayed based on the status. For a full list see [Paywall documentation](/../../tools/paywall).
6464

65-
6665
Here is an example:
6766

6867
```javascript
69-
window.addEventListener("unlockProtocol.status", function (e) {
68+
window.addEventListener("unlockProtocol.status", function(e) {
7069
var state = e.detail;
7170
// the state is a string whose value can either be 'unlocked' or 'locked'...
7271
// If state is 'unlocked': implement code here which will be triggered when
@@ -105,3 +104,79 @@ You can easily configure the following with your lock by replacing the lock addr
105104
borderRadius: "6px"
106105
}} scrolling="no" title="Unlock Sample" src="https://codepen.io/unlock-protocol/embed/bGWZvGM?default-tab=html%2Cresult&editable=true" frameborder="no" loading="lazy" allowTransparency="true" allowFullscreen="true">
107106
</iframe>
107+
108+
## More examples
109+
110+
### Ad-free experience
111+
112+
You can easily use the approach detailed above to create an ad-free experience on your own site.
113+
114+
Once you have [deployed a lock](https://unlock-protocol.com/guides/how-to-create-a-lock/), add the Unlock paywall application JavaScript to your page (see above) and configure it.
115+
116+
Finally, add an event handler to capture the change of state between `locked` and `unlocked`; rendering ad components when relevant.
117+
118+
```javascript
119+
window.addEventListener("unlockProtocol", function(e) {
120+
var state = e.detail;
121+
122+
if (state === "locked") {
123+
// load ad rendering component here
124+
} else {
125+
// current visitor is a member, do not load ads!
126+
}
127+
});
128+
```
129+
130+
While some tailoring may be required for your specific use case, this should provide a starting point towards utilizing the Unlock Protocol to provide your members with an Ad Free experience.
131+
132+
### Locking Media Content
133+
134+
We use the same approach again to lock media content (images, videos... etc). Here is an example of how to achieve this. We want to lock up access to [this video](https://ia801602.us.archive.org/11/items/Rick_Astley_Never_Gonna_Give_You_Up/Rick_Astley_Never_Gonna_Give_You_Up.mp4).
135+
136+
HTML5 actually makes it very easy to embed any video in a document. Here's what it takes:
137+
138+
```markup
139+
<video controls>
140+
<source src="https://ia801602.us.archive.org/11/items/Rick_Astley_Never_Gonna_Give_You_Up/Rick_Astley_Never_Gonna_Give_You_Up.mp4" type="video/mp4">
141+
</video>
142+
```
143+
144+
For any web page which includes a lock, we use the same approach. First, we load the Paywall script, and then, we set the configuration for it (see above).
145+
146+
JavaScript provides us with an API to control the video. We can use that to lock its access. The following sample provides details of how this can work. Note there are multiple ways of doing this; feel free to tinker around!
147+
148+
```bash
149+
<script>
150+
(function() {
151+
// Set a few default variables.
152+
let isUnlocked = true;
153+
const previewTime = 30;
154+
155+
window.addEventListener('unlockProtocol.status', function(e) {
156+
var unlock = e.detail
157+
isUnlocked = (unlock.state === 'unlocked')
158+
// Optional: call video.play() to resume the video!
159+
})
160+
161+
162+
// This assumes there is a single video on the page. Otherwise use class selectors.
163+
const video = document.querySelector('video');
164+
video.addEventListener('timeupdate', (event) => {
165+
// This event gets triggered every time there is an update in the current time.
166+
// If the video is locked and the time is above previewTime seconds
167+
if (!isUnlocked && video.currentTime > previewTime) {
168+
// We stop the video go back to previewTime and pause the video
169+
video.currentTime = previewTime
170+
video.pause()
171+
// We ask the user to get a membership by loading the checkout UI
172+
unlockProtocol.loadCheckoutModal()
173+
}
174+
})
175+
})();
176+
</script>
177+
178+
```
179+
180+
After this, you are all done!
181+
182+
_**Note:** This tutorial implements a front-end locking approach, which is possible to circumvent; a determined actor could tinker with the JavaScript console of their web browser and inspect the code to find a workaround. It is absolutely possible to address this using an approach that is more difficult to circumvent, but that requires a back-end integration, which is more advanced and is outside the scope of this tutorial._

docusaurus.config.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,19 @@ const config = {
102102
},
103103
{
104104
from: "/tutorials/ad-free-experience",
105-
to: "/tutorials/front-end/ad-free-experience",
105+
to: "/tutorials/front-end/locking-page",
106+
},
107+
{
108+
from: "/tutorials/front-end/ad-free-experience",
109+
to: "/tutorials/front-end/locking-page",
110+
},
111+
{
112+
from: "/tutorials/front-end/locking-media-content",
113+
to: "/tutorials/front-end/locking-page"
106114
},
107115
{
108116
from: "/tutorials/locking-media-content",
109-
to: "/tutorials/front-end/locking-media-content",
117+
to: "/tutorials/front-end/locking-page",
110118
},
111119
{
112120
from: "/tutorials/react-example",
@@ -202,7 +210,7 @@ const config = {
202210
],
203211
sitemap_urls: [
204212
"https://docs.unlock-protocol.com/sitemap.xml"
205-
],
213+
],
206214
algolia: {
207215
appId: "J4FN2FD27Q",
208216
apiKey: "9bcefa2858ec26676689edd55f03fd26",
@@ -274,7 +282,7 @@ const config = {
274282
name: "twitter:image:alt",
275283
content: "Unlock logo with the word docs next to it",
276284
},
277-
{name: 'docsearch:docusaurus_tag', content: 'current'}
285+
{ name: 'docsearch:docusaurus_tag', content: 'current' }
278286
],
279287
navbar: {
280288
title: "Unlock",

sidebars.js

-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ const sidebars = {
118118
label: "Front-end",
119119
items: [
120120
"tutorials/front-end/locking-page",
121-
"tutorials/front-end/ad-free-experience",
122-
"tutorials/front-end/locking-media-content",
123121
"tutorials/front-end/react-example",
124122
"tutorials/front-end/scaffold-eth"
125123
]

static/_redirects

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@
3232
/tools/locksmith/authorization-strategy /tools/locksmith/
3333
/tools/paywall/advanced-paywall-configuration /tools/paywall
3434
/tools/paywall/locking-page /tutorials/front-end/locking-page
35-
/developers/tutorials/backend-locking-with-express.js /tutorials/back-end/backend-locking-with-express.js
35+
/developers/tutorials/backend-locking-with-express.js /tutorials/back-end/backend-locking-with-express.js
36+
/tutorials/ad-free-experience /tutorials/front-end/locking-page
37+
/tutorials/front-end/ad-free-experience /tutorials/front-end/locking-page
38+
/tutorials/front-end/locking-media-content /tutorials/front-end/locking-page
39+
/tutorials/locking-media-content /tutorials/front-end/locking-page

0 commit comments

Comments
 (0)