From c983ee7ca693c5462662e23191921b83ad86386f Mon Sep 17 00:00:00 2001 From: Weihao <1340908470@qq.com> Date: Mon, 7 Jun 2021 19:12:51 +0800 Subject: [PATCH] feat: won't send verification code if there is no account bounded to phone/email Signed-off-by: Weihao <1340908470@qq.com> --- controllers/user.go | 23 +++--- web/src/auth/ForgetPage.js | 151 +++++++++++++++++++++---------------- 2 files changed, 97 insertions(+), 77 deletions(-) diff --git a/controllers/user.go b/controllers/user.go index 91475c8c..fa4d10f7 100644 --- a/controllers/user.go +++ b/controllers/user.go @@ -126,23 +126,24 @@ func (c *ApiController) GetEmailAndPhone() { panic(err) } - // get user - var userId string - if form.Username == "" { - userId, _ = c.RequireSignedIn() - } else { - userId = fmt.Sprintf("%s/%s", form.Organization, form.Username) - } - user := object.GetUser(userId) + user := object.GetUserByFields(form.Organization, form.Username) if user == nil { c.ResponseError("No such user.") return } - phone := user.Phone - email := user.Email + respUser := object.User{Email: user.Email, Phone: user.Phone, Name: user.Name} + 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.ServeJSON() diff --git a/web/src/auth/ForgetPage.js b/web/src/auth/ForgetPage.js index a1d22555..cedad026 100644 --- a/web/src/auth/ForgetPage.js +++ b/web/src/auth/ForgetPage.js @@ -49,6 +49,8 @@ class ForgetPage extends React.Component { userId: "", username: "", email: "", + isFixed: false, + fixedContent: "", token: "", phone: "", emailCode: "", @@ -91,33 +93,53 @@ class ForgetPage extends React.Component { } } - onFinishStep1(values) { - AuthBackend.getEmailAndPhone(values).then((res) => { - if (res.status === "ok") { - this.setState({ - username: values.username, - phone: res.data.toString(), - email: res.data2.toString(), - current: 1, - }); - } else { - Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); + 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") { + this.setState({phone: res.data.phone, email: res.data.email, username: res.data.name}); + switch (res.data2) { + case "email": + this.setState({isFixed: true, fixedContent: res.data.email, verifyType: "email"}); + 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 { + Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); + } + }); + break; + case "step2": + const oAuthParams = Util.getOAuthGetParameters(); + 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") { + this.setState({current: 2, userId: res.data, username: res.data.split("/")[1]}) + } else { + Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); + } + }) + break } - }); - } - - onFinishStep2(values) { - values.phonePrefix = this.state.application?.organizationObj.phonePrefix; - values.username = this.state.username; - values.type = "login" - const oAuthParams = Util.getOAuthGetParameters(); - AuthBackend.login(values, oAuthParams).then(res => { - if (res.status === "ok") { - this.setState({current: 2, userId: res.data}) - } else { - Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); - } - }) } onFinish(values) { @@ -134,19 +156,16 @@ class ForgetPage extends React.Component { onFinishFailed(values, errorFields) {} - onChange = (current) => { - this.setState({ current: current }); - }; - renderForm(application) { return ( - <> +