Add a LoginWithToken method which is a cheaper way to login. Closes #89.

Eventually we should consider allowing Login/LoginWithToken to mutate
s.Token, it would probably simplify the API a bit.
This commit is contained in:
Chris Rhodes 2016-01-17 11:13:02 -08:00
parent bf20ffffa8
commit 3561ad1fa6

View file

@ -128,8 +128,8 @@ func unmarshal(data []byte, v interface{}) error {
// Functions specific to Discord Sessions // Functions specific to Discord Sessions
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Login asks the Discord server for an authentication token // Login asks the Discord server for an authentication token.
func (s *Session) Login(email string, password string) (token string, err error) { func (s *Session) Login(email, password string) (token string, err error) {
data := struct { data := struct {
Email string `json:"email"` Email string `json:"email"`
@ -154,6 +154,18 @@ func (s *Session) Login(email string, password string) (token string, err error)
return return
} }
// LoginWithToken will verify a login token, or return a new one if it is invalid.
// This is the preferred way to login, as it uses less rate limiting quota.
func (s *Session) LoginWithToken(email, password, token string) (token string, err error) {
old := s.Token
s.Token = token
token, err = s.Login(email, password)
s.Token = old
return
}
// Register sends a Register request to Discord, and returns the authentication token // Register sends a Register request to Discord, and returns the authentication token
// Note that this account is temporary and should be verified for future use. // Note that this account is temporary and should be verified for future use.
// Another option is to save the authentication token external, but this isn't recommended. // Another option is to save the authentication token external, but this isn't recommended.