-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
56 lines (45 loc) · 1.23 KB
/
login.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
package cmd
import (
"bufio"
"fmt"
"os"
"runtime"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/wantguns/unfold/api"
)
var LoginCmd = &cobra.Command{
Use: "login",
Short: "Log in to your fold account",
Long: `This command must be run before running any other command to authenticate the CLI`,
Run: loginCmdHandler,
}
func loginCmdHandler(cmd *cobra.Command, args []string) {
fmt.Print("Enter the phone number associated with your fold account: ")
phone := bufio.NewScanner(os.Stdin)
phone.Scan()
err := api.Login("+91" + phone.Text())
if err != nil {
log.Error().Err(err).Msg("Login response: ")
runtime.Goexit()
}
fmt.Print("Login request successful, enter OTP: ")
otp := bufio.NewScanner(os.Stdin)
otp.Scan()
access, refresh, err := api.VerifyOtp("+91"+phone.Text(), otp.Text())
if err != nil {
log.Error().Err(err).Msg("Verify otp response: ")
runtime.Goexit()
}
viper.Set("token.access", access)
viper.Set("token.refresh", refresh)
log.Debug().Msg("Fetching user info")
user_uuid, err := api.User()
if err != nil {
log.Error().Err(err).Msg("Refresh response: ")
runtime.Goexit()
}
viper.Set("fold_user.uuid", user_uuid)
fmt.Println("Login successful !")
}