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

81 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-05-18 22:08:54 +02:00
"use strict";
const $ = require("jquery");
const socket = require("../socket");
const storage = require("../localStorage");
const utils = require("../utils");
2019-02-18 10:18:32 +01:00
const {vueApp, getActiveWindowComponent} = require("../vue");
2017-05-18 22:08:54 +02:00
socket.on("auth", function(data) {
2017-08-28 22:06:28 +02: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();
vueApp.$store.commit("isConnected", false);
2018-09-09 15:09:19 +02:00
vueApp.currentUserVisibleError = "Server restarted, reloading…";
location.reload(true);
return;
}
if (data.serverHash > -1) {
utils.serverHash = data.serverHash;
2017-05-18 22:08:54 +02:00
2019-02-18 10:18:32 +01:00
vueApp.activeWindow = "SignIn";
} else {
2019-02-18 10:18:32 +01:00
getActiveWindowComponent().inFlight = false;
}
let token;
const user = storage.get("user");
2017-05-18 22:08:54 +02:00
if (!data.success) {
2019-02-18 10:18:32 +01:00
if (vueApp.activeWindow !== "SignIn") {
socket.disconnect();
vueApp.$store.commit("isConnected", false);
2018-09-09 15:09:19 +02:00
vueApp.currentUserVisibleError = "Authentication failed, reloading…";
location.reload();
return;
}
2017-05-18 22:08:54 +02:00
storage.remove("token");
2019-02-18 10:18:32 +01:00
getActiveWindowComponent().errorShown = true;
} else if (user) {
2017-05-18 22:08:54 +02:00
token = storage.get("token");
2017-05-18 22:08:54 +02:00
if (token) {
2018-09-09 15:09:19 +02:00
vueApp.currentUserVisibleError = "Authorizing…";
$("#loading-page-message").text(vueApp.currentUserVisibleError);
2018-07-15 22:23:49 +02:00
let lastMessage = -1;
for (const network of vueApp.networks) {
for (const chan of network.channels) {
if (chan.messages.length > 0) {
const id = chan.messages[chan.messages.length - 1].id;
if (lastMessage < id) {
lastMessage = id;
2018-07-15 22:23:49 +02:00
}
}
}
}
const openChannel = (vueApp.activeChannel && vueApp.activeChannel.channel.id) || null;
socket.emit("auth", {user, token, lastMessage, openChannel});
2017-05-18 22:08:54 +02:00
}
}
if (token) {
return;
}
2018-07-08 10:52:05 +02:00
$("#loading").remove();
2017-09-12 14:52:16 +02:00
$("#footer")
.find(".sign-in")
2017-05-18 22:08:54 +02:00
.trigger("click", {
pushState: false,
2017-09-12 14:52:16 +02:00
});
2017-05-18 22:08:54 +02:00
});