From 50aadaf0ddd64db120d79287a0d7490758e60ba9 Mon Sep 17 00:00:00 2001 From: kennedy omondi Date: Fri, 6 Sep 2024 13:09:30 +0300 Subject: [PATCH] feat: adding reset password endpoint --- .github/workflows/ci.yml | 3 ++- client.go | 33 +++++++++++++++++++++++++++++++++ models.go | 12 ++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 24a69c8..5fdac23 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,7 @@ jobs: - name: Run lint uses: golangci/golangci-lint-action@v3 with: - version: latest + version: v1.57 - name: Install Go dependencies run: | @@ -30,6 +30,7 @@ jobs: go get github.com/ory/go-acc@latest go install github.com/ory/go-acc@latest go install github.com/axw/gocov/gocov@latest + go get github.com/savannahghi/firebasetools@v0.0.19 - name: Run test run: | diff --git a/client.go b/client.go index a315494..773b6a4 100644 --- a/client.go +++ b/client.go @@ -190,6 +190,39 @@ func (c *Client) LoginUser(ctx context.Context, input *LoginUserPayload) (*OAUTH return responseData, nil } +// ResetPassword is used to reset a user's password on AuthServer +func (c *Client) ResetPassword(ctx context.Context, payload *PasswordResetPayload) (*PasswordResetResponse, error) { + url := fmt.Sprintf("%s/accounts/password/reset/", c.configurations.AuthServerEndpoint) + + resp, err := c.makeRequest(ctx, http.MethodPost, url, &payload, "application/json", true, nil) + if err != nil { + return nil, err + } + + defer resp.Body.Close() + + respData, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + if resp.StatusCode != 200 { + msg := fmt.Sprintf( + "unable to send password reset instructions. Details: %v", + string(respData), + ) + return nil, fmt.Errorf(msg) + } + + var message PasswordResetResponse + err = json.Unmarshal(respData, &message) + if err != nil { + return nil, err + } + + return &message, nil +} + // ValidateUser validates whether a user exists on the authserver func (c *Client) ValidateUser(ctx context.Context, authTokens *OAUTHResponse) (*MeResponse, error) { meURL := fmt.Sprintf("%s/v1/user/me/", c.configurations.AuthServerEndpoint) diff --git a/models.go b/models.go index 4455d8f..2b2b11e 100644 --- a/models.go +++ b/models.go @@ -86,6 +86,18 @@ type CreateUserResponse struct { UserRoles []interface{} `json:"user_roles"` } +// PassworResetHeaders defines the object needed when making a password reset request +type PasswordResetPayload struct { + Origin string `json:"origin"` + Variant string `json:"variant"` + Email string `json:"email"` +} + +// ResetPasswordResponse defines return the json object returned when password reset instruction has been sent +type PasswordResetResponse struct { + Detail string `json:"detail"` +} + // LoginUserPayload defines the object passed when a user logs in type LoginUserPayload struct { Email string `json:"email" validate:"required,email"`