-
Notifications
You must be signed in to change notification settings - Fork 117
/
logout.go
75 lines (61 loc) · 1.48 KB
/
logout.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
package auth
import (
"context"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/dotrill"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)
// LogoutCmd is the command for logging out of a Rill account.
func LogoutCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "logout",
Short: "Logout of the Rill API",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
token := ch.AdminToken()
if token == "" {
ch.PrintfWarn("You are already logged out.\n")
return nil
}
err := Logout(ctx, ch)
if err != nil {
return err
}
ch.PrintfSuccess("Successfully logged out.\n")
return nil
},
}
return cmd
}
func Logout(ctx context.Context, ch *cmdutil.Helper) error {
client, err := ch.Client()
if err != nil {
return err
}
_, err = client.RevokeCurrentAuthToken(ctx, &adminv1.RevokeCurrentAuthTokenRequest{})
if err != nil {
ch.Printf("Failed to revoke token (did you revoke it manually?). Clearing local token anyway.\n")
}
err = dotrill.SetAccessToken("")
if err != nil {
return err
}
// Set original_token as empty
err = dotrill.SetBackupToken("")
if err != nil {
return err
}
// Set representing user email as empty
err = dotrill.SetRepresentingUser("")
if err != nil {
return err
}
// Clear the state during logout
err = dotrill.SetDefaultOrg("")
if err != nil {
return err
}
ch.AdminTokenDefault = ""
return nil
}