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

add sign-in token api #340

Merged
merged 3 commits into from
Oct 23, 2024
Merged
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
13 changes: 13 additions & 0 deletions sign_in_token.go
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"`
}
25 changes: 25 additions & 0 deletions signintoken/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions signintoken/client.go
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
}
55 changes: 55 additions & 0 deletions signintoken/sign_in_tokens_test.go
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)
}
Loading