2017-05-18 16:08:54 -04:00
|
|
|
"use strict";
|
|
|
|
|
2019-11-16 12:24:03 -05:00
|
|
|
import socket from "../socket";
|
|
|
|
import storage from "../localStorage";
|
|
|
|
import {router, navigate} from "../router";
|
|
|
|
import store from "../store";
|
|
|
|
import location from "../location";
|
2019-11-05 14:29:51 -05:00
|
|
|
let lastServerHash = null;
|
2017-05-18 16:08:54 -04:00
|
|
|
|
2019-11-05 14:29:51 -05:00
|
|
|
socket.on("auth:success", function() {
|
|
|
|
store.commit("currentUserVisibleError", "Loading messages…");
|
2019-11-05 14:22:10 -05:00
|
|
|
updateLoadingMessage();
|
2019-11-05 14:29:51 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("auth:failed", function() {
|
|
|
|
storage.remove("token");
|
|
|
|
|
|
|
|
if (store.state.appLoaded) {
|
|
|
|
return reloadPage("Authentication failed, reloading…");
|
|
|
|
}
|
|
|
|
|
|
|
|
showSignIn();
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("auth:start", function(serverHash) {
|
2017-08-28 16:06:28 -04:00
|
|
|
// If we reconnected and serverHash differs, that means the server restarted
|
2017-08-28 05:18:31 -04:00
|
|
|
// And we will reload the page to grab the latest version
|
2019-11-05 14:29:51 -05:00
|
|
|
if (lastServerHash && serverHash !== lastServerHash) {
|
|
|
|
return reloadPage("Server restarted, reloading…");
|
2017-08-28 05:18:31 -04:00
|
|
|
}
|
|
|
|
|
2019-11-05 14:29:51 -05:00
|
|
|
lastServerHash = serverHash;
|
2017-11-27 12:39:16 -05:00
|
|
|
|
|
|
|
const user = storage.get("user");
|
2019-11-05 14:29:51 -05:00
|
|
|
const token = storage.get("token");
|
|
|
|
const doFastAuth = user && token;
|
2017-05-18 16:08:54 -04:00
|
|
|
|
2019-11-05 14:29:51 -05:00
|
|
|
// If we reconnect and no longer have a stored token, reload the page
|
|
|
|
if (store.state.appLoaded && !doFastAuth) {
|
|
|
|
return reloadPage("Authentication failed, reloading…");
|
|
|
|
}
|
2017-08-28 05:18:31 -04:00
|
|
|
|
2019-11-05 14:29:51 -05:00
|
|
|
// If we have user and token stored, perform auth without showing sign-in first
|
|
|
|
if (doFastAuth) {
|
|
|
|
store.commit("currentUserVisibleError", "Authorizing…");
|
2019-11-05 14:22:10 -05:00
|
|
|
updateLoadingMessage();
|
2018-07-15 16:23:49 -04:00
|
|
|
|
2019-11-05 14:29:51 -05:00
|
|
|
let lastMessage = -1;
|
2018-07-15 16:23:49 -04:00
|
|
|
|
2019-11-05 14:29:51 -05:00
|
|
|
for (const network of store.state.networks) {
|
|
|
|
for (const chan of network.channels) {
|
|
|
|
if (chan.messages.length > 0) {
|
|
|
|
const id = chan.messages[chan.messages.length - 1].id;
|
2019-10-17 06:53:29 -04:00
|
|
|
|
2019-11-05 14:29:51 -05:00
|
|
|
if (lastMessage < id) {
|
|
|
|
lastMessage = id;
|
2018-07-15 16:23:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 14:29:51 -05:00
|
|
|
}
|
2018-07-15 16:23:49 -04:00
|
|
|
|
2019-11-05 14:29:51 -05:00
|
|
|
const openChannel =
|
|
|
|
(store.state.activeChannel && store.state.activeChannel.channel.id) || null;
|
2019-10-17 06:53:29 -04:00
|
|
|
|
2019-11-12 05:39:46 -05:00
|
|
|
socket.emit("auth:perform", {
|
|
|
|
user,
|
|
|
|
token,
|
|
|
|
lastMessage,
|
|
|
|
openChannel,
|
|
|
|
hasConfig: store.state.serverConfiguration !== null,
|
|
|
|
});
|
2019-11-05 14:29:51 -05:00
|
|
|
} else {
|
|
|
|
showSignIn();
|
2017-05-18 16:08:54 -04:00
|
|
|
}
|
2019-11-05 14:29:51 -05:00
|
|
|
});
|
2017-05-18 16:08:54 -04:00
|
|
|
|
2019-11-05 14:29:51 -05:00
|
|
|
function showSignIn() {
|
|
|
|
// TODO: this flashes grey background because it takes a little time for vue to mount signin
|
|
|
|
if (window.g_TheLoungeRemoveLoading) {
|
|
|
|
window.g_TheLoungeRemoveLoading();
|
2017-05-18 16:08:54 -04:00
|
|
|
}
|
2017-06-21 03:58:29 -04:00
|
|
|
|
2019-11-12 05:39:46 -05:00
|
|
|
if (router.currentRoute.name !== "SignIn") {
|
2019-11-11 14:18:55 -05:00
|
|
|
navigate("SignIn");
|
2019-11-11 08:48:47 -05:00
|
|
|
}
|
2019-11-05 14:29:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function reloadPage(message) {
|
|
|
|
socket.disconnect();
|
|
|
|
store.commit("currentUserVisibleError", message);
|
|
|
|
location.reload(true);
|
|
|
|
}
|
2019-11-05 14:22:10 -05:00
|
|
|
|
|
|
|
function updateLoadingMessage() {
|
|
|
|
const loading = document.getElementById("loading-page-message");
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
loading.textContent = store.state.currentUserVisibleError;
|
|
|
|
}
|
|
|
|
}
|