-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathauthenticate.go
More file actions
53 lines (43 loc) · 1.32 KB
/
authenticate.go
File metadata and controls
53 lines (43 loc) · 1.32 KB
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
package users
import (
"log"
"os"
"github.com/provideplatform/provide-cli/cmd/common"
provide "github.com/provideplatform/provide-go/api/ident"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// AuthenticateCmd represents the authenticate command
var AuthenticateCmd = &cobra.Command{
Use: "authenticate",
Short: "Authenticate using your credentials",
Long: `Authenticate using user credentials and receive a
valid access/refresh token pair which can be used to make API calls.`,
Run: authenticate,
}
func authenticate(cmd *cobra.Command, args []string) {
email = common.FreeInput("Email", "", common.MandatoryValidation)
passwd = common.FreeInput("Password", "", common.MandatoryValidation)
resp, err := provide.Authenticate(email, passwd)
if err != nil {
log.Println(err)
os.Exit(1)
}
if resp.Token.AccessToken != nil && resp.Token.RefreshToken != nil {
common.CacheAccessRefreshToken(resp.Token)
} else if resp.Token.Token != nil {
cacheAPIToken(*resp.Token.Token)
} else {
log.Println("Failed to get token from authentication response.")
os.Exit(1)
}
log.Print("Authentication successful")
if resp.User != nil {
common.StoreUserDetails(resp.User)
log.Printf("User ID: %s", resp.User.ID)
}
}
func cacheAPIToken(token string) {
viper.Set(common.AccessTokenConfigKey, token)
viper.WriteConfig()
}