hardlounge/client/js/socket-events/auth.ts

105 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

2019-11-16 17:24:03 +00:00
import socket from "../socket";
import storage from "../localStorage";
import {router, navigate} from "../router";
import {store} from "../store";
2019-11-16 17:24:03 +00:00
import location from "../location";
let lastServerHash: number | null = null;
declare global {
interface Window {
g_TheLoungeRemoveLoading?: () => void;
}
}
2017-05-18 20:08:54 +00:00
socket.on("auth:success", function () {
2019-11-05 19:29:51 +00:00
store.commit("currentUserVisibleError", "Loading messages…");
updateLoadingMessage();
2019-11-05 19:29:51 +00:00
});
socket.on("auth:failed", async function () {
2019-11-05 19:29:51 +00:00
storage.remove("token");
if (store.state.appLoaded) {
return reloadPage("Authentication failed, reloading…");
}
await showSignIn();
2019-11-05 19:29:51 +00:00
});
socket.on("auth:start", async function (serverHash) {
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
2019-11-05 19:29:51 +00:00
if (lastServerHash && serverHash !== lastServerHash) {
return reloadPage("Server restarted, reloading…");
}
2019-11-05 19:29:51 +00:00
lastServerHash = serverHash;
const user = storage.get("user");
2019-11-05 19:29:51 +00:00
const token = storage.get("token");
const doFastAuth = user && token;
2017-05-18 20:08:54 +00:00
2019-11-05 19:29:51 +00:00
// If we reconnect and no longer have a stored token, reload the page
if (store.state.appLoaded && !doFastAuth) {
return reloadPage("Authentication failed, reloading…");
}
2019-11-05 19:29:51 +00:00
// If we have user and token stored, perform auth without showing sign-in first
if (doFastAuth) {
store.commit("currentUserVisibleError", "Authorizing…");
updateLoadingMessage();
2018-07-15 20:23:49 +00:00
2019-11-05 19:29:51 +00:00
let lastMessage = -1;
2018-07-15 20:23:49 +00:00
2019-11-05 19:29:51 +00: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-11-05 19:29:51 +00:00
if (lastMessage < id) {
lastMessage = id;
2018-07-15 20:23:49 +00:00
}
}
}
2019-11-05 19:29:51 +00:00
}
2018-07-15 20:23:49 +00:00
2019-11-05 19:29:51 +00:00
const openChannel =
(store.state.activeChannel && store.state.activeChannel.channel.id) || null;
socket.emit("auth:perform", {
user,
token,
lastMessage,
openChannel,
hasConfig: store.state.serverConfiguration !== null,
});
2019-11-05 19:29:51 +00:00
} else {
await showSignIn();
2017-05-18 20:08:54 +00:00
}
2019-11-05 19:29:51 +00:00
});
2017-05-18 20:08:54 +00:00
async function showSignIn() {
2019-11-05 19:29:51 +00:00
// 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 20:08:54 +00:00
}
if (router.currentRoute.value.name !== "SignIn") {
await navigate("SignIn");
}
2019-11-05 19:29:51 +00:00
}
function reloadPage(message: string) {
2019-11-05 19:29:51 +00:00
socket.disconnect();
store.commit("currentUserVisibleError", message);
location.reload();
2019-11-05 19:29:51 +00:00
}
function updateLoadingMessage() {
const loading = document.getElementById("loading-page-message");
if (loading) {
loading.textContent = store.state.currentUserVisibleError;
}
}