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/next/components/auth/try.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Create an anonymous account.7*/89import { Button } from "antd";10import { useState } from "react";11import {12GoogleReCaptchaProvider,13useGoogleReCaptcha,14} from "react-google-recaptcha-v3";1516import { len } from "@cocalc/util/misc";17import A from "components/misc/A";18import Loading from "components/share/loading";19import api from "lib/api/post";20import useCustomize from "lib/use-customize";2122import AuthPageContainer from "./fragments/auth-page-container";2324interface Props {25minimal?: boolean;26publicPathId?: string;27onSuccess: () => void; // if given, call after sign up *succeeds*.28}2930export default function Try(props: Props) {31const { reCaptchaKey } = useCustomize();32const body = <Try0 {...props} />;33if (reCaptchaKey == null) {34return body;35}36return (37<GoogleReCaptchaProvider reCaptchaKey={reCaptchaKey}>38{body}39</GoogleReCaptchaProvider>40);41}4243function Try0({ minimal, onSuccess, publicPathId }: Props) {44const {45siteName,46anonymousSignup,47reCaptchaKey,48anonymousSignupLicensedShares,49} = useCustomize();50const [state, setState] = useState<"wait" | "creating" | "done">("wait");51const [error, setError] = useState<string>("");52const { executeRecaptcha } = useGoogleReCaptcha();5354if (!anonymousSignup && !(anonymousSignupLicensedShares && publicPathId)) {55return (56<h1 style={{ textAlign: "center", margin: "45px auto" }}>57Anonymous Trial of {siteName} Not Currently Available58</h1>59);60}6162async function createAnonymousAccount() {63setState("creating");64try {65let reCaptchaToken: undefined | string;66if (reCaptchaKey) {67if (!executeRecaptcha) {68throw Error("Please wait a few seconds, then try again.");69}70reCaptchaToken = await executeRecaptcha("anonymous");71}7273const result = await api("/auth/sign-up", {74reCaptchaToken,75publicPathId,76});77if (result.issues && len(result.issues) > 0) {78throw Error(JSON.stringify(result.issues)); // TODO: should not happen, except for captcha error...79}80onSuccess?.();81setState("done");82} catch (err) {83setError(err.message);84setState("wait");85}86}8788function renderFooter() {89return !minimal && (90<>91<div>92Already have an account? <A href="/auth/sign-in">Sign In</A>93</div>94<div style={{ marginTop: "15px" }}>95Need an account? <A href="/auth/sign-up">Sign Up</A>96</div>97</>98);99}100101return (102<AuthPageContainer103error={error}104footer={renderFooter()}105minimal={minimal}106title={`Use ${siteName} Anonymously`}107>108<div style={{ margin: "10px 0" }}>109Use {siteName} <b>without</b>{" "}110<A href="/auth/sign-up" external={!!minimal}>111creating an account112</A>113!114<Button115disabled={state != "wait"}116shape="round"117size="large"118type="primary"119style={{ width: "100%", marginTop: "20px" }}120onClick={createAnonymousAccount}121>122{state == "creating" ? (123<Loading>Configuring Anonymous Access...</Loading>124) : (125<>Use {siteName} Anonymously</>126)}127</Button>128</div>129</AuthPageContainer>130);131}132133134