-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_login.go
47 lines (39 loc) · 1.41 KB
/
user_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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package cmd
import (
"fmt"
"github.com/TheThingsNetwork/go-account-lib/account"
"github.com/TheThingsNetwork/go-account-lib/auth"
"github.com/TheThingsNetwork/ttn/ttnctl/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var userLoginCmd = &cobra.Command{
Use: "login [access code]",
Short: "Log in with your TTN account",
Long: `ttnctl user login allows you to log in to your TTN account.`,
Example: `First get an access code from your TTN profile by going to
https://account.thethingsnetwork.org and clicking "ttnctl access code".
$ ttnctl user login [paste the access code you requested above]
INFO Successfully logged in as yourname (your@email.org)
`,
Run: func(cmd *cobra.Command, args []string) {
assertArgsLength(cmd, args, 1, 1)
code := args[0]
token, err := util.Login(ctx, code)
if err != nil {
ctx.WithError(err).Fatal("Login failed")
}
acc := account.New(viper.GetString("auth-server")).WithHeader("User-Agent", util.GetUserAgent())
acc.WithAuth(auth.AccessToken(token.AccessToken))
profile, err := acc.Profile()
if err != nil {
ctx.WithError(err).Fatal("Could not get user profile")
}
ctx.Info(fmt.Sprintf("Successfully logged in as %s (%s)", profile.Username, profile.Email))
},
}
func init() {
userCmd.AddCommand(userLoginCmd)
}