discordo/ui/login_form.go

75 lines
1.6 KiB
Go

package ui
import (
"context"
"log"
"github.com/ayn2op/discordo/config"
"github.com/diamondburned/arikawa/v3/api"
"github.com/rivo/tview"
"github.com/zalando/go-keyring"
)
type LoginForm struct {
*tview.Form
app *Application
}
func newLoginForm(app *Application) *LoginForm {
lf := &LoginForm{
Form: tview.NewForm(),
app: app,
}
lf.AddInputField("Email", "", 0, nil, nil)
lf.AddPasswordField("Password", "", 0, 0, nil)
lf.AddPasswordField("Code (optional)", "", 0, 0, nil)
lf.AddButton("Login", lf.onLoginButtonSelected)
lf.SetTitle("Login")
lf.SetTitleAlign(tview.AlignLeft)
lf.SetBorder(true)
lf.SetBorderPadding(1, 1, 1, 1)
return lf
}
func (v *LoginForm) onLoginButtonSelected() {
email := v.GetFormItem(0).(*tview.InputField).GetText()
password := v.GetFormItem(1).(*tview.InputField).GetText()
if email == "" || password == "" {
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)
if err != nil {
log.Fatal(err)
}
// If the token is not dispatched in the response and the "mfa" field is set as true, login using MFA instead.
if l.Token == "" && l.MFA {
code := v.GetFormItem(2).(*tview.InputField).GetText()
if code == "" {
return
}
// Retry logging in with a 2FA token
l, err = client.TOTP(code, l.Ticket)
if err != nil {
log.Fatal(err)
}
}
err = v.app.Connect(l.Token)
if err != nil {
log.Fatal(err)
}
v.app.SetRoot(v.app.view, true)
v.app.SetFocus(v.app.view.GuildsTree)
go keyring.Set(config.Name, "token", l.Token)
}