Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/components/account/config/anonymous/upgrade.tsx
6079 views
1
/*
2
* This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Alert, Button, Input, Space } from "antd";
7
import { delay } from "awaiting";
8
import { CSSProperties, useState } from "react";
9
10
import { Icon } from "@cocalc/frontend/components/icon";
11
import { MIN_PASSWORD_LENGTH } from "@cocalc/util/auth";
12
import { is_valid_email_address as isValidEmailAddress } from "@cocalc/util/misc";
13
import { TermsCheckbox } from "components/auth/sign-up";
14
import SSO from "components/auth/sso";
15
import { Title } from "components/misc";
16
import apiPost from "lib/api/post";
17
import { useRouter } from "next/router";
18
19
interface Props {
20
style: CSSProperties;
21
}
22
23
export default function Upgrade({ style }: Props) {
24
const [terms, setTerms] = useState<boolean>(false);
25
return (
26
<div style={style}>
27
<TermsCheckbox onChange={setTerms} checked={terms} />
28
<br />
29
<br />
30
{terms && <EmailPassword />}
31
<br />
32
<br />
33
{terms && (
34
<SSO
35
style={{ margin: "30px 0" }}
36
header={<Title level={3}>Or Use Single Sign On</Title>}
37
/>
38
)}
39
</div>
40
);
41
}
42
43
function EmailPassword() {
44
const router = useRouter();
45
const [success, setSuccess] = useState<boolean>(false);
46
const [error, setError] = useState<string>("");
47
const [email_address, setEmailAddress] = useState<string>("");
48
const [password, setPassword] = useState<string>("");
49
async function setEmailAndPassword() {
50
setError("");
51
try {
52
await apiPost("/accounts/set-email-address", {
53
email_address,
54
password,
55
});
56
setSuccess(true);
57
// send them to configure their name, which is a good next step...
58
router.push("/config/account/name");
59
await delay(1000);
60
// need to force reload to know that they are no longer anonymous.
61
router.reload();
62
} catch (err) {
63
setError(err.message);
64
}
65
}
66
return (
67
<>
68
<Title level={3}>Set an Email Address and Password</Title>
69
{error && (
70
<Alert
71
type="error"
72
showIcon
73
message={error}
74
style={{ marginTop: "15px" }}
75
/>
76
)}
77
<Space style={{ width: "100%" }}>
78
<Input
79
disabled={success}
80
style={{ fontSize: "12pt" }}
81
placeholder="Email address"
82
autoComplete="username"
83
value={email_address}
84
onChange={(e) => {
85
setEmailAddress(e.target.value);
86
setError("");
87
}}
88
/>
89
<Input.Password
90
disabled={success}
91
style={{ fontSize: "12pt" }}
92
value={password}
93
placeholder="Password"
94
autoComplete="new-password"
95
onChange={(e) => {
96
setPassword(e.target.value);
97
setError("");
98
}}
99
onPressEnter={setEmailAndPassword}
100
/>
101
{/* change height of button to match input boxes */}
102
<Button
103
type="primary"
104
disabled={
105
success || !email_address || password.length < MIN_PASSWORD_LENGTH
106
}
107
style={{ height: "35px" }}
108
onClick={setEmailAndPassword}
109
>
110
{success ? (
111
<>
112
<Icon name="check" style={{ marginRight: "5px" }} /> Saved
113
</>
114
) : email_address.length > 0 &&
115
!isValidEmailAddress(email_address) ? (
116
"Enter valid email"
117
) : password.length > 0 && password.length < MIN_PASSWORD_LENGTH ? (
118
`At least ${MIN_PASSWORD_LENGTH} characters`
119
) : (
120
<>
121
<Icon name="check" style={{ marginRight: "5px" }} /> Save
122
</>
123
)}
124
</Button>
125
</Space>
126
</>
127
);
128
}
129
130