基本完成重置密码和忘记密码
This commit is contained in:
		
							parent
							
								
									ae07370214
								
							
						
					
					
						commit
						117330ee16
					
				| @ -14,7 +14,7 @@ import ( | |||||||
| func UserResetPasswordHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | func UserResetPasswordHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||||||
| 	return func(w http.ResponseWriter, r *http.Request) { | 	return func(w http.ResponseWriter, r *http.Request) { | ||||||
| 
 | 
 | ||||||
| 		var req types.RequestUserLogin | 		var req types.RequestUserResetPassword | ||||||
| 		userinfo, err := basic.RequestParse(w, r, svcCtx, &req) | 		userinfo, err := basic.RequestParse(w, r, svcCtx, &req) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return | 			return | ||||||
|  | |||||||
| @ -1,6 +1,8 @@ | |||||||
| package logic | package logic | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"fusenapi/model/gmodel" | ||||||
| 	"fusenapi/utils/auth" | 	"fusenapi/utils/auth" | ||||||
| 	"fusenapi/utils/basic" | 	"fusenapi/utils/basic" | ||||||
| 
 | 
 | ||||||
| @ -10,6 +12,7 @@ import ( | |||||||
| 	"fusenapi/server/auth/internal/types" | 	"fusenapi/server/auth/internal/types" | ||||||
| 
 | 
 | ||||||
| 	"github.com/zeromicro/go-zero/core/logx" | 	"github.com/zeromicro/go-zero/core/logx" | ||||||
|  | 	"gorm.io/gorm" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type UserResetPasswordLogic struct { | type UserResetPasswordLogic struct { | ||||||
| @ -30,10 +33,41 @@ func NewUserResetPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) | |||||||
| // func (l *UserResetPasswordLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { | // func (l *UserResetPasswordLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { | ||||||
| // } | // } | ||||||
| 
 | 
 | ||||||
| func (l *UserResetPasswordLogic) UserResetPassword(req *types.RequestUserLogin, userinfo *auth.UserInfo) (resp *basic.Response) { | func (l *UserResetPasswordLogic) UserResetPassword(req *types.RequestUserResetPassword, userinfo *auth.UserInfo) (resp *basic.Response) { | ||||||
| 	// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) | 	// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) | ||||||
| 	// userinfo 传入值时, 一定不为null | 	// userinfo 传入值时, 一定不为null | ||||||
| 
 | 
 | ||||||
|  | 	if !userinfo.IsUser() { | ||||||
|  | 		return resp.SetStatus(basic.CodeUnAuth) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	rt, err := l.svcCtx.ResetTokenManger.Decrypt(req.ResetToken) | ||||||
|  | 	if err != nil { | ||||||
|  | 		logx.Error(err) | ||||||
|  | 		return resp.SetStatus(basic.CodeOAuthResetTokenDecryptErr, err.Error()) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// TODO: 存储 | ||||||
|  | 	if rt.OperateType != auth.OpTypeResetToken { | ||||||
|  | 		return resp.SetStatus(basic.CodeOAuthTypeErr, "error OperateType: rt.OperateType != auth.OpTypeResetToken") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	err = l.svcCtx.AllModels.FsUser.Transaction(l.ctx, func(tx *gorm.DB) error { | ||||||
|  | 		user := &gmodel.FsUser{Id: int64(rt.UserId)} | ||||||
|  | 		err := tx.Take(user).Error | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 		if *user.PasswordHash != rt.OldPassword { | ||||||
|  | 			return fmt.Errorf("password had beed updated") | ||||||
|  | 		} | ||||||
|  | 		return tx.Update("PasswordHash", req.Password).Error | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | 	if err != nil { | ||||||
|  | 		return resp.SetStatus(basic.CodeDbSqlErr, err.Error()) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	return resp.SetStatus(basic.CodeOK) | 	return resp.SetStatus(basic.CodeOK) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -50,13 +50,21 @@ func (l *UserResetTokenLogic) UserResetToken(req *types.RequestUserResetToken, u | |||||||
| 		UserId:      uint64(userinfo.UserId), | 		UserId:      uint64(userinfo.UserId), | ||||||
| 		Wid:         req.Wid, | 		Wid:         req.Wid, | ||||||
| 		Email:       *user.Email, | 		Email:       *user.Email, | ||||||
| 		Password:    *user.PasswordHash, | 		OldPassword: *user.PasswordHash, | ||||||
| 		CreateAt:    time.Now(), | 		CreateAt:    time.Now(), | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	l.svcCtx.ResetTokenManger.Encrypt(token) | 	rtoken, err := l.svcCtx.ResetTokenManger.Encrypt(token) | ||||||
|  | 	if err != nil { | ||||||
|  | 		logx.Error(err) | ||||||
|  | 		return resp.SetStatus(basic.CodeOAuthResetTokenEncryptErr, err.Error()) | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	return resp.SetStatus(basic.CodeOK) | 	data := types.DataResetToken{ | ||||||
|  | 		ResetToken: rtoken, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return resp.SetStatus(basic.CodeOK, data) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 | // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 | ||||||
|  | |||||||
| @ -29,7 +29,7 @@ service auth { | |||||||
| 	get /api/auth/reset/token(RequestUserResetToken) returns (response); | 	get /api/auth/reset/token(RequestUserResetToken) returns (response); | ||||||
| 	 | 	 | ||||||
| 	@handler UserResetPasswordHandler | 	@handler UserResetPasswordHandler | ||||||
| 	post /api/auth/reset/password(RequestUserLogin) returns (response); | 	post /api/auth/reset/password(RequestUserResetPassword) returns (response); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type ( | type ( | ||||||
|  | |||||||
| @ -28,7 +28,7 @@ type ResetToken struct { | |||||||
| 	UserId      uint64    // guest_id 需要继承 | 	UserId      uint64    // guest_id 需要继承 | ||||||
| 	Wid         string    // websocket 通道id | 	Wid         string    // websocket 通道id | ||||||
| 	Email       string    // email | 	Email       string    // email | ||||||
| 	Password    string    // 密码 | 	OldPassword string    // 旧密码 | ||||||
| 	CreateAt    time.Time // 创建时间 | 	CreateAt    time.Time // 创建时间 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -44,6 +44,9 @@ var ( | |||||||
| 	CodeOAuthEmailErr               = &StatusResponse{5072, "Invalid email format"} | 	CodeOAuthEmailErr               = &StatusResponse{5072, "Invalid email format"} | ||||||
| 	CodeOAuthRandReaderErr          = &StatusResponse{5073, "rand reader error"} | 	CodeOAuthRandReaderErr          = &StatusResponse{5073, "rand reader error"} | ||||||
| 	CodeOAuthConfirmationTimeoutErr = &StatusResponse{5074, "confirmation timeout error"} | 	CodeOAuthConfirmationTimeoutErr = &StatusResponse{5074, "confirmation timeout error"} | ||||||
|  | 	CodeOAuthResetTokenEncryptErr   = &StatusResponse{5075, "oauth2 reset  token encrypt error"} | ||||||
|  | 	CodeOAuthResetTokenDecryptErr   = &StatusResponse{5076, "oauth2 reset  token decrypt error"} | ||||||
|  | 	CodeOAuthTypeErr                = &StatusResponse{5077, "oauth2 token operator error"} | ||||||
| 
 | 
 | ||||||
| 	CodeS3PutObjectRequestErr = &StatusResponse{5060, "s3 PutObjectRequest error"}    // s3 PutObjectRequest 错误 | 	CodeS3PutObjectRequestErr = &StatusResponse{5060, "s3 PutObjectRequest error"}    // s3 PutObjectRequest 错误 | ||||||
| 	CodeS3PutSizeLimitErr     = &StatusResponse{5061, "s3 over limit size error"}     // s3 超过文件大小限制 错误 | 	CodeS3PutSizeLimitErr     = &StatusResponse{5061, "s3 over limit size error"}     // s3 超过文件大小限制 错误 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user