thelounge/client/js/socket-events/auth.js

106 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-05-18 20:08:54 +00:00
"use strict";
const $ = require("jquery");
const socket = require("../socket");
const storage = require("../localStorage");
const utils = require("../utils");
2017-11-04 17:16:30 +00:00
const templates = require("../../views");
2018-07-15 20:23:49 +00:00
const {vueApp} = require("../vue");
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
// And we will reload the page to grab the latest version
if (utils.serverHash > -1 && data.serverHash > -1 && data.serverHash !== utils.serverHash) {
socket.disconnect();
2018-08-31 10:43:21 +00:00
vueApp.connected = false;
vueApp.connectionError = "Server restarted, reloading…";
location.reload(true);
return;
}
const login = $("#sign-in");
if (data.serverHash > -1) {
utils.serverHash = data.serverHash;
2017-05-18 20:08:54 +00:00
login.html(templates.windows.sign_in());
2017-11-11 18:33:21 +00:00
2018-05-10 18:37:10 +00:00
utils.togglePasswordField("#sign-in .reveal-password");
login.find("form").on("submit", function() {
const form = $(this);
2017-11-11 18:33:21 +00:00
form.find(".btn").prop("disabled", true);
2017-11-11 18:33:21 +00:00
const values = {};
$.each(form.serializeArray(), function(i, obj) {
values[obj.name] = obj.value;
});
2017-11-11 18:33:21 +00:00
storage.set("user", values.user);
2017-11-11 18:33:21 +00:00
socket.emit("auth", values);
2017-11-11 18:33:21 +00:00
return false;
});
} else {
login.find(".btn").prop("disabled", false);
}
let token;
const user = storage.get("user");
2017-05-18 20:08:54 +00:00
if (!data.success) {
if (login.length === 0) {
socket.disconnect();
2018-08-31 10:43:21 +00:00
vueApp.connected = false;
vueApp.connectionError = "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();
});
} else if (user) {
2017-05-18 20:08:54 +00:00
token = storage.get("token");
2017-05-18 20:08:54 +00:00
if (token) {
2018-08-31 10:43:21 +00:00
vueApp.connectionError = "Authorizing…";
$("#loading-page-message").text(vueApp.connectionError);
2018-07-15 20:23:49 +00:00
let lastMessage = -1;
for (const network of vueApp.networks) {
for (const chan of network.channels) {
for (const msg of chan.messages) {
if (msg.id > lastMessage) {
lastMessage = msg.id;
}
}
}
}
socket.emit("auth", {user, token, lastMessage});
2017-05-18 20:08:54 +00:00
}
}
if (user) {
login.find("input[name='user']").val(user);
2017-05-18 20:08:54 +00:00
}
2017-05-18 20:08:54 +00:00
if (token) {
return;
}
2018-07-08 08:52:05 +00:00
$("#loading").remove();
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
});