Skip to content

Commit

Permalink
Merge pull request thelounge#369 from thelounge/xpaw/fix-losing-auth
Browse files Browse the repository at this point in the history
Do not lose authentication token when the connection gets lost
  • Loading branch information
astorije authored Jun 19, 2016
2 parents 7e3345a + e4316f3 commit 6458427
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
22 changes: 13 additions & 9 deletions client/js/lounge.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,32 @@ $(function() {
});
});

socket.on("auth", function(/* data */) {
socket.on("auth", function(data) {
var body = $("body");
var login = $("#sign-in");
if (!login.length) {
refresh();
return;
}

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");
socket.emit("auth", {token: token});
}
if (body.hasClass("signed-out")) {

var error = login.find(".error");
error.show().closest("form").one("submit", function() {
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']");
if (input.val() === "") {
input.val(window.localStorage.getItem("user") || "");
Expand Down
4 changes: 2 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function index(req, res, next) {

function init(socket, client) {
if (!client) {
socket.emit("auth");
socket.emit("auth", {success: true});
socket.on("auth", auth);
} else {
socket.on(
Expand Down Expand Up @@ -251,7 +251,7 @@ function auth(data) {
}
});
if (!success) {
socket.emit("auth");
socket.emit("auth", {success: success});
}
}
}

0 comments on commit 6458427

Please sign in to comment.