Skip to content

Commit

Permalink
Implement and test GET /reauthenticate
Browse files Browse the repository at this point in the history
  • Loading branch information
kwoodhouse93 committed Nov 4, 2022
1 parent d200a8f commit 7771c77
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Required for V1 release:
- [ ] GET /verify
- [ ] POST /verify
- [X] POST /logout
- [ ] GET /reauthenticate
- [X] GET /reauthenticate
- [X] GET /user
- [X] PUT /user
- [ ] POST /factors
Expand Down
7 changes: 7 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ type Client interface {
// doesn't exist.
OTP(req types.OTPRequest) error

// GET /reauthenticate
//
// Sends a nonce to the user's email (preferred) or phone. This endpoint
// requires the user to be logged in / authenticated first. The user needs to
// have either an email or phone number for the nonce to be sent successfully.
Reauthenticate() error

// POST /recover
//
// Password recovery. Will deliver a password recovery mail to the user based
Expand Down
37 changes: 37 additions & 0 deletions endpoints/reauthenticate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package endpoints

import (
"fmt"
"io/ioutil"
"net/http"
)

const reauthenticatePath = "/reauthenticate"

// GET /reauthenticate
//
// Sends a nonce to the user's email (preferred) or phone. This endpoint
// requires the user to be logged in / authenticated first. The user needs to
// have either an email or phone number for the nonce to be sent successfully.
func (c *Client) Reauthenticate() error {
r, err := c.newRequest(reauthenticatePath, http.MethodGet, nil)
if err != nil {
return err
}

resp, err := c.client.Do(r)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
fullBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("response status code %d", resp.StatusCode)
}
return fmt.Errorf("response status code %d: %s", resp.StatusCode, fullBody)
}

return nil
}
33 changes: 33 additions & 0 deletions integration_test/reauthenticate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package integration_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/kwoodhouse93/gotrue-go/types"
)

func TestReauthenticate(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

// User must be authenticated first
err := client.Reauthenticate()
assert.Error(err)

client := autoconfirmClient

// Create a new user
email := randomEmail()
session, err := client.Signup(types.SignupRequest{
Email: email,
Password: "password",
})
require.NoError(err)

client = client.WithToken(session.AccessToken)
err = client.Reauthenticate()
assert.NoError(err)
}

0 comments on commit 7771c77

Please sign in to comment.