Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/views/pages/paid-gate.ejs

    r156baa3 r9a00f28  
    3131
    3232<script src="/assets/vendor/simplewebauthn-browser.umd.min.js"></script>
    33 <%# Het script staat in assets/js/mod/paid-gate.js; de gegevens via partials/page-data.ejs (shaer-bqr). %>
     33<script>
     34(function () {
     35  var base = "<%= (typeof siteUrlBase !== 'undefined' && siteUrlBase ? siteUrlBase : '') %>";
     36  var slug = "<%= pgSlug %>";
     37  var hasPatron = <%= _hasPatron ? 'true' : 'false' %>;
     38  var I = { join: "<%= t('pgate.join_short') %>", confirm: "<%= t('pgate.confirm') %>", failed: "<%= t('pgate.failed') %>", error: "<%= t('pgate.error') %>" };
     39  var btn = document.getElementById('pg-unlock');
     40  var status = document.getElementById('pg-status');
     41  function say(msg, err) { status.hidden = false; status.textContent = msg; status.classList.toggle('is-err', !!err); }
     42  function toLink() { location.href = base + '/paid/link?post=' + encodeURIComponent(slug); }
     43
     44  // No WebAuthn here: an assertion is impossible. With a Patreon page there is
     45  // already a "Word supporter" button, so hide the (dead) unlock button rather
     46  // than turn it into a second "Word supporter". Without one, this IS the button.
     47  if (!window.SimpleWebAuthnBrowser || !window.PublicKeyCredential) {
     48    if (hasPatron) { btn.style.display = 'none'; }
     49    else { btn.textContent = I.join; btn.addEventListener('click', toLink); }
     50    return;
     51  }
     52
     53  btn.addEventListener('click', function () {
     54    btn.disabled = true;
     55    say(I.confirm);
     56    fetch(base + '/paid/challenge?post=' + encodeURIComponent(slug))
     57      .then(function (r) { if (!r.ok) throw { link: true }; return r.json(); })
     58      .then(function (data) {
     59        return window.SimpleWebAuthnBrowser.startAuthentication({ optionsJSON: data.options })
     60          .then(function (response) {
     61            return fetch(base + '/paid/unlock', {
     62              method: 'POST', headers: { 'Content-Type': 'application/json' },
     63              body: JSON.stringify({ response: response, blob: data.blob }),
     64            });
     65          });
     66      })
     67      .then(function (r) { return r.json().then(function (j) { return { status: r.status, j: j }; }); })
     68      .then(function (res) {
     69        if (res.j && res.j.ok && res.j.redirect) {
     70          // Reload the real post page via the one-shot unlock capability, so it
     71          // renders through its normal template (layout, styles, audio).
     72          location.href = res.j.redirect;
     73        } else if (res.status === 403) {
     74          toLink();   // no valid passkey yet (or lapsed tier): link via Patreon
     75        } else {
     76          btn.disabled = false; say(I.failed, true);
     77        }
     78      })
     79      .catch(function (e) {
     80        if (e && e.link) { toLink(); return; }
     81        if (e && e.name === 'NotAllowedError') { toLink(); return; }   // cancelled / no passkey -> link
     82        btn.disabled = false; say(I.error, true);
     83      });
     84  });
     85})();
     86</script>
    3487
    3588<style>
     
    52105  .pg-status.is-err { color: #c0392b; }
    53106</style>
    54 <%- include('../partials/page-data', { pageData: { base: (typeof siteUrlBase !== 'undefined' && siteUrlBase ? siteUrlBase : ''), slug: pgSlug, hasPatron: !!_hasPatron, i18n: { join: t('pgate.join_short'), confirm: t('pgate.confirm'), failed: t('pgate.failed'), error: t('pgate.error') } } }) %>
Note: See TracChangeset for help on using the changeset viewer.