-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add sign in token api * bad copy paste ): * address PR feedback - rename package - use pointer for URL as it can be null - remove references to 'session'
- Loading branch information
1 parent
12b399e
commit d818ba3
Showing
4 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package clerk | ||
|
||
type SignInToken struct { | ||
APIResource | ||
Object string `json:"object"` | ||
ID string `json:"id"` | ||
Status string `json:"status"` | ||
UserID string `json:"user_id"` | ||
Token string `json:"token,omitempty"` | ||
URL *string `json:"url,omitempty"` | ||
CreatedAt int64 `json:"created_at"` | ||
UpdatedAt int64 `json:"updated_at"` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Package signintoken provides the Sign-In Token API. | ||
package signintoken | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/clerk/clerk-sdk-go/v2" | ||
) | ||
|
||
//go:generate go run ../cmd/gen/main.go | ||
|
||
const path = "/sign_in_tokens" | ||
|
||
// Client is used to invoke the Sign-In Token API. | ||
type Client struct { | ||
Backend clerk.Backend | ||
} | ||
|
||
func NewClient(config *clerk.ClientConfig) *Client { | ||
return &Client{ | ||
Backend: clerk.NewBackend(&config.BackendConfig), | ||
} | ||
} | ||
|
||
type CreateParams struct { | ||
clerk.APIParams | ||
UserID *string `json:"user_id,omitempty"` | ||
ExpiresInSeconds *int64 `json:"expires_in_seconds,omitempty"` | ||
} | ||
|
||
// Create creates a new sign-in token. | ||
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.SignInToken, error) { | ||
req := clerk.NewAPIRequest(http.MethodPost, path) | ||
req.SetParams(params) | ||
token := &clerk.SignInToken{} | ||
err := c.Backend.Call(ctx, req, token) | ||
return token, err | ||
} | ||
|
||
// Revoke revokes a pending sign-in token. | ||
func (c *Client) Revoke(ctx context.Context, id string) (*clerk.SignInToken, error) { | ||
token := &clerk.SignInToken{} | ||
path, err := clerk.JoinPath(path, id, "revoke") | ||
if err != nil { | ||
return token, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodPost, path) | ||
err = c.Backend.Call(ctx, req, token) | ||
return token, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package signintoken | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/clerk/clerk-sdk-go/v2" | ||
"github.com/clerk/clerk-sdk-go/v2/clerktest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSignInTokenTokenCreate(t *testing.T) { | ||
userID := "usr_123" | ||
id := "sign_123" | ||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
HTTPClient: &http.Client{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
In: json.RawMessage(fmt.Sprintf(`{"user_id":"%s"}`, userID)), | ||
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","user_id":"%s"}`, id, userID)), | ||
Path: "/v1/sign_in_tokens", | ||
Method: http.MethodPost, | ||
}, | ||
}, | ||
})) | ||
|
||
signInToken, err := Create(context.Background(), &CreateParams{ | ||
UserID: clerk.String(userID), | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, id, signInToken.ID) | ||
require.Equal(t, userID, signInToken.UserID) | ||
} | ||
|
||
func TestSignInTokenRevoke(t *testing.T) { | ||
id := "sign_456" | ||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
HTTPClient: &http.Client{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","status":"revoked"}`, id)), | ||
Path: fmt.Sprintf("/v1/sign_in_tokens/%s/revoke", id), | ||
Method: http.MethodPost, | ||
}, | ||
}, | ||
})) | ||
|
||
signInToken, err := Revoke(context.Background(), id) | ||
require.NoError(t, err) | ||
require.Equal(t, id, signInToken.ID) | ||
require.Equal(t, "revoked", signInToken.Status) | ||
} |