فهرست منبع

ui: new package

ayn2op 3 سال پیش
والد
کامیت
a1f5bc8e7d
2فایلهای تغییر یافته به همراه36 افزوده شده و 22 حذف شده
  1. 21 20
      internal/ui/login_form.go
  2. 15 2
      main.go

+ 21 - 20
login_form.go → internal/ui/login_form.go

@@ -1,7 +1,6 @@
-package main
+package ui
 
 import (
-	"context"
 	"log"
 
 	"github.com/ayn2op/discordo/internal/config"
@@ -13,11 +12,13 @@ import (
 
 type LoginForm struct {
 	*tview.Form
+	Token chan string
 }
 
-func newLoginForm() *LoginForm {
+func NewLoginForm() *LoginForm {
 	lf := &LoginForm{
-		Form: tview.NewForm(),
+		Form:  tview.NewForm(),
+		Token: make(chan string, 1),
 	}
 
 	lf.AddInputField("Email", "", 0, nil, nil)
@@ -28,6 +29,7 @@ func newLoginForm() *LoginForm {
 
 	lf.SetTitle("Login")
 	lf.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor))
+	lf.SetTitleAlign(tview.AlignLeft)
 
 	p := config.Current.Theme.BorderPadding
 	lf.SetBorder(config.Current.Theme.Border)
@@ -44,36 +46,35 @@ func (lf *LoginForm) onLoginButtonSelected() {
 		return
 	}
 
-	// Make a scratch HTTP client without a token
-	client := api.NewClient("").WithContext(context.Background())
-	// Try to login without TOTP
-	l, err := client.Login(email, password)
+	// Create a new API client without an authentication token.
+	apiClient := api.NewClient("")
+	// Log in using the provided email and password.
+	lr, err := apiClient.Login(email, password)
 	if err != nil {
 		log.Fatal(err)
 	}
 
-	// Retry logging in with a 2FA token
-	if l.Token == "" && l.MFA {
+	// If the account has MFA-enabled, attempt to log in using the provided code.
+	if lr.MFA && lr.Token == "" {
 		code := lf.GetFormItem(2).(*tview.InputField).GetText()
 		if code == "" {
 			return
 		}
 
-		l, err = client.TOTP(code, l.Ticket)
+		lr, err = apiClient.TOTP(code, lr.Ticket)
 		if err != nil {
 			log.Fatal(err)
 		}
 	}
 
-	mainFlex = newMainFlex()
-	if err = openState(l.Token); err != nil {
-		log.Fatal(err)
-	}
-
-	app.SetRoot(mainFlex, true)
+	if lr.Token == "" {
+		log.Fatal("missing token")
+	} else {
+		rememberMe := lf.GetFormItem(3).(*tview.Checkbox).IsChecked()
+		if rememberMe {
+			go keyring.Set(config.Name, "token", lr.Token)
+		}
 
-	rememberMe := lf.GetFormItem(3).(*tview.Checkbox).IsChecked()
-	if rememberMe {
-		go keyring.Set(config.Name, "token", l.Token)
+		lf.Token <- lr.Token
 	}
 }

+ 15 - 2
main.go

@@ -7,6 +7,7 @@ import (
 	"path/filepath"
 
 	"github.com/ayn2op/discordo/internal/config"
+	"github.com/ayn2op/discordo/internal/ui"
 	"github.com/rivo/tview"
 	"github.com/zalando/go-keyring"
 )
@@ -52,9 +53,21 @@ func main() {
 		log.Fatal(err)
 	}
 
-	// mission failed, we'll get 'em next time
 	if token == "" {
-		app.SetRoot(newLoginForm(), true)
+		lf := ui.NewLoginForm()
+
+		go func() {
+			mainFlex = newMainFlex()
+			if err := openState(<-lf.Token); err != nil {
+				log.Fatal(err)
+			}
+
+			app.QueueUpdateDraw(func() {
+				app.SetRoot(mainFlex, true)
+			})
+		}()
+
+		app.SetRoot(lf, true)
 	} else {
 		mainFlex = newMainFlex()
 		if err := openState(token); err != nil {