Skip to content

Commit 6ab914d

Browse files
committed
feat(qrlogin): enhance Export and Auth methods to handle AuthLoginTokenSuccess case
1 parent 9f81eb6 commit 6ab914d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

telegram/auth/qrlogin/errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package qrlogin
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/gotd/td/tg"
78
)
89

10+
// ErrAlreadyAuthenticated indicates that user is already authenticated
11+
// and no new QR token is needed.
12+
var ErrAlreadyAuthenticated = errors.New("already authenticated")
13+
914
// MigrationNeededError reports that Telegram requested DC migration to continue login.
1015
type MigrationNeededError struct {
1116
MigrateTo *tg.AuthLoginTokenMigrateTo

telegram/auth/qrlogin/qrlogin.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (q QR) Export(ctx context.Context, exceptIDs ...int64) (Token, error) {
5353
case *tg.AuthLoginTokenSuccess:
5454
// Token was already accepted, authentication successful
5555
// Return empty token since no new token is needed
56+
ctx.Done()
5657
return Token{}, nil
5758
case *tg.AuthLoginTokenMigrateTo:
5859
// Migration needed
@@ -157,6 +158,13 @@ func (q QR) Auth(
157158
if err != nil {
158159
return nil, err
159160
}
161+
162+
// If token is empty, it means AuthLoginTokenSuccess was returned
163+
// and authentication is already complete
164+
if token.String() == "" {
165+
return q.Import(ctx)
166+
}
167+
160168
timer := q.clock.Timer(until(token))
161169
defer clock.StopTimer(timer)
162170

@@ -173,6 +181,13 @@ func (q QR) Auth(
173181
if err != nil {
174182
return nil, err
175183
}
184+
185+
// If empty token, it means AuthLoginTokenSuccess was returned
186+
if t.String() == "" {
187+
// QR was scanned and accepted, break out of loop
188+
break
189+
}
190+
176191
token = t
177192
timer.Reset(until(token))
178193

0 commit comments

Comments
 (0)