diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 8aedf110..58a4bbc5 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -136,6 +136,11 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge prevReactionPoll: map[networkid.PortalKey]time.Time{}, } + + if !login.Metadata.(*UserLoginMetadata).Session.HasAuthKey() { + return &client, nil + } + dispatcher := UpdateDispatcher{ UpdateDispatcher: tg.NewUpdateDispatcher(), EntityHandler: client.onEntityUpdate, @@ -417,6 +422,7 @@ func (t *TelegramClient) onConnectionStateChange(reason string) func() { } else { t.sendBadCredentials("You're not logged in") t.userLogin.Metadata.(*UserLoginMetadata).Session.AuthKey = nil + t.client = nil if err := t.userLogin.Save(ctx); err != nil { log.Err(err).Msg("failed to save user login") } @@ -427,13 +433,14 @@ func (t *TelegramClient) onConnectionStateChange(reason string) func() { func (t *TelegramClient) onAuthError(err error) { t.sendBadCredentials(err.Error()) t.userLogin.Metadata.(*UserLoginMetadata).Session.AuthKey = nil + t.client = nil if err := t.userLogin.Save(context.Background()); err != nil { t.main.Bridge.Log.Err(err).Msg("failed to save user login") } } func (t *TelegramClient) Connect(ctx context.Context) error { - if len(t.userLogin.Metadata.(*UserLoginMetadata).Session.AuthKey) != 256 { + if !t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey() { t.sendBadCredentials("User does not have an auth key") return nil } @@ -626,6 +633,7 @@ func (t *TelegramClient) LogoutRemote(ctx context.Context) { Str("action", "logout_remote"). Int64("user_id", t.telegramUserID). Logger() + log.Info().Msg("Logging out") err := t.ScopedStore.DeleteUserState(ctx) if err != nil { @@ -642,6 +650,10 @@ func (t *TelegramClient) LogoutRemote(ctx context.Context) { log.Err(err).Msg("failed to delete access hashes for user") } + if !t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey() { + log.Info().Msg("User does not have an auth key, not logging out") + } + _, err = t.client.API().AuthLogOut(ctx) if err != nil { log.Err(err).Msg("failed to logout on Telegram") diff --git a/pkg/connector/config.go b/pkg/connector/config.go index a689c600..a036fae3 100644 --- a/pkg/connector/config.go +++ b/pkg/connector/config.go @@ -173,6 +173,10 @@ type UserLoginSession struct { Salt int64 `json:"salt,omitempty"` } +func (u UserLoginSession) HasAuthKey() bool { + return len(u.AuthKey) == 256 +} + type UserLoginMetadata struct { Phone string `json:"phone"` Session UserLoginSession `json:"session"` @@ -185,7 +189,7 @@ type UserLoginMetadata struct { } func (s *UserLoginSession) Load(_ context.Context) (*session.Data, error) { - if len(s.AuthKey) != 256 { + if !s.HasAuthKey() { return nil, session.ErrNotFound } keyID := crypto.Key(s.AuthKey).ID()