Skip to content

Commit f6168b2

Browse files
authored
feat(BRE2-853): simplify login (#335)
1 parent 747f217 commit f6168b2

1 file changed

Lines changed: 32 additions & 22 deletions

File tree

pkg/auth/auth.go

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"errors"
66
"fmt"
77
"os"
8+
"os/exec"
9+
"runtime"
810
"strings"
911

1012
"github.com/brevdev/brev-cli/pkg/config"
1113
"github.com/brevdev/brev-cli/pkg/entity"
1214
breverrors "github.com/brevdev/brev-cli/pkg/errors"
13-
"github.com/brevdev/brev-cli/pkg/terminal"
1415
"github.com/fatih/color"
1516
"github.com/golang-jwt/jwt/v5"
1617
"github.com/pkg/browser"
@@ -221,38 +222,47 @@ func (t Auth) LoginWithToken(token string) error {
221222
return nil
222223
}
223224

225+
// showLoginURL displays the login link and CLI alternative for manual navigation.
226+
func showLoginURL(url string) {
227+
urlType := color.New(color.FgCyan, color.Bold).SprintFunc()
228+
fmt.Println("Login here: " + urlType(url))
229+
}
230+
224231
func defaultAuthFunc(url, code string) {
225232
codeType := color.New(color.FgWhite, color.Bold).SprintFunc()
226233
if code != "" {
227234
fmt.Println("Your Device Confirmation Code is 👉", codeType(code), "👈")
228235
fmt.Print("\n")
229236
}
230-
urlType := color.New(color.FgCyan, color.Bold).SprintFunc()
231-
fmt.Println("Browser link: " + urlType(url) + "\n")
232-
fmt.Println("Alternatively, get CLI Command (\"Login via CLI\"): ", urlType(fmt.Sprintf("%s/profile?login=cli", config.GlobalConfig.GetConsoleURL())))
233-
fmt.Print("\n")
234-
caretType := color.New(color.FgGreen, color.Bold).SprintFunc()
235-
enterType := color.New(color.FgGreen, color.Bold).SprintFunc()
236-
_ = terminal.PromptGetInput(terminal.PromptContent{
237-
Label: " " + caretType("▸") + " Press " + enterType("Enter") + " to login via browser",
238-
ErrorMsg: "error",
239-
AllowEmpty: true,
240-
})
241237

242-
fmt.Print("\n")
243-
244-
err := browser.OpenURL(url)
245-
if err != nil {
246-
fmt.Println("Error opening browser. Please copy", urlType(url), "and paste it in your browser.")
238+
if hasBrowser() {
239+
if err := browser.OpenURL(url); err == nil {
240+
fmt.Println("Waiting for login to complete in browser...")
241+
return
242+
}
247243
}
248-
fmt.Println("Waiting for login to complete in browser... ")
244+
showLoginURL(url)
245+
fmt.Println("\nWaiting for login to complete...")
249246
}
250247

251248
func skipBrowserAuthFunc(url, _ string) {
252-
urlType := color.New(color.FgCyan, color.Bold).SprintFunc()
253-
fmt.Println("Please copy", urlType(url), "and paste it in your browser.")
254-
fmt.Println("Alternatively, get CLI Command (\"Login via CLI\"): ", urlType(fmt.Sprintf("%s/profile?login=cli", config.GlobalConfig.GetConsoleURL())))
255-
fmt.Println("Waiting for login to complete in browser... Ctrl+C to use CLI command instead.")
249+
showLoginURL(url)
250+
fmt.Println("\nWaiting for login to complete...")
251+
}
252+
253+
// hasBrowser reports whether a browser can be opened on the current platform.
254+
func hasBrowser() bool {
255+
if runtime.GOOS == "darwin" {
256+
// macOS always has "open".
257+
return true
258+
}
259+
// Linux: check for a known browser launcher.
260+
for _, name := range []string{"xdg-open", "x-www-browser", "www-browser"} {
261+
if _, err := exec.LookPath(name); err == nil {
262+
return true
263+
}
264+
}
265+
return false
256266
}
257267

258268
func (t Auth) Login(skipBrowser bool) (*LoginTokens, error) {

0 commit comments

Comments
 (0)