diff --git a/public/js/chrome/login.js b/public/js/chrome/login.js index b1d4d1e7..472306cd 100644 --- a/public/js/chrome/login.js +++ b/public/js/chrome/login.js @@ -1,80 +1,46 @@ -/* globals $ */ -(function (window) { - 'use strict'; +var $username = $('#username'), + $password = $('#password'), + $email = $('#email'); - var pushState = window.history.pushState ? function (url) { - window.history.pushState(null, null, url); - } : false; - var $forms = $('.form-container form'); - var $info = $('.form-container .info'); +var $loginForm = $('form.login').submit(function (event) { + event.preventDefault(); - function currentForm () { - return this.pathname === window.location.pathname; - } + var form = $(this), + name = form.find('input[name=username]').val(), + key = form.find('input[name=password]').val(), + email = form.find('input[name=email]').val(), + $loginFeedback = form.find('.loginFeedback'); - function handlePopstateChanges() { - $tabs.filter(currentForm).trigger('click', [true]); - } - function matchActionAttrTo (path) { - return function(){ - return $(this).attr('action') === path; - }; - } + // jsbin.settings.home = name; // will save later + $loginFeedback.show().text('Checking...'); - var $tabs = $('.tab').click(function (event, fromPopstate) { - if (!pushState && !fromPopstate) { - return; - } - - var path = event.target.pathname || event.target.parentNode.pathname; - - $forms - .hide() - .filter(matchActionAttrTo(path)) // One element now ↓ - .show(); - - // fromPopstate is true when we call click in handlePopstateChanges - // If a user navigated back, to register, it would then set pushState - // to register, leaving teh user stuck on that page. - if (!fromPopstate && pushState) { - event.preventDefault(); - pushState(path); - } - - $('.form-container') - .removeClass('register login') - .addClass($tabs.filter(currentForm)[0].classList[1]); - - $info.empty(); - - }); - // Kick it all off with initial event handlers - - window.addEventListener('popstate', handlePopstateChanges); - handlePopstateChanges(); - - $forms.on('submit', function(event) { - event.preventDefault(); - var $form = $(event.target); - - var data = $form.serializeArray().reduce(function(obj, item) { - return obj[item.name] = item.value, obj; - }, {}); - - $.ajax({ - url: 'https://jsbin.dev' + $form.attr('action'), - type: $form.attr('method'), - data: data, - dataType: 'json', - success: function(res) { - window.location.href = res; - }, - error: function(res) { - $info.text(res.responseText); + $.ajax({ + url: jsbin.root + '/sethome', + data: { name: name, key: key, email: email }, + type: 'post', + dataType: 'json', + complete: function (jqXHR) { + var data = $.parseJSON(jqXHR.responseText) || {}; + // cookie is required to share with the server so we can do a redirect on new bin + if (data.ok) { + if (data.avatar) { + $('#avatar').find('img').remove().end().prepend(''); + } + if (data.message) { + $loginFeedback.text(data.message); + } else { + window.location = window.location.pathname + window.location.search; + } + } else { + analytics.login(false); + $loginFeedback.text(data.message || ('"' + name + '" has already been taken. Please either double check the password, or choose another username.')); } - }); - + } }); +}); -}(window)); \ No newline at end of file +// if ($('#homebtn').length) { +// jsbin.settings.home = document.cookie.split('home=')[1].split(';')[0]; +// document.title = jsbin.settings.home + '@' + document.title; +// } \ No newline at end of file diff --git a/public/js/login.js b/public/js/login.js new file mode 100644 index 00000000..b1d4d1e7 --- /dev/null +++ b/public/js/login.js @@ -0,0 +1,80 @@ +/* globals $ */ +(function (window) { + 'use strict'; + + var pushState = window.history.pushState ? function (url) { + window.history.pushState(null, null, url); + } : false; + var $forms = $('.form-container form'); + var $info = $('.form-container .info'); + + function currentForm () { + return this.pathname === window.location.pathname; + } + + function handlePopstateChanges() { + $tabs.filter(currentForm).trigger('click', [true]); + } + + function matchActionAttrTo (path) { + return function(){ + return $(this).attr('action') === path; + }; + } + + var $tabs = $('.tab').click(function (event, fromPopstate) { + if (!pushState && !fromPopstate) { + return; + } + + var path = event.target.pathname || event.target.parentNode.pathname; + + $forms + .hide() + .filter(matchActionAttrTo(path)) // One element now ↓ + .show(); + + // fromPopstate is true when we call click in handlePopstateChanges + // If a user navigated back, to register, it would then set pushState + // to register, leaving teh user stuck on that page. + if (!fromPopstate && pushState) { + event.preventDefault(); + pushState(path); + } + + $('.form-container') + .removeClass('register login') + .addClass($tabs.filter(currentForm)[0].classList[1]); + + $info.empty(); + + }); + // Kick it all off with initial event handlers + + window.addEventListener('popstate', handlePopstateChanges); + handlePopstateChanges(); + + $forms.on('submit', function(event) { + event.preventDefault(); + var $form = $(event.target); + + var data = $form.serializeArray().reduce(function(obj, item) { + return obj[item.name] = item.value, obj; + }, {}); + + $.ajax({ + url: 'https://jsbin.dev' + $form.attr('action'), + type: $form.attr('method'), + data: data, + dataType: 'json', + success: function(res) { + window.location.href = res; + }, + error: function(res) { + $info.text(res.responseText); + } + }); + + }); + +}(window)); \ No newline at end of file diff --git a/views/register-login.html b/views/register-login.html index 6e50febd..2544c95a 100644 --- a/views/register-login.html +++ b/views/register-login.html @@ -52,4 +52,4 @@ - +