feat: add "forget password" [front & backend] (#75)

* feat: add "forget password" [front & backend]

Signed-off-by: Weihao <1340908470@qq.com>

* fix: verification code can be sent even if no mobile phone or email is selected
refactor: forgetPassword -> forget; GetEmailAndPhoneByUsername -> GetEmailAndPhone; remove useless note

Signed-off-by: Weihao <1340908470@qq.com>
This commit is contained in:
Weihao Chen
2021-06-02 13:39:01 +08:00
committed by GitHub
parent 29049297d8
commit 1cb5ae54c5
11 changed files with 716 additions and 7 deletions

View File

@ -111,6 +111,43 @@ func (c *ApiController) DeleteUser() {
c.ServeJSON()
}
// @Title GetEmailAndPhone
// @Description get email and phone by username
// @Param username formData string true "The username of the user"
// @Param organization formData string true "The organization of the user"
// @Success 200 {object} controllers.Response The Response object
// @router /get-email-and-phone [post]
func (c *ApiController) GetEmailAndPhone() {
var resp Response
var form RequestForm
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
if err != nil {
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)
if user == nil {
c.ResponseError("No such user.")
return
}
phone := user.Phone
email := user.Email
resp = Response{Status: "ok", Msg: "", Data: phone, Data2: email}
c.Data["json"] = resp
c.ServeJSON()
}
// @Title SetPassword
// @Description set password
// @Param userOwner formData string true "The owner of the user"
@ -158,10 +195,14 @@ func (c *ApiController) SetPassword() {
return
}
msg := object.CheckPassword(targetUser, oldPassword)
if msg != "" {
c.ResponseError(msg)
return
if oldPassword != "" {
msg := object.CheckPassword(targetUser, oldPassword)
if msg != "" {
c.ResponseError(msg)
return
}
} else {
}
if strings.Index(newPassword, " ") >= 0 {
@ -174,6 +215,8 @@ func (c *ApiController) SetPassword() {
return
}
c.SetSessionUser("")
targetUser.Password = newPassword
object.SetUserField(targetUser, "password", targetUser.Password)
c.Data["json"] = Response{Status: "ok"}