Skip to content

Commit

Permalink
feat: adding reset password endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kennedy omondi committed Sep 6, 2024
1 parent f93e5be commit 7c8df25
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
33 changes: 33 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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. detail: %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)
Expand Down
12 changes: 12 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 7c8df25

Please sign in to comment.