diff --git a/internal/schema/user.go b/internal/schema/user.go index 8b474e3..5365d8c 100644 --- a/internal/schema/user.go +++ b/internal/schema/user.go @@ -26,6 +26,11 @@ type LoginResponse struct { ExpiresAt int64 `json:"expires_at"` } +// ResetPasswordResponse Reset Password Response +type ResetPasswordResponse struct { + Message string `json:"message"` +} + // ChangePasswordRequest request to change a password type ChangePasswordRequest struct { UserID int64 `json:"user_id" validate:"required"` diff --git a/internal/services/auth.go b/internal/services/auth.go index ae5584c..00b8254 100644 --- a/internal/services/auth.go +++ b/internal/services/auth.go @@ -15,7 +15,7 @@ import ( type AuthService interface { SignIn(*schema.LoginRequest) (*schema.LoginResponse, error) - ResetPassword(ctx context.Context, email string) error + ResetPassword(email string) (*schema.ResetPasswordResponse, error) ChangePassword(ctx context.Context, request *schema.ChangePasswordRequest) error } @@ -74,10 +74,39 @@ func (a *authServiceImpl) SignIn(request *schema.LoginRequest) (*schema.LoginRes return res, nil } -func (a *authServiceImpl) ResetPassword(ctx context.Context, email string) error { - panic("not implemented") -} +func (a *authServiceImpl) ResetPassword(email string) (*schema.ResetPasswordResponse, error) { + + user, err := a.queries.FindUserLoginByEmail(context.Background(), email) + + if err != nil { + a.logger.Error("auth-service", "failed to process login request", "error", err) + return nil, err + } + + //TODO:: move this method to its own file + token := jwt.New(jwt.SigningMethodHS256) + claims := token.Claims.(jwt.MapClaims) + claims["UserID"] = user.ID + claims["Email"] = user.Email + claims["exp"] = time.Now().Add(time.Hour * 24).Unix() + tokenStr, err := token.SignedString([]byte(a.authConfig.JwtSecretKey)) + + if err != nil { + a.logger.Error("auth-service", "failed to create a token", "error", err) + return nil, fmt.Errorf("failed to generate auth token, got: %v", err) + } + + print(tokenStr) + + // send email with token + // emailService.SendResetPasswordEmail(email, tokenStr) + + return &schema.ResetPasswordResponse{ + Message: "Password reset link has been sent to your email", + }, nil + +} func (a *authServiceImpl) ChangePassword(ctx context.Context, request *schema.ChangePasswordRequest) error { panic("not implemented") }