Do not lose authentication token when the connection gets lost

This commit is contained in:
Pavel Djundik 2016-06-01 00:02:10 +03:00
parent 2bf6a2595b
commit 2008abc0e8
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

@ -111,7 +111,7 @@ function index(req, res, next) {
function init(socket, client, token) { function init(socket, client, token) {
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(
@ -255,7 +255,7 @@ function auth(data) {
} }
}); });
if (!success) { if (!success) {
socket.emit("auth"); socket.emit("auth", {success: success});
} }
} }
} }