]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/TwitchToken.php
track twitch token expiration
[alttp.git] / app / Models / TwitchToken.php
index 9b15977b41e881afae2cf5c367c09ed553156cee..3a35d8c8ca0ef10606ebbeb517250e18349f34af 100644 (file)
@@ -18,6 +18,10 @@ class TwitchToken extends Model
                        .'&scope='.implode('+', array_map('rawurlencode', $this->scope));
        }
 
+       public function hasExpired() {
+               return now()->isAfter($this->expires_at);
+       }
+
        public function refresh() {
                $rsp = Http::post('https://id.twitch.tv/oauth2/token', [
                        'client_id' => config('twitch.client_id'),
@@ -32,9 +36,18 @@ class TwitchToken extends Model
                $this->scope = $rsp['scope'];
                $this->access = $rsp['access_token'];
                $this->refresh = $rsp['refresh_token'];
+               $this->expires_at = now()->add($rsp['expires_in'], 'second');
                $this->save();
        }
 
+       public function request() {
+               return Http::baseUrl('https://api.twitch.tv/helix')
+                       ->withToken($this->access)
+                       ->withHeaders([
+                               'Client-Id' => config('twitch.client_id'),
+                       ]);
+       }
+
        protected $casts = [
                'scope' => 'array',
        ];