--- /dev/null
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\Http;
+
+class TwitchToken extends Model
+{
+ use HasFactory;
+
+ public function getAuthUrl() {
+ return 'https://id.twitch.tv/oauth2/authorize'
+ .'?response_type=code'
+ .'&client_id='.rawurlencode(config('twitch.client_id'))
+ .'&redirect_uri='.rawurlencode(config('twitch.redirect_uri'))
+ .'&scope='.implode('+', array_map('rawurlencode', $this->scope));
+ }
+
+ public function refresh() {
+ $rsp = Http::post('https://id.twitch.tv/oauth2/token', [
+ 'client_id' => config('twitch.client_id'),
+ 'client_secret' => config('twitch.client_secret'),
+ 'grant_type' => 'refresh_token',
+ 'refresh_token' => $this->refresh,
+ ]);
+ if (!$rsp->successful()) {
+ throw new \Exception($rsp['message']);
+ }
+ $this->type = $rsp['token_type'];
+ $this->scope = $rsp['scope'];
+ $this->access = $rsp['access_token'];
+ $this->refresh = $rsp['refresh_token'];
+ $this->save();
+ }
+
+ protected $casts = [
+ 'scope' => 'array',
+ ];
+
+ protected $hidden = [
+ 'access',
+ 'refresh',
+ ];
+
+}