From 3561ad1fa60f54383271315d8482a4f38f22cfd8 Mon Sep 17 00:00:00 2001 From: Chris Rhodes Date: Sun, 17 Jan 2016 11:13:02 -0800 Subject: [PATCH] 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. --- restapi.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/restapi.go b/restapi.go index 3d2f6bb..cdb99d4 100644 --- a/restapi.go +++ b/restapi.go @@ -128,8 +128,8 @@ func unmarshal(data []byte, v interface{}) error { // Functions specific to Discord Sessions // ------------------------------------------------------------------------------------------------ -// Login asks the Discord server for an authentication token -func (s *Session) Login(email string, password string) (token string, err error) { +// Login asks the Discord server for an authentication token. +func (s *Session) Login(email, password string) (token string, err error) { data := struct { Email string `json:"email"` @@ -154,6 +154,18 @@ func (s *Session) Login(email string, password string) (token string, err error) 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 // 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.