From dc7b1324e6eab593f9af18be22516ad5cbd559ea Mon Sep 17 00:00:00 2001 From: rtz12 Date: Sun, 13 Nov 2016 18:50:46 +0100 Subject: Expires kommt nun auch in die Datenbank diff --git a/goanilist.go b/goanilist.go index 54cdaf8..dc5d4b9 100644 --- a/goanilist.go +++ b/goanilist.go @@ -39,7 +39,7 @@ func NewClient(db *sql.DB, user int, id, secret string) *Client { id: id, secret: secret, } - err := db.QueryRow("SELECT token, refresh_token FROM login.anilist_api_tokens WHERE user_id = $1;", user).Scan(&c.token, &c.refreshToken) + err := db.QueryRow("SELECT token, refresh_token, expires_at FROM login.anilist_api_tokens WHERE user_id = $1;", user).Scan(&c.token, &c.refreshToken, &c.tokenExpires) c.IsCoupled = err == nil return c } @@ -75,7 +75,7 @@ func (c *Client) CoupleByPin(pin string) error { if err != nil { return err } - _, err = c.db.Exec("INSERT INTO login.anilist_api_tokens (user_id, token, refresh_token) VALUES ($1, $2, $3);", c.user, res.AccessToken, res.RefreshToken) + _, err = c.db.Exec("INSERT INTO login.anilist_api_tokens (user_id, token, refresh_token, expires_at) VALUES ($1, $2, $3, $4);", c.user, res.AccessToken, res.RefreshToken, res.Expires) if err != nil { return err } @@ -89,6 +89,9 @@ func (c *Client) RenewToken() error { if !c.IsCoupled { return ErrAnilistNotCoupled } + if !c.NeedsToRenew() { + return nil + } log.Println("Token expired, get new token..") res := AccessTokenResult{} err := c.post("auth/access_token", url.Values{ @@ -100,7 +103,7 @@ func (c *Client) RenewToken() error { if err != nil { return err } - _, err = c.db.Exec("UPDATE login.anilist_api_tokens SET token = $1 WHERE user_id = $2;", res.AccessToken, c.user) + _, err = c.db.Exec("UPDATE login.anilist_api_tokens SET token = $1, expires_at = $2 WHERE user_id = $3;", res.AccessToken, res.Expires, c.user) if err != nil { return err } @@ -113,10 +116,8 @@ func (c *Client) User() (*UserResult, error) { if !c.IsCoupled { return nil, ErrAnilistNotCoupled } - if c.NeedsToRenew() { - if err := c.RenewToken(); err != nil { - return nil, err - } + if err := c.RenewToken(); err != nil { + return nil, err } res := &UserResult{} err := c.get("user", res) -- cgit v0.10.1