feat: won't send verification code if there is no account bounded to phone/email

Signed-off-by: Weihao <1340908470@qq.com>
This commit is contained in:
Weihao
2021-06-07 19:12:51 +08:00
parent eb658ad8ee
commit c983ee7ca6
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) {
if (res.status === "ok") { case "step1":
this.setState({ const username = forms.step1.getFieldValue("username")
username: values.username, AuthBackend.getEmailAndPhone({
phone: res.data.toString(), application: forms.step1.getFieldValue("application"),
email: res.data2.toString(), organization: forms.step1.getFieldValue("organization"),
current: 1, username: username
}); }).then((res) => {
} else { if (res.status === "ok") {
Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); 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) { 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,31 +276,34 @@ class ForgetPage extends React.Component {
validateFirst validateFirst
hasFeedback hasFeedback
> >
<Select {
disabled={this.state.username === ""} this.state.isFixed ? <Input disabled/> :
placeholder={i18next.t( <Select
"forget:Choose email verification or mobile verification" disabled={this.state.username === ""}
)} placeholder={i18next.t(
onChange={(value) => { "forget:Choose email verification or mobile verification"
if (value === this.state.phone) { )}
this.setState({ verifyType: "phone" }); onChange={(value) => {
} if (value === this.state.phone) {
if (value === this.state.email) { this.setState({ verifyType: "phone" });
this.setState({ verifyType: "email" }); }
} if (value === this.state.email) {
}} this.setState({ verifyType: "email" });
allowClear }
style={{ textAlign: "left" }} }}
> allowClear
<Option key={1} value={this.state.phone}> style={{ textAlign: "left" }}
{this.state.phone.replace(/(\d{3})\d*(\d{4})/,'$1****$2')} >
</Option> <Option key={1} value={this.state.phone}>
<Option key={2} value={this.state.email}> {this.state.phone.replace(/(\d{3})\d*(\d{4})/,'$1****$2')}
{this.state.email.split("@")[0].length>2? </Option>
this.state.email.replace(/(?<=.)[^@]+(?=.@)/, "*****"): <Option key={2} value={this.state.email}>
this.state.email.replace(/(\w?@)/, "*@")} {this.state.email.split("@")[0].length>2?
</Option> this.state.email.replace(/(?<=.)[^@]+(?=.@)/, "*****"):
</Select> this.state.email.replace(/(\w?@)/, "*@")}
</Option>
</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",