2017-05-18 20:08:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const socket = require("../socket");
|
|
|
|
const storage = require("../localStorage");
|
2017-08-28 09:18:31 +00:00
|
|
|
const utils = require("../utils");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
socket.on("auth", function(data) {
|
2017-08-28 20:06:28 +00:00
|
|
|
// If we reconnected and serverHash differs, that means the server restarted
|
2017-08-28 09:18:31 +00:00
|
|
|
// And we will reload the page to grab the latest version
|
|
|
|
if (utils.serverHash > -1 && data.serverHash > -1 && data.serverHash !== utils.serverHash) {
|
|
|
|
socket.disconnect();
|
2017-08-28 15:03:27 +00:00
|
|
|
$("#connection-error").text("Server restarted, reloading…");
|
2017-08-28 09:18:31 +00:00
|
|
|
location.reload(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.serverHash = data.serverHash;
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
const login = $("#sign-in");
|
|
|
|
let token;
|
2017-06-21 07:58:29 +00:00
|
|
|
const user = storage.get("user");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
login.find(".btn").prop("disabled", false);
|
|
|
|
|
|
|
|
if (!data.success) {
|
2017-08-28 09:18:31 +00:00
|
|
|
if (login.length === 0) {
|
|
|
|
socket.disconnect();
|
|
|
|
$("#connection-error").text("Authentication failed, reloading…");
|
|
|
|
location.reload();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
storage.remove("token");
|
|
|
|
|
|
|
|
const error = login.find(".error");
|
|
|
|
error.show().closest("form").one("submit", function() {
|
|
|
|
error.hide();
|
|
|
|
});
|
2017-06-21 07:58:29 +00:00
|
|
|
} else if (user) {
|
2017-05-18 20:08:54 +00:00
|
|
|
token = storage.get("token");
|
2017-08-28 09:18:31 +00:00
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
if (token) {
|
2017-08-28 15:03:27 +00:00
|
|
|
$("#loading-page-message, #connection-error").text("Authorizing…");
|
2017-08-28 09:18:31 +00:00
|
|
|
|
|
|
|
socket.emit("auth", {
|
|
|
|
user: user,
|
|
|
|
token: token,
|
|
|
|
lastMessage: utils.lastMessageId,
|
|
|
|
});
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 07:58:29 +00:00
|
|
|
if (user) {
|
|
|
|
login.find("input[name='user']").val(user);
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
2017-06-21 07:58:29 +00:00
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
if (token) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-21 07:58:29 +00:00
|
|
|
|
2017-09-12 12:52:16 +00:00
|
|
|
$("#footer")
|
|
|
|
.find(".sign-in")
|
2017-05-18 20:08:54 +00:00
|
|
|
.trigger("click", {
|
|
|
|
pushState: false,
|
2017-09-12 12:52:16 +00:00
|
|
|
});
|
2017-05-18 20:08:54 +00:00
|
|
|
});
|