Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement reset password functionality #5

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/schema/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
37 changes: 33 additions & 4 deletions internal/services/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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")
}