-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic-link.go
72 lines (59 loc) · 2.18 KB
/
magic-link.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package otplessgo
import "fmt"
// Struct represents the payload for sending magic link to a phone number.
type PhoneMagicLinkPayload struct {
RedirectURI string `json:"redirectURI"`
Expiry int `json:"expiry"`
PhoneNumber string `json:"phoneNumber"`
Channels []Channel `json:"channels"`
Metadata Metadata `json:"metadata,omitempty"`
}
// Struct represents the payload for sending magic link to an email.
type EmailMagicLinkPayload struct {
RedirectURI string `json:"redirectURI"`
Expiry int `json:"expiry"`
Email string `json:"email"`
Metadata Metadata `json:"metadata,omitempty"`
}
type emailMagicLinkPayloadForApi struct {
RedirectURI string `json:"redirectURI"`
Expiry int `json:"expiry"`
Email string `json:"email"`
Metadata Metadata `json:"metadata,omitempty"`
Channel []Channel `json:"channels"`
}
// Sends the Magic link to a phone number
func (o *OTPLessClient) SendPhoneMagicLink(payload *PhoneMagicLinkPayload) (*ApiResponse[ApiSuccessWithRequestID], error) {
return getRequestId(o.restyClient, "/initiate/magiclink", payload)
}
// Sends the Magic link to an email number
func (o *OTPLessClient) SendEmailMagicLink(payload *EmailMagicLinkPayload) (*ApiResponse[ApiSuccessWithRequestID], error) {
apiPayload := emailMagicLinkPayloadForApi{
RedirectURI: payload.RedirectURI,
Expiry: payload.Expiry,
Email: payload.Email,
Metadata: payload.Metadata,
Channel: []Channel{ChannelEmail},
}
return getRequestId(o.restyClient, "/initiate/magiclink", apiPayload)
}
// Verify the code and returns the user details object.
func (o *OTPLessClient) VerifyCode(code string) (*ApiResponse[UserDetails], error) {
apiPayload := CodeVerifyPayload{
Code: code,
}
var failureResp ApiErrorResponse
var successResp UserDetails
resp, err := o.restyClient.R().SetBody(apiPayload).SetError(&failureResp).SetResult(&successResp).Post("/verify/code")
if err != nil {
return nil, err
}
if resp.StatusCode() > 300 {
return nil, fmt.Errorf(failureResp.Message)
}
return &ApiResponse[UserDetails]{
Response: &successResp,
ErrorResponse: &failureResp,
RawResponse: resp.RawResponse,
}, nil
}