Reinstating old login code

This commit is contained in:
Fabien O'Carroll 2014-02-17 17:50:29 +00:00
parent fc6a8a491d
commit dfbd4d8aca
3 changed files with 119 additions and 73 deletions

View File

@ -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('<img class="avatar" src="' + data.avatar + '">');
}
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));
// if ($('#homebtn').length) {
// jsbin.settings.home = document.cookie.split('home=')[1].split(';')[0];
// document.title = jsbin.settings.home + '@' + document.title;
// }

80
public/js/login.js Normal file
View File

@ -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));

View File

@ -52,4 +52,4 @@
<script src="/js/vendor/jquery.js"></script>
<script src="/js/chrome/login.js"></script>
<script src="/js/login.js"></script>