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/admin/users/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 { Component, Rendered } from "@cocalc/frontend/app-framework";6import { Button } from "@cocalc/frontend/antd-bootstrap";7import { CopyToClipBoard, Icon, ErrorDisplay } from "@cocalc/frontend/components";8import { webapp_client } from "../../webapp-client";9import { appBasePath } from "@cocalc/frontend/customize/app-base-path";1011interface Props {12email_address?: string;13}1415interface State {16error?: string;17running: boolean;18link?: string;19}2021export class PasswordReset extends Component<Props, State> {22mounted: boolean = true;2324constructor(props: any) {25super(props);26this.state = { running: false };27}2829componentWillUnmount(): void {30this.mounted = false;31}3233async do_request(): Promise<void> {34if (!this.props.email_address) throw Error("bug");35this.setState({ running: true });36let link: string;37try {38link = await webapp_client.admin_client.admin_reset_password(39this.props.email_address40);41} catch (err) {42if (!this.mounted) return;43this.setState({ error: `${err}`, running: false });44return;45}46if (!this.mounted) return;47link = `${document.location.origin}${48appBasePath.length <= 1 ? "" : appBasePath49}${link}`;50this.setState({ link, running: false });51}5253render_password_reset_button(): Rendered {54return (55<Button56disabled={this.state.running}57onClick={() => {58this.do_request();59}}60>61<Icon62name={this.state.running ? "sync" : "lock-open"}63spin={this.state.running}64/>{" "}65Request Password Reset Link...66</Button>67);68}6970render_error(): Rendered {71if (!this.state.error) {72return;73}74return (75<ErrorDisplay76error={this.state.error}77onClose={() => {78this.setState({ error: undefined });79}}80/>81);82}8384render_password_reset_link(): Rendered {85if (!this.state.link) return;86return (87<div>88<div style={{ marginTop: "20px" }}>89{" "}90Send this somehow to{" "}91<a92href={`mailto:${this.props.email_address}`}93target="_blank"94rel="noopener noreferrer"95>96{this.props.email_address}.97</a>98<br />99<CopyToClipBoard value={this.state.link} />100</div>101</div>102);103}104105render(): Rendered {106if (!this.props.email_address) {107return (108<div>109User does not have an email address set, so password reset does not110make sense.111</div>112);113}114return (115<div>116<b>Password Reset:</b>117<br />118{this.render_error()}119{this.render_password_reset_button()}120{this.render_password_reset_link()}121<br />122<br />123</div>124);125}126}127128129