Path: blob/master/src/packages/frontend/account/init.ts
1503 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { deep_copy } from "@cocalc/util/misc";6import { SCHEMA } from "@cocalc/util/schema";7import { webapp_client } from "../webapp-client";8import { AccountActions } from "./actions";9import { AccountStore } from "./store";10import { AccountTable } from "./table";11import { init_dark_mode } from "./dark-mode";12import { reset_password_key } from "../client/password-reset";13import { hasRememberMe } from "@cocalc/frontend/misc/remember-me";14import { appBasePath } from "@cocalc/frontend/customize/app-base-path";15import { once } from "@cocalc/util/async-utils";1617export function init(redux) {18// Register account store19// Use the database defaults for all account info until this gets set after they login20const init = deep_copy(SCHEMA.accounts.user_query?.get?.fields) ?? {};21// ... except for show_global_info2 (null or a timestamp)22// REGISTER and STRATEGIES are injected in app.html via the /customize endpoint -- do not delete them!23init.token = global["REGISTER"];24init.strategies = global["STRATEGIES"];25init.other_settings.show_global_info2 = "loading"; // indicates there is no data yet26init.editor_settings.physical_keyboard = "NO_DATA"; // indicator that there is no data27init.user_type = hasRememberMe(appBasePath) ? "signing_in" : "public"; // default28const store = redux.createStore("account", AccountStore, init);29const actions = redux.createActions("account", AccountActions);3031actions._init(store);32init_dark_mode(store);3334redux.createTable("account", AccountTable);35redux.getTable("account")._table.on("error", (tableError) => {36actions.setState({ tableError });37});38redux.getTable("account")._table.on("clear-error", () => {39actions.setState({ tableError: undefined });40});4142// Password reset43actions.setState({ reset_key: reset_password_key() });4445// Login status46webapp_client.on("signed_in", async (mesg) => {47if (mesg?.api_key) {48// wait for sign in to finish and cookie to get set, then redirect49setTimeout(() => {50window.location.href = `https://authenticated?api_key=${mesg.api_key}`;51}, 2000);52}53const table = redux.getTable("account")._table;54if (table != "connected") {55// not fully signed in until the account table is connected, so that we know56// email address, etc. If we don't set this, the UI thinks the user is anonymous57// for a second, which is disconcerting.58await once(table, "connected");59}60redux.getActions("account").set_user_type("signed_in");61});6263webapp_client.on("signed_out", () =>64redux.getActions("account").set_user_type("public"),65);6667webapp_client.on("remember_me_failed", () =>68redux.getActions("account").set_user_type("public"),69);7071// Autosave interval72let _autosave_interval: NodeJS.Timeout | undefined = undefined;73const init_autosave = function (autosave) {74if (_autosave_interval) {75// This function can safely be called again to *adjust* the76// autosave interval, in case user changes the settings.77clearInterval(_autosave_interval);78_autosave_interval = undefined;79}8081// Use the most recent autosave value.82if (autosave) {83const save_all_files = function () {84if (webapp_client.is_connected()) {85redux.getActions("projects")?.save_all_files();86}87};88_autosave_interval = setInterval(save_all_files, autosave * 1000);89}90};9192let _last_autosave_interval_s = undefined;93store.on("change", function () {94const interval_s = store.get("autosave");95if (interval_s !== _last_autosave_interval_s) {96_last_autosave_interval_s = interval_s;97init_autosave(interval_s);98}99});100101// Standby timeout102let last_set_standby_timeout_m = undefined;103store.on("change", function () {104// NOTE: we call this on any change to account settings, which is maybe too extreme.105const x = store.getIn(["other_settings", "standby_timeout_m"]);106if (last_set_standby_timeout_m !== x) {107last_set_standby_timeout_m = x;108webapp_client.idle_client.set_standby_timeout_m(x);109}110});111}112113114