Skip to content

Commit 7d6a0ea

Browse files
authored
status shows user handle
1 parent 2b3a47a commit 7d6a0ea

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

client/auth.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ type LoginResponse struct {
2020
RefreshToken string `json:"refresh_token"`
2121
}
2222

23+
type CurrentUserResponse struct {
24+
Handle string `json:"handle"`
25+
}
26+
2327
func FetchAccessToken() (*LoginResponse, error) {
2428
api_url := viper.GetString("api_url")
2529
client := &http.Client{}
@@ -82,6 +86,21 @@ func LoginWithCode(code string) (*LoginResponse, error) {
8286
return &creds, nil
8387
}
8488

89+
func FetchCurrentUser() (*CurrentUserResponse, error) {
90+
body, err := fetchWithAuth("GET", "/v1/users/me")
91+
if err != nil {
92+
return nil, err
93+
}
94+
95+
var user CurrentUserResponse
96+
err = json.Unmarshal(body, &user)
97+
if err != nil {
98+
return nil, err
99+
}
100+
101+
return &user, nil
102+
}
103+
85104
func fetchWithAuth(method string, url string) ([]byte, error) {
86105
body, code, err := fetchWithAuthAndPayload(method, url, []byte{})
87106
if err != nil {

cmd/status.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ func checkAuthStatus() {
3636
return
3737
}
3838

39+
user, err := api.FetchCurrentUser()
40+
if err == nil && user != nil && user.Handle != "" {
41+
fmt.Printf("Logged in as @%s\n", user.Handle)
42+
return
43+
}
44+
3945
fmt.Println("Logged in")
40-
// TODO: Consider adding user data endpoint to show email/username
4146
}
4247

4348
func checkVersionStatus(cmd *cobra.Command) {

0 commit comments

Comments
 (0)