]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/TwitchToken.php
fix some twitch bot stuff
[alttp.git] / app / Models / TwitchToken.php
diff --git a/app/Models/TwitchToken.php b/app/Models/TwitchToken.php
new file mode 100644 (file)
index 0000000..9b15977
--- /dev/null
@@ -0,0 +1,47 @@
+<?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',
+       ];
+
+}