Skip to content

Instantly share code, notes, and snippets.

@BitPuffin
Last active December 25, 2015 06:59

Revisions

  1. BitPuffin revised this gist Oct 11, 2013. 1 changed file with 55 additions and 0 deletions.
    55 changes: 55 additions & 0 deletions gistfile3.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@

    function getCookie(name) {
    var parts = document.cookie.split(name + "=");
    if (parts.length == 2) return parts.pop().split(";").shift();
    }

    var currentUser = getCookie("useremail") || null

    function simpleXhrSentinel(xhr) {
    return function() {
    if (xhr.readyState == 4) {
    if (xhr.status == 200){
    // reload page to reflect new login state
    window.location.reload();
    }
    else {
    navigator.id.logout();
    alert("XMLHttpRequest error: " + xhr.status);
    }
    }
    }
    }

    function verifyAssertion(assertion) {
    // Your backend must return HTTP status code 200 to indicate successful
    // verification of user's email address and it must arrange for the binding
    // of currentUser to said address when the page is reloaded
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/sign-in", true);
    // see http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
    var param = "assertion="+assertion;
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Content-length", param.length);
    xhr.setRequestHeader("Connection", "close");
    xhr.send(param); // for verification by your backend

    xhr.onreadystatechange = simpleXhrSentinel(xhr);
    }

    function signoutUser() {
    // Your backend must return HTTP status code 200 to indicate successful
    // sign out (usually the resetting of one or more session variables) and
    // it must arrange for the binding of currentUser to 'null' when the page
    // is reloaded
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "/sign-out", true);
    xhr.send(null);
    xhr.onreadystatechange = simpleXhrSentinel(xhr);
    }

    navigator.id.watch({
    loggedInUser: currentUser,
    onlogin: verifyAssertion,
    onlogout: signoutUser
    });
  2. BitPuffin revised this gist Oct 11, 2013. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions gistfile2.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #! stdtmpl
    # proc htmlLayout(request: TRequest, content, title: string): string =
    # result = ""
    <!DOCTYPE html>
    <html>
    <head>
    <title>${title}</title>
    <script src="https://login.persona.org/include.js"></script>
    <script src="/js/personabuttons.js"></script>
    <script src="/js/personawatcher.js"></script>
    </head>
    <body>
    <h1>Personal Site</h1>
    ${content}
    <a href="#" id="signin"><img src="https://developer.mozilla.org/files/3969/plain_sign_in_blue.png"/></a>
    <a href="#" id="signout">sign out</a>
    </body>
    </html>
  3. BitPuffin created this gist Oct 11, 2013.
    10 changes: 10 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    var signinLink = document.getElementById('signin')
    var signoutLink = document.getElementById('signout')

    if (signinLink) {
    signinLink.onclick = function() { alert("Time to sign in!"); navigator.id.request(); }
    }

    if (signoutLink) {
    signoutLink.onclick = function() { navigator.id.logout(); }
    }