diff options
-rw-r--r-- | goanilist.go | 15 |
1 files changed, 8 insertions, 7 deletions
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 { | |||
39 | id: id, | 39 | id: id, |
40 | secret: secret, | 40 | secret: secret, |
41 | } | 41 | } |
42 | err := db.QueryRow("SELECT token, refresh_token FROM login.anilist_api_tokens WHERE user_id = $1;", user).Scan(&c.token, &c.refreshToken) | 42 | 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) |
43 | c.IsCoupled = err == nil | 43 | c.IsCoupled = err == nil |
44 | return c | 44 | return c |
45 | } | 45 | } |
@@ -75,7 +75,7 @@ func (c *Client) CoupleByPin(pin string) error { | |||
75 | if err != nil { | 75 | if err != nil { |
76 | return err | 76 | return err |
77 | } | 77 | } |
78 | _, 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) | 78 | _, 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) |
79 | if err != nil { | 79 | if err != nil { |
80 | return err | 80 | return err |
81 | } | 81 | } |
@@ -89,6 +89,9 @@ func (c *Client) RenewToken() error { | |||
89 | if !c.IsCoupled { | 89 | if !c.IsCoupled { |
90 | return ErrAnilistNotCoupled | 90 | return ErrAnilistNotCoupled |
91 | } | 91 | } |
92 | if !c.NeedsToRenew() { | ||
93 | return nil | ||
94 | } | ||
92 | log.Println("Token expired, get new token..") | 95 | log.Println("Token expired, get new token..") |
93 | res := AccessTokenResult{} | 96 | res := AccessTokenResult{} |
94 | err := c.post("auth/access_token", url.Values{ | 97 | err := c.post("auth/access_token", url.Values{ |
@@ -100,7 +103,7 @@ func (c *Client) RenewToken() error { | |||
100 | if err != nil { | 103 | if err != nil { |
101 | return err | 104 | return err |
102 | } | 105 | } |
103 | _, err = c.db.Exec("UPDATE login.anilist_api_tokens SET token = $1 WHERE user_id = $2;", res.AccessToken, c.user) | 106 | _, 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) |
104 | if err != nil { | 107 | if err != nil { |
105 | return err | 108 | return err |
106 | } | 109 | } |
@@ -113,10 +116,8 @@ func (c *Client) User() (*UserResult, error) { | |||
113 | if !c.IsCoupled { | 116 | if !c.IsCoupled { |
114 | return nil, ErrAnilistNotCoupled | 117 | return nil, ErrAnilistNotCoupled |
115 | } | 118 | } |
116 | if c.NeedsToRenew() { | 119 | if err := c.RenewToken(); err != nil { |
117 | if err := c.RenewToken(); err != nil { | 120 | return nil, err |
118 | return nil, err | ||
119 | } | ||
120 | } | 121 | } |
121 | res := &UserResult{} | 122 | res := &UserResult{} |
122 | err := c.get("user", res) | 123 | err := c.get("user", res) |