CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/components/auth/password-reset.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Alert, Button, Input } from "antd";
7
import { useState } from "react";
8
9
import { Icon } from "@cocalc/frontend/components/icon";
10
import { is_valid_email_address as isValidEmailAddress } from "@cocalc/util/misc";
11
import Contact from "components/landing/contact";
12
import A from "components/misc/A";
13
import apiPost from "lib/api/post";
14
import useCustomize from "lib/use-customize";
15
16
import AuthPageContainer from "./fragments/auth-page-container";
17
18
export default function PasswordReset() {
19
const { siteName } = useCustomize();
20
const [email, setEmail] = useState<string>("");
21
const [resetting, setResetting] = useState<boolean>(false);
22
const [error, setError] = useState<string>("");
23
const [success, setSuccess] = useState<string>("");
24
25
async function resetPassword() {
26
if (resetting) return;
27
try {
28
setError("");
29
setSuccess("");
30
setResetting(true);
31
const result = await apiPost("/auth/password-reset", { email });
32
if (result.success) {
33
setEmail("");
34
setSuccess(result.success);
35
}
36
} catch (err) {
37
setError(`${err}`);
38
} finally {
39
setResetting(false);
40
}
41
}
42
43
function renderFooter() {
44
return (
45
<>
46
<p>
47
Remember your password? <A href="/auth/sign-in">Sign In</A>
48
</p>
49
<p>
50
Don't have an account? <A href="/auth/sign-up">Sign Up</A>
51
</p>
52
You can also {" "}
53
<A href="/auth/try">try {siteName} without creating an account</A>
54
</>
55
);
56
}
57
58
function renderError() {
59
return error && (
60
<div style={{ fontSize: "12pt" }}>
61
<b>{error}</b>
62
<br/>
63
If you are stuck, please <Contact lower/>.
64
</div>
65
);
66
}
67
68
return (
69
<AuthPageContainer
70
error={renderError()}
71
footer={renderFooter()}
72
title={`Reset Your ${siteName} Password`}
73
>
74
<div style={{ margin: "10px 0" }}>
75
Enter your {siteName} account's email address and we will email a
76
password reset link to you.
77
</div>
78
<form>
79
<Input
80
style={{ fontSize: "13pt" }}
81
autoFocus
82
placeholder="Enter your email address"
83
autoComplete="username"
84
value={email}
85
onChange={(e) => {
86
setEmail(e.target.value);
87
setError("");
88
setSuccess("");
89
}}
90
onPressEnter={(e) => {
91
e.preventDefault();
92
if (resetting || !isValidEmailAddress(email)) return;
93
resetPassword();
94
}}
95
/>
96
<Button
97
disabled={!email || resetting || !isValidEmailAddress(email) || !!error}
98
shape="round"
99
size="large"
100
type="primary"
101
style={{ width: "100%", marginTop: "20px" }}
102
onClick={resetPassword}
103
>
104
{resetting ? (
105
<>
106
<Icon name="spinner" spin/> Sending password reset email...
107
</>
108
) : !email || !isValidEmailAddress(email) || error ? (
109
"Enter your email address."
110
) : (
111
"Send password reset email"
112
)}
113
</Button>
114
</form>
115
{success && (
116
<Alert
117
style={{ marginTop: "20px" }}
118
message={<b>Success</b>}
119
description={<div style={{ fontSize: "12pt" }}>{success}</div>}
120
type="success"
121
showIcon
122
/>
123
)}
124
</AuthPageContainer>
125
);
126
}
127
128