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/redeem-password-reset.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Alert, Button, Input } from "antd";6import { useState } from "react";7import { useRouter } from "next/router";89import useCustomize from "lib/use-customize";10import A from "components/misc/A";11import apiPost from "lib/api/post";12import { Icon } from "@cocalc/frontend/components/icon";13import Contact from "components/landing/contact";1415import AuthPageContainer from "./fragments/auth-page-container";1617export default function PasswordReset({ passwordResetId }) {18const { siteName } = useCustomize();19const [password, setPassword] = useState<string>("");20const [resetting, setResetting] = useState<boolean>(false);21const [error, setError] = useState<string>("");22const [success, setSuccess] = useState<string>("");23const router = useRouter();2425async function resetPassword() {26if (resetting) return;27try {28setError("");29setSuccess("");30setResetting(true);31await apiPost("/auth/redeem-password-reset", {32password,33passwordResetId,34});35// if no error got signed in, so go to success page.36router.push("/auth/password-reset-done");37} catch (err) {38setError(`${err}`);39} finally {40setResetting(false);41}42}4344function renderFooter() {45return (46<>47<p>48Remember your password? <A href="/auth/sign-in">Sign In</A>49</p>50You can also{" "}51<A href="/auth/try">use {siteName} without creating an account</A>52</>53);54}5556function renderError() {57return error && (58<div style={{ fontSize: "12pt" }}>59<b>{error}</b>60<br/> If you are stuck, please <Contact lower/>.61</div>62);63}6465return (66<AuthPageContainer67error={renderError()}68footer={renderFooter()}69title={`Reset Your ${siteName} Password`}70>71<div style={{ margin: "10px 0" }}>72Choose a new {siteName} password:73</div>74<form>75<Input.Password76style={{ fontSize: "13pt" }}77autoFocus78placeholder="New Password"79autoComplete="username"80value={password}81onChange={(e) => {82setPassword(e.target.value);83setError("");84setSuccess("");85}}86onPressEnter={(e) => {87e.preventDefault();88if (resetting || !isValidPassword(password)) return;89resetPassword();90}}91/>92<Button93disabled={!password || resetting || !isValidPassword(password)}94shape="round"95size="large"96type="primary"97style={{ width: "100%", marginTop: "20px" }}98onClick={resetPassword}99>100{resetting ? (101<>102<Icon name="spinner" spin/> Changing password...103</>104) : !isValidPassword(password) ? (105"Enter password (at least 6 characters)."106) : (107"Change Password"108)}109</Button>110</form>111{success && (112<Alert113style={{ marginTop: "20px" }}114message={<b>Success</b>}115description={<div style={{ fontSize: "12pt" }}>{success}</div>}116type="success"117showIcon118/>119)}120</AuthPageContainer>121);122}123124function isValidPassword(password: string): boolean {125return password.length >= 6;126}127128129