-
-
Save ezekg/0f87abd27e60be3ffeef02c7c2829319 to your computer and use it in GitHub Desktop.
Example of using Keygen's licensing and distribution APIs together to only load an app's main logic for licensed users. The example below uses Keygen's demo account and is fully functional. Download the HTML file and open it in a browser.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copy this snippet below and paste into a terminal to download the HTML file | |
# and open it in your default browser | |
tmp=$(mktemp /tmp/keygen-XXXXXX) && \ | |
curl -o "$tmp" https://gist.githubusercontent.com/ezekg/0f87abd27e60be3ffeef02c7c2829319/raw/97b71eb58dcd85383a4c8fccb8ce38c887e0eacb/index.html && \ | |
open "$tmp" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the protected app logic that we're conditionally loading | |
// *only* for licensed users | |
alert('Hello world!') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
const KEYGEN_ACCOUNT_ID = '1fddcec8-8dd3-4d8d-9b16-215cac0f9b52' // Available at https://app.keygen.sh/settings | |
const KEYGEN_PRODUCT_ID = '6962277c-e729-4bf2-96f1-edc98255675c' | |
async function activateAppWithValidLicenseKey(key) { | |
const res = await fetch(`https://api.keygen.sh/v1/accounts/${KEYGEN_ACCOUNT_ID}/licenses/actions/validate-key`, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/vnd.api+json', | |
'Accept': 'application/vnd.api+json' | |
}, | |
body: JSON.stringify({ | |
meta: { | |
scope: { product: KEYGEN_PRODUCT_ID }, | |
key | |
} | |
}) | |
}) | |
const { meta } = await res.json() | |
if (meta.valid) { | |
// Whenever the license key is valid, request our protected script using the license key | |
const script = document.createElement('script') | |
script.src = `https://dist.keygen.sh/v1/${KEYGEN_ACCOUNT_ID}/${KEYGEN_PRODUCT_ID}/releases/js/app.js?key=${key}` | |
// Append the script to the body (which will run it) | |
document.body.appendChild(script) | |
} else { | |
alert(`Your license is invalid: ${meta.detail}`) | |
// Ask the user if they'd like to retry inputting their license key | |
if (confirm('Do you want to try again?')) { | |
await boopApp() | |
} | |
} | |
} | |
async function boopApp() { | |
// Prompt the user for their license key - to demo a valid license | |
// key, enter: test-key | |
const key = prompt('Please enter your license key:') | |
// Attempt to activate the app by validating the license key | |
await activateAppWithValidLicenseKey(key) | |
} | |
boopApp() | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment