// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import React from "react"; import {Button, Col, Form, Input, Row, Select, Steps} from "antd"; import * as AuthBackend from "./AuthBackend"; import * as ApplicationBackend from "../backend/ApplicationBackend"; import * as Util from "./Util"; import * as Setting from "../Setting"; import i18next from "i18next"; import {CountDownInput} from "../common/CountDownInput"; import * as UserBackend from "../backend/UserBackend"; import {CheckCircleOutlined, KeyOutlined, LockOutlined, SolutionOutlined, UserOutlined} from "@ant-design/icons"; import CustomGithubCorner from "../CustomGithubCorner"; const {Step} = Steps; const {Option} = Select; class ForgetPage extends React.Component { constructor(props) { super(props); this.state = { classes: props, account: props.account, applicationName: props.applicationName !== undefined ? props.applicationName : props.match === undefined ? null : props.match.params.applicationName, application: null, msg: null, userId: "", username: "", name: "", email: "", isFixed: false, fixedContent: "", token: "", phone: "", emailCode: "", phoneCode: "", verifyType: null, // "email" or "phone" current: 0, }; } UNSAFE_componentWillMount() { if (this.state.applicationName !== undefined) { this.getApplication(); } else { Util.showMessage( "error", i18next.t("forget:Unknown forget type: ") + this.state.type, ); } } getApplication() { if (this.state.applicationName === null) { return; } ApplicationBackend.getApplication("admin", this.state.applicationName).then( (application) => { this.setState({ application: application, }); }, ); } getApplicationObj() { if (this.props.application !== undefined) { return this.props.application; } else { return this.state.application; } } onFormFinish(name, info, forms) { switch (name) { case "step1": const username = forms.step1.getFieldValue("username"); AuthBackend.getEmailAndPhone({ application: forms.step1.getFieldValue("application"), organization: forms.step1.getFieldValue("organization"), username: username, }).then((res) => { if (res.status === "ok") { const phone = res.data.phone; const email = res.data.email; this.setState({phone: phone, email: email, username: res.data.name, name: res.data.name}); if (phone !== "" && email === "") { this.setState({ verifyType: "phone", }); } else if (phone === "" && email !== "") { this.setState({ verifyType: "email", }); } switch (res.data2) { case "email": this.setState({isFixed: true, fixedContent: email, verifyType: "email"}); break; case "phone": this.setState({isFixed: true, fixedContent: phone, verifyType: "phone"}); break; default: break; } if (this.state.isFixed) { forms.step2.setFieldsValue({email: this.state.fixedContent}); this.setState({username: this.state.fixedContent}); } this.setState({current: 1}); } else { Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); } }); break; case "step2": const oAuthParams = Util.getOAuthGetParameters(); if (this.state.verifyType === "email") { this.setState({username: this.state.email}); } else if (this.state.verifyType === "phone") { this.setState({username: this.state.phone}); } AuthBackend.login({ application: forms.step2.getFieldValue("application"), organization: forms.step2.getFieldValue("organization"), username: this.state.username, name: this.state.name, code: forms.step2.getFieldValue("emailCode"), phonePrefix: this.state.application?.organizationObj.phonePrefix, type: "login", }, oAuthParams).then(res => { if (res.status === "ok") { this.setState({current: 2, userId: res.data, username: res.data.split("/")[1]}); } else { Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); } }); break; default: break; } } onFinish(values) { values.username = this.state.username; values.userOwner = this.state.application?.organizationObj.name; UserBackend.setPassword(values.userOwner, values.username, "", values?.newPassword).then(res => { if (res.status === "ok") { Setting.goToLogin(this, this.state.application); } else { Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); } }); } onFinishFailed(values, errorFields) {} renderOptions() { let options = []; if (this.state.phone !== "") { options.push( , ); } if (this.state.email !== "") { options.push( , ); } return options; } renderForm(application) { return ( { this.onFormFinish(name, info, forms); }}> {/* STEP 1: input username -> get email & phone */} ); } render() { const application = this.getApplicationObj(); if (application === null) { return Util.renderMessageLarge(this, this.state.msg); } return (
{ Setting.renderHelmet(application) } { Setting.renderLogo(application) }
{i18next.t("forget:Retrieve password")}
} /> } /> } />
{this.renderForm(application)}
); } } export default ForgetPage;