Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/client/anonymous-setup.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { once } from "@cocalc/util/async-utils";6import { redux } from "../app-framework";7import { QueryParams } from "../misc/query-params";8import { WelcomeFile } from "./welcome-file";9import { WebappClient } from "./client";10import { PROJECT_INVITE_QUERY_PARAM } from "../collaborators/handle-project-invite";11import { hasRememberMe } from "@cocalc/frontend/misc/remember-me";12import { appBasePath } from "@cocalc/frontend/customize/app-base-path";1314export const ANON_PROJECT_TITLE = "Welcome to CoCalc!";1516/*17should_do_anonymous_setup: Determine if the anonymous query param is set at all18(doesn't matter to what) during initial page load. Similar, if the19project_invite query param is set, this implies anonymous, so we also do anon20setup there if the user isn't already (likely) signed in.2122Also do NOT make true if has_remember_me is set, since then probably23the user has an account.24*/25let project_invite_query_param = QueryParams.get(PROJECT_INVITE_QUERY_PARAM);26export function should_do_anonymous_setup(): boolean {27const anonymous_query_param = QueryParams.get("anonymous");28return (29(anonymous_query_param != null || project_invite_query_param != null) &&30!hasRememberMe(appBasePath)31);32}3334async function setup_default_project(log) {35const actions = redux.getActions("projects");36log("creating project");37const project_id = await actions.create_project({38title: ANON_PROJECT_TITLE,39start: true,40description: "",41});42log("opening project");43actions.open_project({ project_id, switch_to: true });44await new WelcomeFile(project_id).open();45}4647export async function do_anonymous_setup(client: WebappClient): Promise<void> {48function log(..._args): void {49// uncomment to debug...50// console.log("do_anonymous_setup", ..._args);51}52log();53try {54redux.getActions("account").setState({ doing_anonymous_setup: true });55log("creating account");56try {57const resp = await client.account_client.create_account({58first_name: "Anonymous",59last_name: `User-${Math.round(Date.now() / 1000)}`,60});61if (resp?.event == "account_creation_failed") {62throw Error(resp.error);63}64} catch (err) {65log("failed to create account", err);66// If there is an error specifically with creating the account67// due to the backend not allowing it (e.g., missing token), then68// it is fine to silently return, which falls back to the login69// screen. Of course, all other errors below should make some noise.70return;71}72if (!client.is_signed_in()) {73log("waiting to be signed in");74await once(client, "signed_in");75}76if (project_invite_query_param) {77// This will get handled elsewhere. In particular, we78// don't need to do anything else besides make79// their anonymous account.80return;81}8283// "share" and "custom software images" create projects on their own!84const launch_store = redux.getStore(85(await import("../launch/actions")).NAME86);87const need_project = !launch_store.get("type");88if (need_project) {89await setup_default_project(log);90}91} catch (err) {92console.warn("ERROR doing anonymous sign up -- ", err);93log("err", err);94// There was an error creating the account (probably), so we do nothing95// further involving making an anonymous account.96// If the user didn't get signed in, this will fallback to sign in page, which97// is reasonable behavior.98// Such an error *should* happen if, e.g., a sign in token is required,99// or maybe this user's ip is blocked. Falling back100// to normal sign up makes sense in this case.101return;102} finally {103redux.getActions("account").setState({ doing_anonymous_setup: false });104log("removing anonymous param");105// In all cases, remove the 'anonymous' parameter. This way if106// they refresh their browser it won't cause confusion.107QueryParams.remove("anonymous");108}109}110111112