/
signup.go
80 lines (62 loc) · 1.65 KB
/
signup.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
package main
import (
"context"
"fmt"
"github.com/urfave/cli"
"github.com/manifoldco/manifold-cli/analytics"
"github.com/manifoldco/manifold-cli/config"
"github.com/manifoldco/manifold-cli/errs"
"github.com/manifoldco/manifold-cli/prompts"
"github.com/manifoldco/manifold-cli/session"
)
func init() {
signupCmd := cli.Command{
Name: "signup",
Usage: "Create a new account",
Category: "ADMINISTRATIVE",
Action: signup,
}
cmds = append(cmds, signupCmd)
}
func signup(_ *cli.Context) error {
ctx := context.Background()
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.ErrAlreadyLoggedIn
}
name, err := prompts.FullName("")
if err != nil {
return err
}
email, err := prompts.Email("")
if err != nil {
return err
}
password, err := prompts.Password()
if err != nil {
return err
}
_, err = session.Signup(ctx, cfg, name, email, password)
if err != nil {
return cli.NewExitError("A problem occurred: "+err.Error(), -1)
}
s, err = session.Create(ctx, cfg, email, password)
if err != nil {
return cli.NewExitError("Failed to login after signup. "+err.Error(), -1)
}
a, err := analytics.New(cfg, s)
if err != nil {
return cli.NewExitError("A problem occurred: "+err.Error(), -1)
}
a.Track(ctx, "Logged In", nil)
fmt.Println("Account created. You are logged in.")
fmt.Println("A verification code has been sent to your e-mail address.")
return verifyEmailCode(ctx, cfg, s, "")
}