Merge pull request #369 from thelounge/xpaw/fix-losing-auth

Do not lose authentication token when the connection gets lost
This commit is contained in:
Jérémie Astori 2016-06-18 22:35:12 -04:00 committed by GitHub
commit 75c578c02c
2 changed files with 15 additions and 11 deletions

View File

@ -74,28 +74,32 @@ $(function() {
}); });
}); });
socket.on("auth", function(/* data */) { socket.on("auth", function(data) {
var body = $("body"); var body = $("body");
var login = $("#sign-in"); var login = $("#sign-in");
if (!login.length) { if (!login.length) {
refresh(); refresh();
return; return;
} }
login.find(".btn").prop("disabled", false); login.find(".btn").prop("disabled", false);
var token = window.localStorage.getItem("token");
if (token) { if (!data.success) {
body.addClass("signed-out");
window.localStorage.removeItem("token"); window.localStorage.removeItem("token");
socket.emit("auth", {token: token});
}
if (body.hasClass("signed-out")) {
var error = login.find(".error"); var error = login.find(".error");
error.show().closest("form").one("submit", function() { error.show().closest("form").one("submit", function() {
error.hide(); error.hide();
}); });
} else {
var token = window.localStorage.getItem("token");
if (token) {
socket.emit("auth", {token: token});
}
} }
if (!token) {
body.addClass("signed-out");
}
var input = login.find("input[name='user']"); var input = login.find("input[name='user']");
if (input.val() === "") { if (input.val() === "") {
input.val(window.localStorage.getItem("user") || ""); input.val(window.localStorage.getItem("user") || "");

View File

@ -106,7 +106,7 @@ function index(req, res, next) {
function init(socket, client) { function init(socket, client) {
if (!client) { if (!client) {
socket.emit("auth"); socket.emit("auth", {success: true});
socket.on("auth", auth); socket.on("auth", auth);
} else { } else {
socket.on( socket.on(
@ -251,7 +251,7 @@ function auth(data) {
} }
}); });
if (!success) { if (!success) {
socket.emit("auth"); socket.emit("auth", {success: success});
} }
} }
} }