Skip to content

Commit

Permalink
✨ feat: support reset password by email (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwnmengjing authored Aug 14, 2024
1 parent f4a27c3 commit 5c5bf4b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ test:
go test -coverprofile=coverage.out ./...
deps:
go mod download
generate:
go generate ./...

.PHONY: lint
lint:
Expand Down
42 changes: 42 additions & 0 deletions apis/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type User struct {
// Other handler
func (e *User) Other(r *gin.RouterGroup) {
r.POST("/user/#", middleware.Auth.LoginHandler)
r.POST("/user/reset-password", e.ResetPassword)
r.POST("/user/fakeCaptcha", e.FakeCaptcha)
r.POST("/user/#/github", middleware.Auth.LoginHandler)
r.GET("/user/refresh-token", middleware.Auth.RefreshHandler)
Expand All @@ -58,6 +59,47 @@ func (e *User) Other(r *gin.RouterGroup) {
r.POST("/user/avatar", middleware.Auth.MiddlewareFunc(), e.UpdateAvatar)
}

// ResetPassword 重置密码
// @Summary 重置密码
// @Description 重置密码
// @Tags user
// @Accept application/json
// @Product application/json
// @Param data body dto.ResetPasswordRequest true "data"
// @Success 200
// @Router /admin/api/user/reset-password [post]
func (e *User) ResetPassword(ctx *gin.Context) {
api := response.Make(ctx)
req := &dto.ResetPasswordRequest{}
if api.Bind(req).Error != nil {
api.Err(http.StatusUnprocessableEntity)
return
}
ok, err := center.Default.VerifyCode(ctx, req.Email, req.Captcha)
if err != nil {
api.AddError(err).Log.Error("VerifyCode error")
api.Err(http.StatusInternalServerError)
return
}
if !ok {
api.Err(http.StatusForbidden)
return
}
user, err := models.GetUserByEmail(ctx, req.Email)
if err != nil {
api.AddError(err).Log.Error("GetUser error")
api.Err(http.StatusInternalServerError)
return
}
err = models.PasswordReset(ctx, user.ID, req.Password)
if err != nil {
api.AddError(err).Log.Error("PasswordReset error")
api.Err(http.StatusInternalServerError)
return
}
api.OK(nil)
}

func (e *User) UpdateAvatar(ctx *gin.Context) {
api := response.Make(ctx)
verify := middleware.GetVerify(ctx)
Expand Down
6 changes: 6 additions & 0 deletions dto/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (
* @Last Modified time: 2023/8/6 22:16:32
*/

type ResetPasswordRequest struct {
Email string `json:"email" binding:"required"`
Captcha string `json:"captcha" binding:"required"`
Password string `json:"password" binding:"required"`
}

type UserSearch struct {
actions.Pagination `search:"inline"`
// ID
Expand Down
1 change: 1 addition & 0 deletions storage/cache/verify_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ func (v *VerifyCode) VerifyCode(ctx context.Context, key, code string) (bool, er
if s == "" {
return false, nil
}
_ = v.Cache.Del(ctx, fmt.Sprintf("verify-code-%s", key))
return s == code, nil
}

0 comments on commit 5c5bf4b

Please # to comment.