login_view.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package ui
  2. import (
  3. "context"
  4. "log"
  5. "github.com/ayntgl/discordo/config"
  6. "github.com/diamondburned/arikawa/v3/api"
  7. "github.com/rivo/tview"
  8. "github.com/zalando/go-keyring"
  9. )
  10. type LoginView struct {
  11. *tview.Form
  12. core *Core
  13. }
  14. func NewLoginView(c *Core) *LoginView {
  15. v := &LoginView{
  16. Form: tview.NewForm(),
  17. core: c,
  18. }
  19. v.AddInputField("Email", "", 0, nil, nil)
  20. v.AddPasswordField("Password", "", 0, 0, nil)
  21. v.AddPasswordField("Code (optional)", "", 0, 0, nil)
  22. v.AddButton("Login", v.onLoginButtonSelected)
  23. v.SetTitle("Login")
  24. v.SetTitleAlign(tview.AlignLeft)
  25. v.SetBorder(true)
  26. v.SetBorderPadding(1, 1, 1, 1)
  27. return v
  28. }
  29. func (v *LoginView) onLoginButtonSelected() {
  30. email := v.GetFormItem(0).(*tview.InputField).GetText()
  31. password := v.GetFormItem(1).(*tview.InputField).GetText()
  32. if email == "" || password == "" {
  33. return
  34. }
  35. // Make a scratch HTTP client without a token
  36. client := api.NewClient("").WithContext(context.Background())
  37. // Try to login without TOTP
  38. l, err := client.Login(email, password)
  39. if err != nil {
  40. log.Fatal(err)
  41. }
  42. // If the token is not dispatched in the response and the "mfa" field is set as true, login using MFA instead.
  43. if l.Token == "" && l.MFA {
  44. code := v.GetFormItem(2).(*tview.InputField).GetText()
  45. if code == "" {
  46. return
  47. }
  48. // Retry logging in with a 2FA token
  49. l, err = client.TOTP(code, l.Ticket)
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. }
  54. err = v.core.Run(l.Token)
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. v.core.Draw()
  59. go keyring.Set(config.Name, "token", l.Token)
  60. }