Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13696a7

Browse files
committed
Inline saveSessionToken into both code paths
1 parent 5ecba6d commit 13696a7

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

cli/login.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,18 @@ func login() *cobra.Command {
126126
return xerrors.Errorf("login with password: %w", err)
127127
}
128128

129-
err = saveSessionToken(cmd, client, resp.SessionToken, serverURL)
129+
sessionToken := resp.SessionToken
130+
config := createConfig(cmd)
131+
err = config.Session().Write(sessionToken)
130132
if err != nil {
131-
return xerrors.Errorf("save session token: %w", err)
133+
return xerrors.Errorf("write session token: %w", err)
134+
}
135+
err = config.URL().Write(serverURL.String())
136+
if err != nil {
137+
return xerrors.Errorf("write server url: %w", err)
132138
}
133139

140+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Welcome to Coder, %s! You're authenticated.\n", color.HiBlackString(">"), color.HiCyanString(username))
134141
return nil
135142
}
136143

@@ -142,7 +149,7 @@ func login() *cobra.Command {
142149
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Your browser has been opened to visit:\n\n\t%s\n\n", authURL.String())
143150
}
144151

145-
apiKey, err := prompt(cmd, &promptui.Prompt{
152+
sessionToken, err := prompt(cmd, &promptui.Prompt{
146153
Label: "Paste your token here:",
147154
Mask: '*',
148155
Validate: func(token string) error {
@@ -158,35 +165,31 @@ func login() *cobra.Command {
158165
return xerrors.Errorf("paste token prompt: %w", err)
159166
}
160167

161-
err = saveSessionToken(cmd, client, apiKey, serverURL)
168+
// Login to get user data - verify it is OK before persisting
169+
client.SessionToken = sessionToken
170+
resp, err := client.User(cmd.Context(), "me")
171+
if err != nil {
172+
return xerrors.Errorf("get user: %w", err)
173+
}
174+
175+
config := createConfig(cmd)
176+
err = config.Session().Write(sessionToken)
177+
if err != nil {
178+
return xerrors.Errorf("write session token: %w", err)
179+
}
180+
err = config.URL().Write(serverURL.String())
162181
if err != nil {
163-
return xerrors.Errorf("save session token after login: %w", err)
182+
return xerrors.Errorf("write server url: %w", err)
164183
}
165184

185+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Welcome to Coder, %s! You're authenticated.\n", color.HiBlackString(">"), color.HiCyanString(resp.Username))
166186
return nil
167187
},
168188
}
169189
}
170190

171191
func saveSessionToken(cmd *cobra.Command, client *codersdk.Client, sessionToken string, serverURL *url.URL) error {
172-
// Login to get user data - verify it is OK before persisting
173-
client.SessionToken = sessionToken
174-
resp, err := client.User(cmd.Context(), "me")
175-
if err != nil {
176-
return xerrors.Errorf("get user: %w", err)
177-
}
178-
179-
config := createConfig(cmd)
180-
err = config.Session().Write(sessionToken)
181-
if err != nil {
182-
return xerrors.Errorf("write session token: %w", err)
183-
}
184-
err = config.URL().Write(serverURL.String())
185-
if err != nil {
186-
return xerrors.Errorf("write server url: %w", err)
187-
}
188192

189-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Welcome to Coder, %s! You're authenticated.\n", color.HiBlackString(">"), color.HiCyanString(resp.Username))
190193
return nil
191194
}
192195

0 commit comments

Comments
 (0)