password reset fix
This commit is contained in:
parent
89c91484d1
commit
66b1b52d55
|
|
@ -25,3 +25,9 @@ type TeamResetPasswordReq struct {
|
|||
Otp string `json:"otp" validate:"required"`
|
||||
Password string `json:"password" validate:"required,min=8"`
|
||||
}
|
||||
|
||||
// TeamVerifyPasswordResetRes is returned by GET /team/verifyResetCode (does not consume the OTP).
|
||||
type TeamVerifyPasswordResetRes struct {
|
||||
Valid bool `json:"valid"`
|
||||
Reason string `json:"reason,omitempty"` // missing | invalid | used | expired
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,6 +63,35 @@ func (s *Service) SendPasswordReset(ctx context.Context, email string) error {
|
|||
})
|
||||
}
|
||||
|
||||
func (s *Service) VerifyPasswordReset(ctx context.Context, email, otpCode string) domain.TeamVerifyPasswordResetRes {
|
||||
email = strings.TrimSpace(strings.ToLower(email))
|
||||
otpCode = strings.TrimSpace(otpCode)
|
||||
if email == "" || otpCode == "" {
|
||||
return domain.TeamVerifyPasswordResetRes{Valid: false, Reason: "missing"}
|
||||
}
|
||||
|
||||
member, err := s.teamStore.GetTeamMemberByEmail(ctx, email)
|
||||
if err != nil {
|
||||
return domain.TeamVerifyPasswordResetRes{Valid: false, Reason: "invalid"}
|
||||
}
|
||||
|
||||
otp, err := s.teamStore.GetTeamResetOtpByCode(ctx, member.ID, otpCode)
|
||||
if err != nil {
|
||||
return domain.TeamVerifyPasswordResetRes{Valid: false, Reason: "invalid"}
|
||||
}
|
||||
if otp.For != domain.OtpReset {
|
||||
return domain.TeamVerifyPasswordResetRes{Valid: false, Reason: "invalid"}
|
||||
}
|
||||
if otp.Used {
|
||||
return domain.TeamVerifyPasswordResetRes{Valid: false, Reason: "used"}
|
||||
}
|
||||
if time.Now().After(otp.ExpiresAt) {
|
||||
return domain.TeamVerifyPasswordResetRes{Valid: false, Reason: "expired"}
|
||||
}
|
||||
|
||||
return domain.TeamVerifyPasswordResetRes{Valid: true}
|
||||
}
|
||||
|
||||
func (s *Service) ResetPasswordWithOtp(ctx context.Context, req domain.TeamResetPasswordReq) error {
|
||||
email := strings.TrimSpace(strings.ToLower(req.Email))
|
||||
otpCode := strings.TrimSpace(req.Otp)
|
||||
|
|
|
|||
|
|
@ -8,6 +8,26 @@ import (
|
|||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// VerifyTeamPasswordReset godoc
|
||||
// @Summary Verify team password reset link
|
||||
// @Description Checks whether email+otp from a reset link are still valid without consuming the OTP. Public.
|
||||
// @Tags team
|
||||
// @Produce json
|
||||
// @Param email query string true "Team member email"
|
||||
// @Param otp query string true "Reset OTP from the email link"
|
||||
// @Success 200 {object} domain.Response
|
||||
// @Router /api/v1/team/verifyResetCode [get]
|
||||
func (h *Handler) VerifyTeamPasswordReset(c *fiber.Ctx) error {
|
||||
email := c.Query("email")
|
||||
otp := c.Query("otp")
|
||||
res := h.teamSvc.VerifyPasswordReset(c.Context(), email, otp)
|
||||
return c.JSON(domain.Response{
|
||||
Message: "Password reset link checked",
|
||||
Success: true,
|
||||
Data: res,
|
||||
})
|
||||
}
|
||||
|
||||
// SendTeamPasswordReset godoc
|
||||
// @Summary Send team password reset link
|
||||
// @Description Sends a password-reset email with a link to the team member (admin panel). Public.
|
||||
|
|
|
|||
|
|
@ -471,6 +471,7 @@ func (a *App) initAppRoutes() {
|
|||
teamGroup.Post("/login", h.TeamMemberLogin)
|
||||
teamGroup.Post("/refresh", h.TeamMemberRefresh)
|
||||
teamGroup.Post("/sendResetCode", h.SendTeamPasswordReset)
|
||||
teamGroup.Get("/verifyResetCode", h.VerifyTeamPasswordReset)
|
||||
teamGroup.Post("/resetPassword", h.ResetTeamPassword)
|
||||
teamGroup.Get("/invitations/verify", h.VerifyTeamInvitation)
|
||||
teamGroup.Post("/invitations/accept", h.AcceptTeamInvitation)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user