Skip to content

Commit 5b1772c

Browse files
authored
refactor: swaps implementation from deprecated methods to new methods (#112)
1 parent ab34bf9 commit 5b1772c

10 files changed

+606
-715
lines changed

app.go

+47-53
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,41 @@ package passage
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
"github.com/lestrrat-go/jwx/v2/jwk"
89
)
910

1011
const jwksUrl = "https://auth.passage.id/v1/apps/%v/.well-known/jwks.json"
1112

13+
type Passage = App
14+
15+
// Config holds the configuration for the Passage SDK.
16+
//
17+
// Deprecated: will be removed in v2.
1218
type Config struct {
1319
APIKey string
1420
HeaderAuth bool
1521
}
1622

17-
// Deprecated: will be renamed to `Passage` in v2
23+
// App is the main struct for the Passage SDK.
24+
//
25+
// Deprecated: will be renamed to `Passage` in v2.
1826
type App struct {
27+
// Deprecated: will be removed in v2.
1928
ID string
20-
// Deprecated
21-
Config *Config
22-
User *appUser
23-
client *ClientWithResponses
24-
jwksCacheSet jwk.Set
29+
// Deprecated: will be removed in v2.
30+
Config *Config
31+
Auth *auth
32+
User *user
33+
client *ClientWithResponses
2534
}
2635

27-
// Deprecated: Will be replaced with a different signature in v2
28-
func New(appID string, config *Config) (*App, error) {
36+
// New creates a new Passage instance.
37+
//
38+
// Deprecated: Will be replaced with a different signature in v2 -- `New(appID string, apiKey string) (*Passage, error)`.
39+
func New(appID string, config *Config) (*Passage, error) {
2940
if config == nil {
3041
config = &Config{}
3142
}
@@ -39,12 +50,6 @@ func New(appID string, config *Config) (*App, error) {
3950
return nil, err
4051
}
4152

42-
app := App{
43-
ID: appID,
44-
Config: config,
45-
client: client,
46-
}
47-
4853
url := fmt.Sprintf(jwksUrl, appID)
4954
cache := jwk.NewCache(context.Background())
5055
if err := cache.Register(url); err != nil {
@@ -55,17 +60,24 @@ func New(appID string, config *Config) (*App, error) {
5560
return nil, Error{Message: "failed to fetch jwks"}
5661
}
5762

58-
app.jwksCacheSet = jwk.NewCachedSet(cache, url)
59-
60-
app.User = newAppUser(app)
63+
auth, err := newAuth(appID, client)
64+
if err != nil {
65+
return nil, err
66+
}
67+
user := newUser(appID, client)
6168

62-
return &app, nil
69+
return &App{
70+
ID: appID,
71+
Config: config,
72+
Auth: auth,
73+
User: user,
74+
client: client,
75+
}, nil
6376
}
6477

65-
// GetApp gets information about an app
66-
// returns App on success, error on failure
78+
// GetApp fetches the Passage app info.
6779
//
68-
// Deprecated: GetApp - this method will not be replaced
80+
// Deprecated: will be removed in v2.
6981
func (a *App) GetApp() (*AppInfo, error) {
7082
res, err := a.client.GetAppWithResponse(context.Background(), a.ID)
7183
if err != nil {
@@ -99,40 +111,22 @@ func (a *App) GetApp() (*AppInfo, error) {
99111
}
100112
}
101113

102-
// CreateMagicLink receives a CreateMagicLinkBody struct, creating a magic link with provided values
103-
// returns MagicLink on success, error on failure
114+
// CreateMagicLink creates a Magic Link for your app.
115+
//
116+
// Deprecated: use `Passage.Auth.CreateMagicLink` instead.
104117
func (a *App) CreateMagicLink(createMagicLinkBody CreateMagicLinkBody) (*MagicLink, error) {
105-
res, err := a.client.CreateMagicLinkWithResponse(context.Background(), a.ID, createMagicLinkBody)
118+
magicLink, err := a.Auth.CreateMagicLink(createMagicLinkBody)
106119
if err != nil {
107-
return nil, Error{Message: "network error: failed to create Passage Magic Link"}
120+
var passageError PassageError
121+
if errors.As(err, &passageError) {
122+
return magicLink, Error{
123+
ErrorText: passageError.Message,
124+
ErrorCode: passageError.ErrorCode,
125+
Message: passageError.Message,
126+
StatusCode: passageError.StatusCode,
127+
}
128+
}
108129
}
109130

110-
if res.JSON201 != nil {
111-
return &res.JSON201.MagicLink, nil
112-
}
113-
114-
var errorText string
115-
var errorCode string
116-
switch {
117-
case res.JSON400 != nil:
118-
errorText = res.JSON400.Error
119-
errorCode = string(res.JSON400.Code)
120-
case res.JSON401 != nil:
121-
errorText = res.JSON401.Error
122-
errorCode = string(res.JSON401.Code)
123-
case res.JSON404 != nil:
124-
errorText = res.JSON404.Error
125-
errorCode = string(res.JSON404.Code)
126-
case res.JSON500 != nil:
127-
errorText = res.JSON500.Error
128-
errorCode = string(res.JSON500.Code)
129-
}
130-
131-
return nil, Error{
132-
Message: "failed to create Passage Magic Link",
133-
StatusCode: res.StatusCode(),
134-
StatusText: res.Status(),
135-
ErrorText: errorText,
136-
ErrorCode: errorCode,
137-
}
131+
return magicLink, err
138132
}

0 commit comments

Comments
 (0)