diff --git a/authHelper.go b/authHelper.go index 2f4816e..df5e07a 100644 --- a/authHelper.go +++ b/authHelper.go @@ -9,21 +9,6 @@ import ( "github.com/pkg/errors" ) -func ifAuthNecessary(ctx context.Context, c *auth.Client, conversator AuthConversator, phone string, sendOpts auth.SendCodeOptions) error { - auth, err := c.Status(ctx) - if err != nil { - return errors.Wrap(err, "get auth status") - } - if auth.Authorized { - return nil - } - if err := authFlow(ctx, c, conversator, phone, sendOpts); err != nil { - return errors.Wrap(err, "auth flow") - } - return nil - -} - type Flow auth.Flow func (f Flow) handleSignUp(ctx context.Context, client auth.FlowClient, phone, hash string, s *auth.SignUpRequired) error { diff --git a/client.go b/client.go index cb2d206..0476cc5 100644 --- a/client.go +++ b/client.go @@ -76,6 +76,9 @@ type Client struct { // PeerStorage is the storage for all the peers. // It is recommended to use storage.NewPeerStorage function for this field. PeerStorage *storage.PeerStorage + // NoAutoAuth is a flag to disable automatic authentication + // if the current session is invalid. + NoAutoAuth bool authConversator AuthConversator clientType ClientType @@ -176,6 +179,9 @@ type ClientOpts struct { // If < 0, compression will be disabled. // If == 0, default value will be used. CompressThreshold int + // NoAutoAuth is a flag to disable automatic authentication + // if the current session is invalid. + NoAutoAuth bool } // NewClient creates a new gotgproto client and logs in to telegram. @@ -221,6 +227,7 @@ func NewClient(appId int, apiHash string, cType ClientType, opts *ClientOpts) (* Logger: opts.Logger, SystemLangCode: opts.SystemLangCode, ClientLangCode: opts.ClientLangCode, + NoAutoAuth: opts.NoAutoAuth, authConversator: opts.AuthConversator, Dispatcher: d, PeerStorage: peerStorage, @@ -276,8 +283,24 @@ func (c *Client) login() error { authClient := c.Auth() if c.clientType.BotToken == "" { - if err := ifAuthNecessary(c.ctx, authClient, c.authConversator, c.clientType.Phone, auth.SendCodeOptions{}); err != nil { - return err + status, err := authClient.Status(c.ctx) + if err != nil { + return errors.Wrap(err, "get auth status") + } + if status.Authorized { + return nil + } + if c.NoAutoAuth { + return intErrors.ErrSessionUnauthorized + } + err = authFlow( + c.ctx, authClient, + c.authConversator, + c.clientType.Phone, + auth.SendCodeOptions{}, + ) + if err != nil { + return errors.Wrap(err, "auth flow") } } else { status, err := authClient.Status(c.ctx) diff --git a/errors/errors.go b/errors/errors.go index ff2b9e3..ac119e4 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -4,6 +4,7 @@ import "errors" var ( ErrClientAlreadyRunning = errors.New("client is already running") + ErrSessionUnauthorized = errors.New("session is unauthorized") ) var (