Merge pull request #80 from 1340908470/master

feat: find back password using email/phone instead of username
This commit is contained in:
Yang Luo
2021-06-07 20:12:27 +08:00
committed by GitHub
2 changed files with 97 additions and 77 deletions

View File

@ -126,23 +126,24 @@ func (c *ApiController) GetEmailAndPhone() {
panic(err) panic(err)
} }
// get user user := object.GetUserByFields(form.Organization, form.Username)
var userId string
if form.Username == "" {
userId, _ = c.RequireSignedIn()
} else {
userId = fmt.Sprintf("%s/%s", form.Organization, form.Username)
}
user := object.GetUser(userId)
if user == nil { if user == nil {
c.ResponseError("No such user.") c.ResponseError("No such user.")
return return
} }
phone := user.Phone respUser := object.User{Email: user.Email, Phone: user.Phone, Name: user.Name}
email := user.Email var contentType string
switch form.Username {
case user.Email:
contentType = "email"
case user.Phone:
contentType = "phone"
case user.Name:
contentType = "username"
}
resp = Response{Status: "ok", Msg: "", Data: phone, Data2: email} resp = Response{Status: "ok", Msg: "", Data: respUser, Data2: contentType}
c.Data["json"] = resp c.Data["json"] = resp
c.ServeJSON() c.ServeJSON()

View File

@ -49,6 +49,8 @@ class ForgetPage extends React.Component {
userId: "", userId: "",
username: "", username: "",
email: "", email: "",
isFixed: false,
fixedContent: "",
token: "", token: "",
phone: "", phone: "",
emailCode: "", emailCode: "",
@ -91,33 +93,53 @@ class ForgetPage extends React.Component {
} }
} }
onFinishStep1(values) { onFormFinish(name, info, forms) {
AuthBackend.getEmailAndPhone(values).then((res) => { 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") { if (res.status === "ok") {
this.setState({ this.setState({phone: res.data.phone, email: res.data.email, username: res.data.name});
username: values.username, switch (res.data2) {
phone: res.data.toString(), case "email":
email: res.data2.toString(), this.setState({isFixed: true, fixedContent: res.data.email, verifyType: "email"});
current: 1, break
}); case "phone":
this.setState({isFixed: true, fixedContent: res.data.phone, verifyType: "phone"});
break
}
if (this.state.isFixed) {
forms.step2.setFieldsValue({email: this.state.fixedContent})
}
this.setState({current: 1})
} else { } else {
Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); Setting.showMessage("error", i18next.t(`signup:${res.msg}`));
} }
}); });
} break;
case "step2":
onFinishStep2(values) {
values.phonePrefix = this.state.application?.organizationObj.phonePrefix;
values.username = this.state.username;
values.type = "login"
const oAuthParams = Util.getOAuthGetParameters(); const oAuthParams = Util.getOAuthGetParameters();
AuthBackend.login(values, oAuthParams).then(res => { AuthBackend.login({
application: forms.step2.getFieldValue("application"),
organization: forms.step2.getFieldValue("organization"),
email: forms.step2.getFieldValue("email"),
emailCode: forms.step2.getFieldValue("emailCode"),
phonePrefix: this.state.application?.organizationObj.phonePrefix,
username: this.state.username,
type: "login"
}, oAuthParams).then(res => {
if (res.status === "ok") { if (res.status === "ok") {
this.setState({current: 2, userId: res.data}) this.setState({current: 2, userId: res.data, username: res.data.split("/")[1]})
} else { } else {
Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); Setting.showMessage("error", i18next.t(`signup:${res.msg}`));
} }
}) })
break
}
} }
onFinish(values) { onFinish(values) {
@ -134,19 +156,16 @@ class ForgetPage extends React.Component {
onFinishFailed(values, errorFields) {} onFinishFailed(values, errorFields) {}
onChange = (current) => {
this.setState({ current: current });
};
renderForm(application) { renderForm(application) {
return ( return (
<> <Form.Provider onFormFinish={(name, {info, forms}) => {
this.onFormFinish(name, info, forms);
}}>
{/* STEP 1: input username -> get email & phone */} {/* STEP 1: input username -> get email & phone */}
<Form <Form
hidden={this.state.current !== 0} hidden={this.state.current !== 0}
ref={this.form} ref={this.form}
name="get-email-and-Phone" name="step1"
onFinish={(values) => this.onFinishStep1(values)}
onFinishFailed={(errorInfo) => console.log(errorInfo)} onFinishFailed={(errorInfo) => console.log(errorInfo)}
initialValues={{ initialValues={{
application: application.name, application: application.name,
@ -198,7 +217,7 @@ class ForgetPage extends React.Component {
}); });
}} }}
prefix={<UserOutlined />} prefix={<UserOutlined />}
placeholder={i18next.t("signup:Username")} placeholder={i18next.t("signup:username, Email or phone")}
/> />
</Form.Item> </Form.Item>
<br /> <br />
@ -213,8 +232,7 @@ class ForgetPage extends React.Component {
<Form <Form
hidden={this.state.current !== 1} hidden={this.state.current !== 1}
ref={this.form} ref={this.form}
name="forgetPassword" name="step2"
onFinish={(values) => this.onFinishStep2(values)}
onFinishFailed={(errorInfo) => onFinishFailed={(errorInfo) =>
this.onFinishFailed( this.onFinishFailed(
errorInfo.values, errorInfo.values,
@ -258,6 +276,8 @@ class ForgetPage extends React.Component {
validateFirst validateFirst
hasFeedback hasFeedback
> >
{
this.state.isFixed ? <Input disabled/> :
<Select <Select
disabled={this.state.username === ""} disabled={this.state.username === ""}
placeholder={i18next.t( placeholder={i18next.t(
@ -283,6 +303,7 @@ class ForgetPage extends React.Component {
this.state.email.replace(/(\w?@)/, "*@")} this.state.email.replace(/(\w?@)/, "*@")}
</Option> </Option>
</Select> </Select>
}
</Form.Item> </Form.Item>
<Form.Item <Form.Item
name="emailCode" //use emailCode instead of email/phoneCode to adapt to RequestForm in account.go name="emailCode" //use emailCode instead of email/phoneCode to adapt to RequestForm in account.go
@ -332,7 +353,6 @@ class ForgetPage extends React.Component {
<Button <Button
block block
type="primary" type="primary"
disabled={this.state.phone === "" || this.state.verifyType === ""}
htmlType="submit" htmlType="submit"
> >
{i18next.t("forget:Next Step")} {i18next.t("forget:Next Step")}
@ -344,7 +364,7 @@ class ForgetPage extends React.Component {
<Form <Form
hidden={this.state.current !== 2} hidden={this.state.current !== 2}
ref={this.form} ref={this.form}
name="forgetPassword" name="step3"
onFinish={(values) => this.onFinish(values)} onFinish={(values) => this.onFinish(values)}
onFinishFailed={(errorInfo) => onFinishFailed={(errorInfo) =>
this.onFinishFailed( this.onFinishFailed(
@ -441,9 +461,9 @@ class ForgetPage extends React.Component {
</Button> </Button>
</Form.Item> </Form.Item>
</Form> </Form>
</> </Form.Provider>
); );
} }
render() { render() {
const application = this.getApplicationObj(); const application = this.getApplicationObj();
@ -460,7 +480,6 @@ class ForgetPage extends React.Component {
<Col span={24} style={{ display: "flex", justifyContent: "center" }}> <Col span={24} style={{ display: "flex", justifyContent: "center" }}>
<Steps <Steps
current={this.state.current} current={this.state.current}
onChange={this.onChange}
style={{ style={{
width: "90%", width: "90%",
maxWidth: "500px", maxWidth: "500px",