package auth
import (
"context"
"github.com/go-faster/errors"
"github.com/gotd/td/tg"
)
type Status struct {
Authorized bool
User *tg .User
}
func (c *Client ) Status (ctx context .Context ) (*Status , error ) {
u , err := c .self (ctx )
if IsUnauthorized (err ) {
return &Status {}, nil
}
if err != nil {
return nil , err
}
return &Status {
Authorized : true ,
User : u ,
}, nil
}
func (c *Client ) IfNecessary (ctx context .Context , flow Flow ) error {
auth , err := c .Status (ctx )
if err != nil {
return errors .Wrap (err , "get auth status" )
}
if auth .Authorized {
return nil
}
if err := flow .Run (ctx , c ); err != nil {
return errors .Wrap (err , "auth flow" )
}
return nil
}
func (c *Client ) Test (ctx context .Context , dc int ) error {
return c .IfNecessary (ctx , NewFlow (Test (c .rand , dc ), SendCodeOptions {}))
}
func (c *Client ) TestUser (ctx context .Context , phone string , dc int ) error {
return c .IfNecessary (ctx , NewFlow (TestUser (phone , dc ), SendCodeOptions {}))
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .