Skip to content

Commit

Permalink
improve login
Browse files Browse the repository at this point in the history
  • Loading branch information
olebeck committed Jul 10, 2024
1 parent e8fe425 commit d719c1d
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 137 deletions.
20 changes: 12 additions & 8 deletions cmd/bedrocktool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ func main() {
log.Infof(locale.Loc("bedrocktool_version", locale.Strmap{"Version": updater.Version}))
}

err := utils.Auth.Startup()
if err != nil {
logrus.Fatal(err)
}

ctx, cancel := context.WithCancelCause(context.Background())
utils.Auth.InitCtx(ctx)

recovery.ErrorHandler = func(err error) {
if isDebug {
Expand All @@ -107,7 +111,7 @@ func main() {
os.Exit(1)
}

flag.StringVar(&utils.RealmsEnv, "realms-env", "", "realms env")
//flag.StringVar(&utils.RealmsEnv, "realms-env", "", "realms env")
flag.BoolVar(&utils.Options.Debug, "debug", false, locale.Loc("debug_mode", nil))
flag.BoolVar(&utils.Options.ExtraDebug, "extra-debug", false, locale.Loc("extra_debug", nil))
flag.StringVar(&utils.Options.PathCustomUserData, "userdata", "", locale.Loc("custom_user_data", nil))
Expand All @@ -133,7 +137,7 @@ func main() {
log.Error("Failed to init UI!")
return
}
err := ui.Start(ctx, cancel)
err = ui.Start(ctx, cancel)
cancel(err)
if err != nil {
log.Error(err)
Expand All @@ -149,7 +153,7 @@ func (*TransCMD) Synopsis() string { return "" }
func (c *TransCMD) SetFlags(f *flag.FlagSet) {
f.BoolVar(&c.auth, "auth", false, locale.Loc("should_login_xbox", nil))
}
func (c *TransCMD) Execute(_ context.Context) error {
func (c *TransCMD) Execute(ctx context.Context) error {
const (
BlackFg = "\033[30m"
Bold = "\033[1m"
Expand All @@ -159,10 +163,10 @@ func (c *TransCMD) Execute(_ context.Context) error {
Reset = "\033[0m"
)
if c.auth {
_, err := utils.Auth.GetTokenSource()
if err != nil {
logrus.Error(err)
return nil
if utils.Auth.LoggedIn() {
logrus.Info("Already Logged in")
} else {
utils.Auth.Login(ctx, nil)
}
}
fmt.Println(BlackFg + Bold + Blue + " Trans " + Pink + " Rights " + White + " Are " + Pink + " Human " + Blue + " Rights " + Reset)
Expand Down
2 changes: 1 addition & 1 deletion cmd/generate-color-lookup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
log.Fatal(err)
}

var packs []utils.Pack
var packs []resource.Pack
for _, fi := range packNames {
p := folder + "/" + fi.Name()
pack, err := utils.PackFromBase(resource.MustReadPath(p))
Expand Down
2 changes: 1 addition & 1 deletion gophertunnel
12 changes: 11 additions & 1 deletion subcommands/realms-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ func (*RealmListCMD) Name() string { return "list-realms" }
func (*RealmListCMD) Synopsis() string { return locale.Loc("list_realms_synopsis", nil) }
func (c *RealmListCMD) SetFlags(f *flag.FlagSet) {}
func (c *RealmListCMD) Execute(ctx context.Context) error {
realms, err := utils.GetRealmsAPI().Realms(ctx)
if !utils.Auth.LoggedIn() {
err := utils.Auth.Login(ctx, nil)
if err != nil {
return err
}
}
realmsClient, err := utils.Auth.Realms()
if err != nil {
return err
}
realms, err := realmsClient.Realms(ctx)
if err != nil {
return err
}
Expand Down
9 changes: 9 additions & 0 deletions ui/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"

"github.com/bedrock-tool/bedrocktool/ui/messages"
"github.com/bedrock-tool/bedrocktool/utils"
"github.com/bedrock-tool/bedrocktool/utils/updater"
"github.com/google/subcommands"
)
Expand All @@ -28,5 +29,13 @@ func (c *CLI) Start(ctx context.Context, cancel context.CancelCauseFunc) error {
}

func (c *CLI) HandleMessage(msg *messages.Message) *messages.Message {
switch data := msg.Data.(type) {
case messages.RequestLogin:
if data.Wait {
utils.Auth.Login(context.Background(), nil)
} else {
go utils.Auth.Login(context.Background(), nil)
}
}
return nil
}
30 changes: 0 additions & 30 deletions ui/gui/pages/msauth.go

This file was deleted.

62 changes: 58 additions & 4 deletions ui/gui/pages/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pages
import (
"context"
"errors"
"image/color"
"log"
"reflect"
"sync"
Expand Down Expand Up @@ -32,7 +33,6 @@ type Router struct {
cmdCtx context.Context
cmdCtxCancel context.CancelFunc
Wg sync.WaitGroup
msAuth *msAuth
Invalidate func()
LogWidget func(layout.Context, *material.Theme) layout.Dimensions

Expand All @@ -45,6 +45,8 @@ type Router struct {
ModalLayer *component.ModalLayer
NonModalDrawer bool

loggingIn bool
loginButton widget.Clickable
updateButton widget.Clickable
updateAvailable bool

Expand All @@ -61,7 +63,6 @@ func NewRouter(uii ui.UI) *Router {
r := &Router{
ui: uii,
pages: make(map[string]func() Page),
msAuth: &msAuth{},
ModalLayer: modal,
ModalNavDrawer: component.ModalNavFrom(&nav, modal),
AppBar: component.NewAppBar(modal),
Expand All @@ -70,8 +71,6 @@ func NewRouter(uii ui.UI) *Router {
Duration: time.Millisecond * 250,
},
}
r.msAuth.router = r
utils.Auth.MSHandler = r.msAuth

return r
}
Expand Down Expand Up @@ -183,8 +182,35 @@ func (r *Router) Layout(gtx layout.Context, th *material.Theme) layout.Dimension
return layout.Dimensions{Size: gtx.Constraints.Max}
}

func (r *Router) layoutLoginButton(gtx layout.Context, fg, bg color.NRGBA) layout.Dimensions {
if r.loginButton.Clicked(gtx) {
if !utils.Auth.LoggedIn() {
if !r.loggingIn {
messages.Router.Handle(&messages.Message{
Source: "ui",
Target: "ui",
Data: messages.RequestLogin{},
})
}
} else {
utils.Auth.Logout()
}
}

var text = "Login"
if utils.Auth.LoggedIn() {
text = "Logout"
}
button := material.Button(r.th, &r.loginButton, text)
button.Background.R -= 20
button.Background.G -= 20
button.Background.B -= 32
return button.Layout(gtx)
}

func (r *Router) setActions() {
var extra []component.AppBarAction
extra = append(extra, component.AppBarAction{Layout: r.layoutLoginButton})
extra = append(extra, AppBarSwitch(&r.logToggle, "Logs", &r.th))

if r.updateAvailable {
Expand Down Expand Up @@ -230,6 +256,34 @@ func (r *Router) HandleMessage(msg *messages.Message) *messages.Message {
case "popup":
r.RemovePopup(data.ID)
}

case messages.RequestLogin:
if r.loggingIn {
logrus.Info("RequestLogin, while already logging in")
break
}
r.loggingIn = true
ctx, cancel := context.WithCancel(r.Ctx)
loginPopup := popups.NewGuiAuth(r.Invalidate, cancel, func(err error) {})
r.PushPopup(loginPopup)
c := make(chan struct{})
go func() {
err := utils.Auth.Login(ctx, loginPopup)
if err != nil {
if !errors.Is(err, context.Canceled) {
r.PushPopup(popups.NewErrorPopup(err, nil, false))
}
}
r.loggingIn = false
r.Invalidate()
close(c)
}()
if data.Wait {
<-c
}

case messages.Error:
r.PushPopup(popups.NewErrorPopup(data, nil, false))
}

for _, p := range r.popups {
Expand Down
36 changes: 26 additions & 10 deletions ui/gui/popups/authpopup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,36 @@ import (
)

type guiAuth struct {
invalidate func()
cancel func()
onError func(error)
uri string
click widget.Clickable
code string
codeSelect widget.Selectable
err error
close widget.Clickable
}

func NewGuiAuth(uri, code string) Popup {
return &guiAuth{
uri: uri,
code: code,
func (g *guiAuth) AuthCode(uri string, code string) {
g.uri = uri
g.code = code
g.invalidate()
}

func (g *guiAuth) Finished(err error) {
if err != nil {
g.onError(err)
}
messages.Router.Handle(&messages.Message{
Source: "ui",
Target: "ui",
Data: messages.Close{Type: "popup", ID: g.ID()},
})
g.cancel()
}

func NewGuiAuth(invalidate func(), cancel func(), onError func(error)) *guiAuth {
return &guiAuth{invalidate: invalidate, cancel: cancel, onError: onError}
}

func (guiAuth) ID() string {
Expand All @@ -34,12 +51,12 @@ func (g *guiAuth) Layout(gtx layout.Context, th *material.Theme) layout.Dimensio
}

if g.close.Clicked(gtx) {
utils.Auth.Cancel()
messages.Router.Handle(&messages.Message{
Source: "ui",
Target: "ui",
Data: messages.Close{Type: "popup", ID: g.ID()},
})
g.cancel()
}

return LayoutPopupBackground(gtx, th, "guiAuth", func(gtx C) D {
Expand All @@ -48,6 +65,9 @@ func (g *guiAuth) Layout(gtx layout.Context, th *material.Theme) layout.Dimensio
}.Layout(gtx,
layout.Flexed(1, func(gtx C) D {
return layout.Center.Layout(gtx, func(gtx C) D {
if g.code == "" {
return material.Body1(th, "Loading").Layout(gtx)
}
return layout.Flex{Axis: layout.Vertical, Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Flex{
Expand Down Expand Up @@ -87,9 +107,5 @@ func (g *guiAuth) Layout(gtx layout.Context, th *material.Theme) layout.Dimensio
}

func (p *guiAuth) HandleMessage(msg *messages.Message) *messages.Message {
switch m := msg.Data.(type) {
case messages.Error:
p.err = m
}
return nil
}
4 changes: 3 additions & 1 deletion ui/gui/popups/errorpopup.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func (errorPopup) ID() string {

func (e *errorPopup) Layout(gtx C, th *material.Theme) D {
if e.close.Clicked(gtx) {
e.onClose()
if e.onClose != nil {
e.onClose()
}
messages.Router.Handle(&messages.Message{
Source: e.ID(),
Target: "ui",
Expand Down
Loading

0 comments on commit d719c1d

Please sign in to comment.