Skip to content

Commit e4e70f6

Browse files
authored
Add methods and events for email verification and password reset (#340)
* Add methods and events for email verification and password reset * Apply changes and fix copy pasta
1 parent 5ad2b6d commit e4e70f6

File tree

5 files changed

+500
-17
lines changed

5 files changed

+500
-17
lines changed

pkg/events/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ const (
4646
OrganizationMembershipUpdated = "organization_membership.updated"
4747
OrganizationMembershipRemoved = "organization_membership.removed" // Deprecated: use OrganizationMembershipDeleted instead
4848
SessionCreated = "session.created"
49+
EmailVerificationCreated = "email_verification.created"
4950
InvitationCreated = "invitation.created"
5051
MagicAuthCreated = "magic_auth.created"
52+
PasswordResetCreated = "password_reset.created"
5153
)
5254

5355
// Client represents a client that performs Event requests to the WorkOS API.

pkg/usermanagement/client.go

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ const (
3838
Desc Order = "desc"
3939
)
4040

41+
type EmailVerification struct {
42+
ID string `json:"id"`
43+
UserId string `json:"user_id"`
44+
Email string `json:"email"`
45+
ExpiresAt string `json:"expires_at"`
46+
Code string `json:"code"`
47+
CreatedAt string `json:"created_at"`
48+
UpdatedAt string `json:"updated_at"`
49+
}
50+
4151
// InvitationState represents the state of an Invitation.
4252
type InvitationState string
4353

@@ -58,6 +68,7 @@ type Invitation struct {
5868
Token string `json:"token"`
5969
AcceptInvitationUrl string `json:"accept_invitation_url`
6070
OrganizationID string `json:"organization_id,omitempty"`
71+
InviterUserID string `json:"inviter_user_id,omitempty"`
6172
ExpiresAt string `json:"expires_at"`
6273
CreatedAt string `json:"created_at"`
6374
UpdatedAt string `json:"updated_at"`
@@ -73,6 +84,16 @@ type MagicAuth struct {
7384
UpdatedAt string `json:"updated_at"`
7485
}
7586

87+
type PasswordReset struct {
88+
ID string `json:"id"`
89+
UserId string `json:"user_id"`
90+
Email string `json:"email"`
91+
PasswordResetToken string `json:"password_reset_token"`
92+
PasswordResetUrl string `json:"password_reset_url"`
93+
ExpiresAt string `json:"expires_at"`
94+
CreatedAt string `json:"created_at"`
95+
}
96+
7697
// Organization contains data about a particular Organization.
7798
type Organization struct {
7899
// The Organization's unique identifier.
@@ -314,6 +335,11 @@ type RefreshAuthenticationResponse struct {
314335
RefreshToken string `json:"refresh_token"`
315336
}
316337

338+
type GetEmailVerificationOpts struct {
339+
// The Email Verification's unique identifier.
340+
EmailVerification string
341+
}
342+
317343
type SendVerificationEmailOpts struct {
318344
// The unique ID of the User who will be sent a verification email.
319345
User string
@@ -326,6 +352,16 @@ type VerifyEmailOpts struct {
326352
Code string `json:"code"`
327353
}
328354

355+
type GetPasswordResetOpts struct {
356+
// The Password Reset's unique identifier.
357+
PasswordReset string
358+
}
359+
360+
type CreatePasswordResetOpts struct {
361+
// The email address the password reset is for.
362+
Email string `json:"email"`
363+
}
364+
329365
type SendPasswordResetEmailOpts struct {
330366
// The unique ID of the User whose email address will be verified.
331367
Email string `json:"email"`
@@ -1169,6 +1205,36 @@ func (c *Client) AuthenticateWithOrganizationSelection(ctx context.Context, opts
11691205
return body, err
11701206
}
11711207

1208+
// GetEmailVerification fetches an EmailVerification object by its ID.
1209+
func (c *Client) GetEmailVerification(ctx context.Context, opts GetEmailVerificationOpts) (EmailVerification, error) {
1210+
endpoint := fmt.Sprintf("%s/user_management/email_verification/%s", c.Endpoint, opts.EmailVerification)
1211+
1212+
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
1213+
if err != nil {
1214+
return EmailVerification{}, err
1215+
}
1216+
req = req.WithContext(ctx)
1217+
req.Header.Set("User-Agent", "workos-go/"+workos.Version)
1218+
req.Header.Set("Authorization", "Bearer "+c.APIKey)
1219+
req.Header.Set("Content-Type", "application/json")
1220+
1221+
res, err := c.HTTPClient.Do(req)
1222+
if err != nil {
1223+
return EmailVerification{}, err
1224+
}
1225+
defer res.Body.Close()
1226+
1227+
if err = workos_errors.TryGetHTTPError(res); err != nil {
1228+
return EmailVerification{}, err
1229+
}
1230+
1231+
var body EmailVerification
1232+
dec := json.NewDecoder(res.Body)
1233+
err = dec.Decode(&body)
1234+
1235+
return body, err
1236+
}
1237+
11721238
// SendVerificationEmail creates an email verification challenge and emails verification token to user.
11731239
func (c *Client) SendVerificationEmail(ctx context.Context, opts SendVerificationEmailOpts) (UserResponse, error) {
11741240
endpoint := fmt.Sprintf(
@@ -1249,8 +1315,76 @@ func (c *Client) VerifyEmail(ctx context.Context, opts VerifyEmailOpts) (UserRes
12491315
return body, err
12501316
}
12511317

1252-
// SendPasswordResetEmail creates a password reset challenge and emails a password reset link to an
1253-
// unmanaged user.
1318+
// GetPasswordReset fetches a PasswordReset object by its ID.
1319+
func (c *Client) GetPasswordReset(ctx context.Context, opts GetPasswordResetOpts) (PasswordReset, error) {
1320+
endpoint := fmt.Sprintf("%s/user_management/password_reset/%s", c.Endpoint, opts.PasswordReset)
1321+
1322+
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
1323+
if err != nil {
1324+
return PasswordReset{}, err
1325+
}
1326+
req = req.WithContext(ctx)
1327+
req.Header.Set("User-Agent", "workos-go/"+workos.Version)
1328+
req.Header.Set("Authorization", "Bearer "+c.APIKey)
1329+
req.Header.Set("Content-Type", "application/json")
1330+
1331+
res, err := c.HTTPClient.Do(req)
1332+
if err != nil {
1333+
return PasswordReset{}, err
1334+
}
1335+
defer res.Body.Close()
1336+
1337+
if err = workos_errors.TryGetHTTPError(res); err != nil {
1338+
return PasswordReset{}, err
1339+
}
1340+
1341+
var body PasswordReset
1342+
dec := json.NewDecoder(res.Body)
1343+
err = dec.Decode(&body)
1344+
1345+
return body, err
1346+
}
1347+
1348+
// CreatePasswordReset creates a PasswordReset token that can be emailed to the user.
1349+
func (c *Client) CreatePasswordReset(ctx context.Context, opts CreatePasswordResetOpts) (PasswordReset, error) {
1350+
endpoint := fmt.Sprintf("%s/user_management/password_reset", c.Endpoint)
1351+
1352+
data, err := json.Marshal(opts)
1353+
if err != nil {
1354+
return PasswordReset{}, err
1355+
}
1356+
1357+
req, err := http.NewRequest(
1358+
http.MethodPost,
1359+
endpoint,
1360+
bytes.NewBuffer(data),
1361+
)
1362+
if err != nil {
1363+
return PasswordReset{}, err
1364+
}
1365+
req = req.WithContext(ctx)
1366+
req.Header.Set("User-Agent", "workos-go/"+workos.Version)
1367+
req.Header.Set("Authorization", "Bearer "+c.APIKey)
1368+
req.Header.Set("Content-Type", "application/json")
1369+
1370+
res, err := c.HTTPClient.Do(req)
1371+
if err != nil {
1372+
return PasswordReset{}, err
1373+
}
1374+
defer res.Body.Close()
1375+
1376+
if err = workos_errors.TryGetHTTPError(res); err != nil {
1377+
return PasswordReset{}, err
1378+
}
1379+
1380+
var body PasswordReset
1381+
dec := json.NewDecoder(res.Body)
1382+
err = dec.Decode(&body)
1383+
1384+
return body, err
1385+
}
1386+
1387+
// Deprecated: Use CreatePasswordReset instead. This method will be removed in a future major version.
12541388
func (c *Client) SendPasswordResetEmail(ctx context.Context, opts SendPasswordResetEmailOpts) error {
12551389
endpoint := fmt.Sprintf(
12561390
"%s/user_management/password_reset/send",
@@ -1356,7 +1490,7 @@ func (c *Client) GetMagicAuth(ctx context.Context, opts GetMagicAuthOpts) (Magic
13561490
return body, err
13571491
}
13581492

1359-
// CreateMagicAuth creates a one-time Magic Auth code that can be emailed it to the user.
1493+
// CreateMagicAuth creates a one-time Magic Auth code that can be emailed to the user.
13601494
func (c *Client) CreateMagicAuth(ctx context.Context, opts CreateMagicAuthOpts) (MagicAuth, error) {
13611495
endpoint := fmt.Sprintf("%s/user_management/magic_auth", c.Endpoint)
13621496

0 commit comments

Comments
 (0)