/
verify.go
98 lines (78 loc) · 2.23 KB
/
verify.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"fmt"
"github.com/urfave/cli"
"github.com/manifoldco/manifold-cli/analytics"
"github.com/manifoldco/manifold-cli/api"
"github.com/manifoldco/manifold-cli/config"
"github.com/manifoldco/manifold-cli/errs"
"github.com/manifoldco/manifold-cli/prompts"
"github.com/manifoldco/manifold-cli/session"
"github.com/manifoldco/manifold-cli/generated/identity/client/user"
"github.com/manifoldco/manifold-cli/generated/identity/models"
)
func init() {
verifyCmd := cli.Command{
Name: "verify",
ArgsUsage: "[code]",
Usage: "Verify an e-mail address with an e-mail verification code",
Category: "ADMINISTRATIVE",
Action: verify,
}
cmds = append(cmds, verifyCmd)
}
func verify(cliCtx *cli.Context) error {
ctx := context.Background()
if err := maxOptionalArgsLength(cliCtx, 1); err != nil {
return err
}
verificationCode, err := optionalArgCode(cliCtx, 0, "e-mail verification")
if err != nil {
return err
}
cfg, err := config.Load()
if err != nil {
return cli.NewExitError("Could not load config: "+err.Error(), -1)
}
s, err := session.Retrieve(ctx, cfg)
if err != nil {
return cli.NewExitError("Could not retrieve session: "+err.Error(), -1)
}
if !s.Authenticated() {
return errs.ErrMustLogin
}
return verifyEmailCode(ctx, cfg, s, verificationCode)
}
func verifyEmailCode(ctx context.Context, cfg *config.Config,
s session.Session, verificationCode string) error {
var err error
if verificationCode == "" {
verificationCode, err = prompts.EmailVerificationCode("")
}
if err != nil {
return err
}
client, err := api.New(api.Identity)
if err != nil {
return err
}
params := user.NewPostUsersVerifyParams()
params.SetBody(&models.VerifyEmail{
Body: &models.VerifyEmailBody{
VerificationCode: &verificationCode,
},
})
_, err = client.Identity.User.PostUsersVerify(params, nil)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("Failed to verify e-mail code: %s", err), -1)
}
analyticsClient, err := analytics.New(cfg, s)
if err != nil {
return cli.NewExitError(fmt.Sprintf("A problem occurred: %s", err), -1)
}
analyticsClient.Track(ctx, "E-mail Verified", nil)
fmt.Printf("Thanks! Your e-mail address has been verified.\n")
return nil
}