/
verify.go
83 lines (69 loc) · 1.65 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
package cmd
import (
"context"
"fmt"
"strings"
"github.com/urfave/cli"
"github.com/manifoldco/torus-cli/api"
"github.com/manifoldco/torus-cli/config"
"github.com/manifoldco/torus-cli/errs"
"github.com/manifoldco/torus-cli/prompts"
)
func init() {
version := cli.Command{
Name: "verify",
ArgsUsage: "<code>",
Usage: "Verify an email address for your account",
Category: "ACCOUNT",
Action: chain(
ensureDaemon, ensureSession, verifyEmailCmd,
),
}
Cmds = append(Cmds, version)
}
func verifyEmailCmd(ctx *cli.Context) error {
return verifyEmail(ctx, nil, false)
}
func askToVerify(ctx *cli.Context) error {
code, err := prompts.VerificationCode("", false)
if err != nil {
return err
}
err = verifyEmail(ctx, &code, true)
if err != nil {
return err
}
return nil
}
func verifyEmail(ctx *cli.Context, code *string, subCommand bool) error {
cfg, err := config.LoadConfig()
if err != nil {
return err
}
var verifyCode string
if code != nil {
verifyCode = *code
}
client := api.NewClient(cfg)
c := context.Background()
// Code is nil, check args for value
if !subCommand {
args := ctx.Args()
if len(args) > 1 {
return errs.NewUsageExitError("Too many arguments", ctx)
}
if len(args) != 1 {
return errs.NewUsageExitError("Missing verification code", ctx)
}
verifyCode = args[0]
}
err = client.Users.Verify(c, verifyCode)
if err != nil {
if strings.Contains(err.Error(), "wrong user state: active") {
return errs.NewExitError("Email already verified :)")
}
return errs.NewExitError("Email verification failed, please try again.")
}
fmt.Println("Your email is now verified.")
return nil
}