refactor(login): phone input. close #5

This commit is contained in:
iyear 2022-09-10 15:05:13 +08:00
parent fa3c480a28
commit 52ca0caf01
2 changed files with 12 additions and 14 deletions

View file

@ -24,11 +24,20 @@ func (c noSignUp) AcceptTermsOfService(_ context.Context, tos tg.HelpTermsOfServ
// termAuth implements authentication via terminal. // termAuth implements authentication via terminal.
type termAuth struct { type termAuth struct {
noSignUp noSignUp
phone string
} }
func (a termAuth) Phone(_ context.Context) (string, error) { func (a termAuth) Phone(_ context.Context) (string, error) {
return a.phone, nil phone, err := input.DefaultUI().Ask(color.BlueString("Enter your phone number:"), &input.Options{
Default: color.CyanString("+86 12345678900"),
Loop: true,
Required: true,
})
if err != nil {
return "", err
}
color.Blue("Sending Code...")
return strings.TrimSpace(phone), nil
} }
func (a termAuth) Password(_ context.Context) (string, error) { func (a termAuth) Password(_ context.Context) (string, error) {

View file

@ -7,7 +7,6 @@ import (
"github.com/iyear/tdl/app/internal/tgc" "github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts" "github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv" "github.com/iyear/tdl/pkg/kv"
"github.com/tcnksm/go-input"
) )
func Run(ctx context.Context, ns, proxy string) error { func Run(ctx context.Context, ns, proxy string) error {
@ -29,17 +28,7 @@ func Run(ctx context.Context, ns, proxy string) error {
color.Blue("Login...") color.Blue("Login...")
color.Yellow("WARN: If data exists in the namespace, data will be overwritten") color.Yellow("WARN: If data exists in the namespace, data will be overwritten")
phone, err := input.DefaultUI().Ask(color.BlueString("Enter your phone number:"), &input.Options{ flow := auth.NewFlow(termAuth{}, auth.SendCodeOptions{})
Default: color.CyanString("+86 12345678900"),
Loop: true,
Required: true,
})
if err != nil {
return err
}
color.Blue("Send code...")
flow := auth.NewFlow(termAuth{phone: phone}, auth.SendCodeOptions{})
if err := c.Auth().IfNecessary(ctx, flow); err != nil { if err := c.Auth().IfNecessary(ctx, flow); err != nil {
return err return err
} }