Skip to content

Commit

Permalink
feat: reworks the new create magic link func into three separate func…
Browse files Browse the repository at this point in the history
…tions (#117)
  • Loading branch information
ctran88 authored Dec 12, 2024
1 parent e61d115 commit 741b260
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
5 changes: 3 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ func (a *App) GetApp() (*AppInfo, error) {

// CreateMagicLink creates a Magic Link for your app.
//
// Deprecated: use `Passage.Auth.CreateMagicLink` instead.
// Deprecated: use `Passage.Auth.CreateMagicLinkWithEmail`, `Passage.Auth.CreateMagicLinkWithPhone`,
// or `Passage.Auth.CreateMagicLinkWithUser` instead.
func (a *App) CreateMagicLink(createMagicLinkBody CreateMagicLinkBody) (*MagicLink, error) {
magicLink, err := a.Auth.CreateMagicLink(createMagicLinkBody)
magicLink, err := a.Auth.createMagicLink(createMagicLinkBody, nil)
if err != nil {
var passageError PassageError
if errors.As(err, &passageError) {
Expand Down
81 changes: 73 additions & 8 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
"github.com/lestrrat-go/jwx/v2/jwk"
)

type MagicLinkOptions struct {
Language string
MagicLinkPath string
RedirectURL string
TTL int
}

type auth struct {
appID string
client *ClientWithResponses
Expand Down Expand Up @@ -38,18 +45,56 @@ func newAuth(appID string, client *ClientWithResponses) (*auth, error) {
return &auth, nil
}

// CreateMagicLink creates a Magic Link for your app.
func (a *auth) CreateMagicLink(args CreateMagicLinkBody) (*MagicLink, error) {
res, err := a.client.CreateMagicLinkWithResponse(context.Background(), a.appID, args)
if err != nil {
return nil, err
// CreateMagicLink creates a Magic Link for your app using an email address.
func (a *auth) CreateMagicLinkWithEmail(
email string,
magicLinkType MagicLinkType,
send bool,
opts *MagicLinkOptions,
) (*MagicLink, error) {
args := CreateMagicLinkBody{
Email: email,
Channel: EmailChannel,
Type: magicLinkType,
Send: send,
}

if res.JSON201 != nil {
return &res.JSON201.MagicLink, nil
return a.createMagicLink(args, opts)
}

// CreateMagicLink creates a Magic Link for your app using an E164-formatted phone number.
func (a *auth) CreateMagicLinkWithPhone(
phone string,
magicLinkType MagicLinkType,
send bool,
opts *MagicLinkOptions,
) (*MagicLink, error) {
args := CreateMagicLinkBody{
Phone: phone,
Channel: PhoneChannel,
Type: magicLinkType,
Send: send,
}

return nil, errorFromResponse(res.Body, res.StatusCode())
return a.createMagicLink(args, opts)
}

// CreateMagicLink creates a Magic Link for your app using a Passage user ID.
func (a *auth) CreateMagicLinkWithUser(
userID string,
channel ChannelType,
magicLinkType MagicLinkType,
send bool,
opts *MagicLinkOptions,
) (*MagicLink, error) {
args := CreateMagicLinkBody{
UserID: userID,
Channel: channel,
Type: magicLinkType,
Send: send,
}

return a.createMagicLink(args, opts)
}

// ValidateJWT validates the JWT and returns the user ID.
Expand Down Expand Up @@ -80,6 +125,26 @@ func (a *auth) ValidateJWT(jwt string) (string, error) {
return userID, nil
}

func (a *auth) createMagicLink(args CreateMagicLinkBody, opts *MagicLinkOptions) (*MagicLink, error) {
if opts != nil {
args.Language = opts.Language
args.MagicLinkPath = opts.MagicLinkPath
args.RedirectURL = opts.RedirectURL
args.TTL = opts.TTL
}

res, err := a.client.CreateMagicLinkWithResponse(context.Background(), a.appID, args)
if err != nil {
return nil, err
}

if res.JSON201 != nil {
return &res.JSON201.MagicLink, nil
}

return nil, errorFromResponse(res.Body, res.StatusCode())
}

func (a *auth) getPublicKey(token *jwt.Token) (interface{}, error) {
keyID, ok := token.Header["kid"].(string)
if !ok {
Expand Down

0 comments on commit 741b260

Please sign in to comment.