sync/www/assets/js/account.js

410 lines
13 KiB
JavaScript
Raw Normal View History

2013-06-04 22:26:17 +00:00
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2013-08-18 03:41:54 +00:00
/*
So, it turns out that $.post causes Firefox to use a GET request
on cross-site requests. What the hell? I'd understand if they just
made it error instead, but why give me chicken tenders if I ordered a
cheeseburger and act like everything's peachy?
*/
function postJSON(url, data, callback) {
$.ajax(url, {
method: "POST",
crossDomain: true,
data: data,
success: function (data) {
try {
data = data.substring(data.indexOf("{"));
data = data.substring(0, data.lastIndexOf("}") + 1);
data = JSON.parse(data);
callback(data);
} catch(e) {
return;
}
},
dataType: "text"
});
}
2013-06-19 21:54:27 +00:00
var uname = readCookie("cytube_uname") || "";
var session = readCookie("cytube_session") || "";
2013-05-29 15:49:09 +00:00
var loggedin = false;
if(uname && session) {
2013-08-12 03:10:55 +00:00
var data = {
name: uname,
session: session
};
2013-08-18 03:41:54 +00:00
postJSON(WEB_URL + "/api/login?callback=?", data, function (data) {
2013-08-18 19:21:42 +00:00
console.log(data);
2013-08-12 03:10:55 +00:00
if(data.success)
2013-05-29 15:49:09 +00:00
onLogin();
2013-08-18 19:21:42 +00:00
});
2013-05-29 15:49:09 +00:00
}
function onLogin() {
$("#cpwusername").val(uname);
$("#ceusername").val(uname);
$("#accountnav li")[0].innerHTML = "Logged in as " + uname;
$("#register").hide();
loggedin = true;
$("#login").text("Logout");
createCookie("cytube_uname", uname, 7);
createCookie("cytube_session", session, 7);
2013-05-29 15:49:09 +00:00
}
function makeTabCallback(tabid, paneid) {
return function() {
$("#accountnav li").each(function() {
$(this).removeClass("active");
});
$(tabid).parent().addClass("active");
$(".span7").each(function() {
$(this).css("display", "none");
});
$(paneid).css("display", "");
};
}
$("#register").click(makeTabCallback("#register", "#registerpane"));
$("#pwchange").click(makeTabCallback("#pwchange", "#changepwpane"));
$("#pwreset").click(makeTabCallback("#pwreset", "#pwresetpane"));
2013-05-29 15:49:09 +00:00
$("#email").click(makeTabCallback("#email", "#changeemailpane"));
2013-05-30 01:04:22 +00:00
$("#profile").click(makeTabCallback("#profile", "#profilepane"));
$("#profile").click(function() {
if(uname != "") {
2013-08-12 03:10:55 +00:00
$.getJSON(WEB_URL+"/api/users/"+uname+"/profile?callback=?",
function (data) {
2013-05-30 01:04:22 +00:00
if(data.success) {
$("#profiletext").val(data.profile_text);
$("#profileimg").val(data.profile_image);
}
else {
$("<div/>").addClass("alert alert-error")
.text("Failed to retrieve profile: " + data.error)
.insertBefore($("#profilepane form"));
}
});
}
});
$("#channels").click(makeTabCallback("#channels", "#channelspane"));
$("#channels").click(function () {
if(!loggedin) {
var error = $("<div/>").addClass("alert alert-error")
.text("You must be logged in to view this page")
.insertBefore($("#channellist"));
$("<button/>").addClass("close pull-right").click(function () {
error.remove();
}).html("&times;").prependTo(error);
return;
}
2013-08-12 03:10:55 +00:00
var auth = "name=" + encodeURIComponent(uname) + "&session=" +
encodeURIComponent(session);
$.getJSON(WEB_URL+"/api/account/mychannels?"+auth+"&callback=?",
function (data) {
$("#channellist tbody").remove();
data.channels.forEach(function (chan) {
var tr = $("<tr/>").appendTo($("#channellist"));
var td = $("<td/>").appendTo(tr);
$("<a/>").attr("href", "./r/" + chan.name)
.attr("target", "_blank")
.text(chan.name)
.appendTo(td);
});
});
});
2013-05-29 15:49:09 +00:00
$("#registerbtn").click(function() {
$("#registerpane").find(".alert-error").remove();
$("#registerpane").find(".error").removeClass("error");
var name = $("#regusername").val();
2013-05-29 15:49:09 +00:00
var pw = $("#regpw").val();
var pwc = $("#regpwconfirm").val();
var err = false;
if(!name.match(/^[a-z0-9_]{1,20}$/i)) {
2013-05-29 15:49:09 +00:00
$("<div/>").addClass("alert alert-error")
.text("Usernames must be 1-20 characters long and contain only a-z, 0-9, and underscores")
.insertAfter($("#regusername").parent().parent());
err = true;
}
if(pw == "") {
$("<div/>").addClass("alert alert-error")
.text("Password must not be blank")
.insertAfter($("#regpw").parent().parent());
$("#regpw").parent().parent().addClass("error");
err = true;
}
if(pw != pwc) {
$("<div/>").addClass("alert alert-error")
.text("Passwords do not match")
.insertAfter($("#regpwconfirm").parent().parent());
$("#regpwconfirm").parent().parent().addClass("error");
err = true;
}
if(err) {
return;
}
// Input valid, try registering
2013-08-12 03:10:55 +00:00
var data = {
name: name,
pw: pw
};
2013-08-18 03:41:54 +00:00
postJSON(WEB_URL + "/api/register?callback=?", data, function (data) {
if(data.success) {
uname = name;
session = data.session;
onLogin();
$("<div/>").addClass("alert alert-success")
.text("Registration successful")
.insertBefore($("#registerpane form"));
$("#regpw").val("");
$("#regusername").val("");
}
else {
$("<div/>").addClass("alert alert-error")
.text(data.error)
.insertBefore($("#registerpane form"));
}
2013-08-18 19:21:42 +00:00
});
2013-05-29 15:49:09 +00:00
});
$("#loginbtn").click(function() {
$("#loginpane").find(".alert-error").remove();
$("#loginpane").find(".alert-success").remove();
if($("#loginpw").val() == "") {
$("<div/>").addClass("alert alert-error")
.text("Please provide a password")
.insertAfter($("#loginpw").parent().parent());
$("#loginpw").parent().parent().addClass("error");
return;
}
uname = $("#loginusername").val();
2013-08-12 03:10:55 +00:00
var data = {
name: uname,
pw: $("#loginpw").val()
};
2013-08-18 03:41:54 +00:00
postJSON(WEB_URL+"/api/login?callback=?", data, function(data) {
2013-05-29 15:49:09 +00:00
if(data.success) {
session = data.session;
onLogin();
$("<div/>").addClass("alert alert-success")
.text("Login successful")
.insertBefore($("#loginpane form"));
$("#loginpw").val("");
$("#loginusername").val("");
}
else {
$("<div/>").addClass("alert alert-error")
.text(data.error)
.insertBefore($("#loginpane form"));
}
2013-08-18 19:21:42 +00:00
});
2013-05-29 15:49:09 +00:00
});
$("#cpwbtn").click(function() {
$("#changepwpane").find(".alert-error").remove();
$("#changepwpane").find(".alert-success").remove();
$("#changepwpane").find(".error").removeClass("error");
var name = $("#cpwusername").val();
var oldpw = $("#cpwoldpw").val();
var newpw = $("#cpwnewpw").val();
var newpwc = $("#cpwconfirm").val();
var err = false;
if(oldpw == "") {
$("<div/>").addClass("alert alert-error")
.text("Password must not be empty")
.insertAfter($("#cpwoldpw").parent().parent());
$("#cpwoldpw").parent().parent().addClass("error");
err = true;
}
if(newpw == "") {
$("<div/>").addClass("alert alert-error")
.text("Password must not be empty")
.insertAfter($("#cpwnewpw").parent().parent());
$("#cpwnewpw").parent().parent().addClass("error");
err = true;
}
if(newpw != newpwc) {
$("<div/>").addClass("alert alert-error")
.text("Passwords do not match")
.insertAfter($("#cpwconfirm").parent().parent());
$("#cpwconfirm").parent().parent().addClass("error");
err = true;
}
if(err) {
return;
}
// Input valid, try changing password
2013-08-12 03:10:55 +00:00
var data = {
name: name,
oldpw: oldpw,
newpw: newpw
};
2013-08-18 03:41:54 +00:00
postJSON(WEB_URL + "/api/account/passwordchange?callback=?", data,
2013-08-12 03:10:55 +00:00
function (data) {
2013-05-29 15:49:09 +00:00
if(data.success) {
$("<div/>").addClass("alert alert-success")
.text("Password changed.")
.insertBefore($("#changepwpane form"));
uname = name;
session = data.session;
onLogin();
}
else {
$("<div/>").addClass("alert alert-error")
.text(data.error)
.insertBefore($("#changepwpane form"));
}
2013-08-18 19:21:42 +00:00
});
2013-05-29 15:49:09 +00:00
});
$("#cebtn").click(function() {
$("#changeemailpane").find(".alert-error").remove();
$("#changeemailpane").find(".alert-success").remove();
var name = $("#ceusername").val();
var pw = $("#cepw").val();
var email = $("#ceemail").val();
if(pw == "") {
$("<div/>").addClass("alert alert-error")
.text("Please provide a password")
.insertAfter($("#cepw").parent().parent());
$("#cepw").parent().parent().addClass("error");
return;
}
2013-08-12 03:10:55 +00:00
if(!email.match(/^[\w_\.]+@[\w_\.]+[a-zA-Z]+$/)) {
2013-05-29 15:49:09 +00:00
$("<div/>").addClass("alert alert-error")
.text("Invalid email")
.insertAfter($("#ceemail").parent().parent());
$("#ceemail").parent().parent().addClass("error");
return;
}
if(email.match(/.*@(localhost|127\.0\.0\.1)/i)) {
$("<div/>").addClass("alert alert-error")
.text("Nice try, but no.")
.insertAfter($("#ceemail").parent().parent());
$("#ceemail").parent().parent().addClass("error");
return;
}
2013-08-12 03:10:55 +00:00
var data = {
name: name,
pw: pw,
email: email
};
2013-08-18 03:41:54 +00:00
postJSON(WEB_URL + "/api/account/email?callback=?", data,
2013-08-12 03:10:55 +00:00
function (data) {
2013-05-29 15:49:09 +00:00
if(data.success) {
$("<div/>").addClass("alert alert-success")
.text("Email updated")
.insertBefore($("#changeemailpane form"));
uname = name;
session = data.session;
onLogin();
}
else {
$("<div/>").addClass("alert alert-error")
.text(data.error)
.insertBefore($("#changeemailpane form"));
}
2013-08-18 19:21:42 +00:00
});
2013-05-29 15:49:09 +00:00
});
$("#rpbtn").click(function() {
2013-05-30 18:07:29 +00:00
$("#rpbtn").text("Sending...");
$("#pwresetpane").find(".alert-error").remove();
$("#pwresetpane").find(".alert-success").remove();
var name = $("#rpusername").val();
var email = $("#rpemail").val();
2013-08-12 03:10:55 +00:00
var data = {
name: name,
email: email
};
2013-08-18 03:41:54 +00:00
postJSON(WEB_URL + "/api/account/passwordreset?callback=?", data,
2013-08-12 03:10:55 +00:00
function (data) {
2013-05-30 18:07:29 +00:00
$("#rpbtn").text("Send Reset");
if(data.success) {
$("<div/>").addClass("alert alert-success")
.text("Password reset link issued. Check your email.")
.insertBefore($("#pwresetpane form"));
}
else {
$("<div/>").addClass("alert alert-error")
.text(data.error)
.insertBefore($("#pwresetpane form"));
}
2013-08-18 19:21:42 +00:00
});
});
2013-05-30 01:04:22 +00:00
$("#profilesave").click(function() {
$("#profilepane").find(".alert-error").remove();
$("#profilepane").find(".alert-success").remove();
var img = $("#profileimg").val();
2013-08-12 03:10:55 +00:00
var text = $("#profiletext").val();
var data = {
name: uname,
session: session,
profile_image: img,
profile_text: text
};
2013-08-18 03:41:54 +00:00
postJSON(WEB_URL+"/api/account/profile?callback=?", data,
2013-08-12 03:10:55 +00:00
function (data) {
2013-05-30 01:04:22 +00:00
if(data.success) {
$("<div/>").addClass("alert alert-success")
.text("Profile updated.")
.insertBefore($("#profilepane form"));
}
else {
$("<div/>").addClass("alert alert-error")
.text(data.error)
.insertBefore($("#profilepane form"));
}
2013-08-18 19:21:42 +00:00
});
2013-05-30 01:04:22 +00:00
});
2013-05-29 15:49:09 +00:00
$("#login").click(function() {
if(!loggedin) {
makeTabCallback("#login", "#loginpane")();
}
else {
uname = "";
session = "";
eraseCookie("cytube_uname");
eraseCookie("cytube_session");
2013-05-29 15:49:09 +00:00
$("#accountnav li")[0].innerHTML = "Not Logged In";
$("#register").show();
$("#login").text("Login");
loggedin = false;
}
});