Skip to content

Instantly share code, notes, and snippets.

@GertSallaerts
Forked from Sitebase/bbbx-login-stack.js
Last active August 29, 2015 14:10
Show Gist options
  • Save GertSallaerts/41dc4c4f806d3d5858d9 to your computer and use it in GitHub Desktop.
Save GertSallaerts/41dc4c4f806d3d5858d9 to your computer and use it in GitHub Desktop.

Revisions

  1. @gertt gertt revised this gist Nov 21, 2014. 1 changed file with 45 additions and 17 deletions.
    62 changes: 45 additions & 17 deletions bbbx-login-stack.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@
    * BuboBox form callback module
    *
    * @author Wim Mostmans ([email protected])
    * @contributor Gert Sallaerts ([email protected])
    *
    * @license BSD
    *
    * Copyright (c) 2014 BuboBox
    @@ -40,33 +42,59 @@ window.bbbx_modules.push(function(sandbox, $) {
    function verify( e ) {

    // Get credentials from the form
    var credentials = $('#' + formId).serialize();
    var credentials = $('#' + formId).serialize(),
    username = $('#' + formId + ' input[name=username]').val();

    // Set button to grey to give the user a visual cue that something is happening
    e.origin.addClass('bbbx-button-grey');

    $.post(settings.endpoint, credentials).done(function( response ) {
    sandbox.api.validation.custom(
    // The data you want to validate
    { username: username },
    // The rules to apply
    { username: [ 'required', 'unique_field' ] },

    // Callback
    function (response) {

    if (response.valid) {
    // If the username has not yet participated, we will try to authenticate

    $.post(settings.endpoint, credentials).done(function( response ) {

    // Enable login button again
    e.origin.removeClass('bbbx-button-grey');

    // Enable login button again
    e.origin.removeClass('bbbx-button-grey');
    if( response.status === 'error' ) {
    hasError( true );
    $('#' + formId + ' #bbbx-username').focus(); // Set focus on the input field
    } else {
    hasError( false );

    if( response.status === 'error' ) {
    hasError( true );
    $('#' + formId + ' #bbbx-username').focus(); // Set focus on the input field
    } else {
    hasError( false );
    // If response contains meta add it to the current user
    if( response.hasOwnProperty( 'meta' ) ) {
    for( key in response.meta ) {
    sandbox.setMeta( key, response.meta[ key ] );
    }
    }

    // If response contains meta add it to the current user
    if( response.hasOwnProperty( 'meta' ) ) {
    for( key in response.meta ) {
    sandbox.setMeta( key, response.meta[ key ] );
    }
    sandbox.publish('stack.next', {origin: $('#' + formId)});
    }

    });

    } else {
    // The username has already participated, we will not try to authenticate
    hasError(true);
    }

    },

    sandbox.publish('stack.next', {origin: $('#' + formId)});
    // Error handler (if something goes wrong with the API request)
    function (response) {
    console.error(response);
    }

    });
    );
    }

    function hasError( error ) {
  2. @Sitebase Sitebase revised this gist Nov 13, 2014. 2 changed files with 24 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions bbbx-login-stack.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    <?php

    // This is just a very simple example but this can ofcourse be replaced with a call to
    // your own API/Database to do the user validation

    $username = isset( $_POST['username'] ) ? $_POST['username'] : null;
    $password = isset( $_POST['password'] ) ? $_POST['password'] : null;

    if( $username === 'admin' && $password === 'test' ) {
    $response = array('status' => 'ok', 'meta' => array('username' => $username, 'custommeta' => 'myvalue'));
    } else {
    $response = array('status' => 'error');
    }

    header('Content-Type: application/json');
    echo json_encode( $response );
    8 changes: 8 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <html>
    <head>
    <script src="bbbx-login-stack.js" data-endpoint="bbbx-login-stack.php" data-intro="This is a super awesome login form. Use your BuboBox credentials to login."></script>
    </head>
    <body>
    <!-- your BuboBox widget snippet -->
    </body>
    </html>
  3. @Sitebase Sitebase created this gist Nov 13, 2014.
    131 changes: 131 additions & 0 deletions bbbx-login-stack.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,131 @@
    /**
    * BuboBox form callback module
    *
    * @author Wim Mostmans ([email protected])
    * @license BSD
    *
    * Copyright (c) 2014 BuboBox
    */

    window.bbbx_modules = window.bbbx_modules || []; // Make modules array if not yet exists
    window.bbbx_modules.push(function(sandbox, $) {

    var NAME = 'bbbx-login-stack';
    var settings = $(bbbx_login_stack_tag).data();
    var formId = 'bbbx-login-stack-form';

    /**
    * Add a stack item to the recorder/player popup
    * name can be pre/post or name of a stack item + pre/post
    * For example "form.pre" or "terms.post"
    */
    function addItem( e ) {

    // Check if intro text is configured
    var intro = '';
    if( settings.hasOwnProperty('intro') && settings.intro !== '' ) {
    intro = '<p>' + settings.intro + '</p><hr />';
    }

    e.stack.itemReady({
    name: 'pre',
    view: {
    title: 'Login',
    body: intro + '<form id="' + formId + '"><label for="username">Username</label><input type="text" name="username" id="bbbx-username" value=""><label for="password">Password</label><input type="password" name="password" id="bbbx-password" value=""></form>',
    footer: '<button class="bbbx-button" data-bbbx-trigger="bbbx-login-stack.login">Login</button>'
    }
    });
    }

    function verify( e ) {

    // Get credentials from the form
    var credentials = $('#' + formId).serialize();

    // Set button to grey to give the user a visual cue that something is happening
    e.origin.addClass('bbbx-button-grey');

    $.post(settings.endpoint, credentials).done(function( response ) {

    // Enable login button again
    e.origin.removeClass('bbbx-button-grey');

    if( response.status === 'error' ) {
    hasError( true );
    $('#' + formId + ' #bbbx-username').focus(); // Set focus on the input field
    } else {
    hasError( false );

    // If response contains meta add it to the current user
    if( response.hasOwnProperty( 'meta' ) ) {
    for( key in response.meta ) {
    sandbox.setMeta( key, response.meta[ key ] );
    }
    }

    sandbox.publish('stack.next', {origin: $('#' + formId)});
    }

    });
    }

    function hasError( error ) {
    var inputs = $('#' + formId).find('input');
    if( error === true ) {
    inputs.addClass('bbbx-error');
    } else {
    inputs.removeClass('bbbx-error');
    }
    }

    var exports = {
    NAME: NAME,

    /**
    * Module is registered inside the BuboBox widget
    */
    init: function() {
    console.error('Custom module initialized');
    },

    /**
    * All modules are loaded
    */
    ready: function() {
    console.error('Custom module ready, all other modules are also loaded at this point');
    },

    /**
    * Add listeners for certain actions that happen
    * in the BuboBox widgets
    */
    bind: function() {
    sandbox.subscribe('stack.add.pre', addItem);
    sandbox.subscribe('bbbx-login-stack.login', verify);
    },

    /**
    * Remove listeners we create in the bind function
    */
    unbind: function() {
    sandbox.unsubscribe(['stack.add.pre', addItem]);
    sandbox.unsubscribe(['bbbx-login-stack.login', verify]);
    },

    /**
    * Clean up all stuff this module has created
    * - DOM elements
    * - Bindings
    */
    destroy: function() {
    this.unbind();
    // remove some left over DOM elments
    }
    };

    return exports;

    });

    // Store the current script tag reference
    var bbbx_login_stack_tag = document.currentScript;