X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FTwitchToken.php;h=3a35d8c8ca0ef10606ebbeb517250e18349f34af;hb=76e2a3da33fc0cccbdb8864416bbb2a8684987ab;hp=9b15977b41e881afae2cf5c367c09ed553156cee;hpb=898d01d4ac5ccaa23621abda0761a893ff8c1074;p=alttp.git diff --git a/app/Models/TwitchToken.php b/app/Models/TwitchToken.php index 9b15977..3a35d8c 100644 --- a/app/Models/TwitchToken.php +++ b/app/Models/TwitchToken.php @@ -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', ];