]> git.localhorst.tv Git - alttp.git/commitdiff
upgrade larascord to v6
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 2 Sep 2024 11:46:04 +0000 (13:46 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 2 Sep 2024 11:46:04 +0000 (13:46 +0200)
app/Http/Controllers/DiscordController.php [deleted file]
app/Models/User.php
composer.json
composer.lock
config/larascord.php
database/migrations/2023_05_25_121158_add_remember_token_to_users_table.php [new file with mode: 0644]
database/migrations/2023_05_26_165816_create_discord_access_tokens_table.php [new file with mode: 0644]
database/migrations/2023_05_27_055058_remove_refresh_token_from_users_table.php [new file with mode: 0644]
database/migrations/2023_06_11_062809_update_users_table.php [new file with mode: 0644]
routes/web.php

diff --git a/app/Http/Controllers/DiscordController.php b/app/Http/Controllers/DiscordController.php
deleted file mode 100644 (file)
index 1625e32..0000000
+++ /dev/null
@@ -1,161 +0,0 @@
-<?php
-
-namespace App\Http\Controllers;
-
-use App\Models\User;
-use App\Providers\RouteServiceProvider;
-use Illuminate\Http\Request;
-use Illuminate\Support\Facades\Auth;
-use Illuminate\Support\Facades\Http;
-
-class DiscordController extends \Jakyeru\Larascord\Http\Controllers\DiscordController
-{
-
-       /**
-        * Handles the Discord OAuth2 login.
-        *
-        * @param Request $request
-        * @return \Illuminate\Http\JsonResponse
-        */
-       public function handle(Request $request)//: \Illuminate\Http\JsonResponse
-       {
-               // Checking if the authorization code is present in the request.
-               if ($request->missing('code')) {
-                       if (env('APP_DEBUG')) {
-                               return response()->json([
-                                       'larascord_message' => config('larascord.error_messages.missing_code', 'The authorization code is missing.'),
-                                       'code' => 400
-                               ]);
-                       } else {
-                               return redirect('/')->with('error', config('larascord.error_messages.missing_code', 'The authorization code is missing.'));
-                       }
-               }
-
-               // Getting the access_token from the Discord API.
-               try {
-                       $accessToken = $this->getDiscordAccessToken($request->get('code'));
-               } catch (\Exception $e) {
-                       if (env('APP_DEBUG')) {
-                               return response()->json([
-                                       'larascord_message' => config('larascord.error_messages.invalid_code', 'The authorization code is invalid.'),
-                                       'message' => $e->getMessage(),
-                                       'code' => $e->getCode()
-                               ]);
-                       } else {
-                               return redirect('/')->with('error', config('larascord.error_messages.invalid_code', 'The authorization code is invalid.'));
-                       }
-               }
-
-               // Using the access_token to get the user's Discord ID.
-               try {
-                       $user = $this->getDiscordUser($accessToken->access_token);
-               } catch (\Exception $e) {
-                       if (env('APP_DEBUG')) {
-                               return response()->json([
-                                       'larascord_message' => config('larascord.error_messages.authorization_failed', 'The authorization failed.'),
-                                       'message' => $e->getMessage(),
-                                       'code' => $e->getCode()
-                               ]);
-                       } else {
-                               return redirect('/')->with('error', config('larascord.error_messages.authorization_failed', 'The authorization failed.'));
-                       }
-               }
-
-               // Making sure the current logged-in user's ID is matching the ID retrieved from the Discord API.
-               if (Auth::check() && (Auth::id() !== $user->id)) {
-                       Auth::logout();
-                       return redirect('/')->with('error', config('larascord.error_messages.invalid_user', 'The user ID doesn\'t match the logged-in user.'));
-               }
-
-               // Confirming the session in case the user was redirected from the password.confirm middleware.
-               if (Auth::check()) {
-                       $request->session()->put('auth.password_confirmed_at', time());
-               }
-
-               // Trying to create or update the user in the database.
-               try {
-                       $user = $this->createOrUpdateUser($user, $accessToken->refresh_token);
-               } catch (\Exception $e) {
-                       if (env('APP_DEBUG')) {
-                               return response()->json([
-                                       'larascord_message' => config('larascord.error_messages.database_error', 'There was an error while trying to create or update the user.'),
-                                       'message' => $e->getMessage(),
-                                       'code' => $e->getCode()
-                               ]);
-                       } else {
-                               return redirect('/')->with('error', config('larascord.error_messages.database_error', 'There was an error while trying to create or update the user.'));
-                       }
-               }
-
-               // Authenticating the user if the user is not logged in.
-               if (!Auth::check()) {
-                       Auth::login($user);
-               }
-
-               // Redirecting the user to the intended page or to the home page.
-               return redirect()->intended(RouteServiceProvider::HOME);
-       }
-
-       /**
-        * Handles the Discord OAuth2 callback.
-        *
-        * @param string $code
-        * @return object
-        * @throws \Illuminate\Http\Client\RequestException
-        */
-       private function getDiscordAccessToken(string $code): object
-       {
-               $this->tokenData['code'] = $code;
-
-               $response = Http::asForm()->post($this->tokenURL, $this->tokenData);
-
-               $response->throw();
-
-               return json_decode($response->body());
-       }
-
-       /**
-        * Handles the Discord OAuth2 login.
-        *
-        * @param string $access_token
-        * @return object
-        * @throws \Illuminate\Http\Client\RequestException
-        */
-       private function getDiscordUser(string $access_token): object
-       {
-               $response = Http::withToken($access_token)->get($this->apiURLBase);
-
-               $response->throw();
-
-               return json_decode($response->body());
-       }
-
-       /**
-        * Handles the creation or update of the user.
-        *
-        * @param object $user
-        * @param string $refresh_token
-        * @return User
-        * @throws \Exception
-        */
-       private function createOrUpdateUser(object $user, string $refresh_token): User
-       {
-               return User::updateOrCreate(
-                       [
-                               'id' => $user->id,
-                       ],
-                       [
-                               'username' => $user->username,
-                               'discord_nickname' => isset($user->global_name) ? $user->global_name : NULL,
-                               'discriminator' => $user->discriminator,
-                               'email' => isset($user->email) ? $user->email : NULL,
-                               'avatar' => $user->avatar ?: NULL,
-                               'verified' => isset($user->verified) ? $user->verified : 0,
-                               'locale' => $user->locale,
-                               'mfa_enabled' => $user->mfa_enabled,
-                               'refresh_token' => $refresh_token
-                       ]
-               );
-       }
-
-}
index db834b7a240ca423141f254a1494fc9916b82c22..9360255455f9dbf1696a933e54e744ff457705c1 100644 (file)
@@ -6,12 +6,13 @@ use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Foundation\Auth\User as Authenticatable;
 use Illuminate\Notifications\Notifiable;
+use Jakyeru\Larascord\Traits\InteractsWithDiscord;
 use Laravel\Sanctum\HasApiTokens;
 
 
 class User extends Authenticatable
 {
-       use HasApiTokens, HasFactory, Notifiable;
+       use HasApiTokens, HasFactory, InteractsWithDiscord, Notifiable;
 
        public function findResult(Round $round) {
                foreach ($round->results as $result) {
@@ -195,14 +196,20 @@ class User extends Authenticatable
                'id',
                'username',
                'discord_nickname',
+               'global_name',
                'discriminator',
                'email',
                'avatar',
                'verified',
+               'banner',
+               'banner_color',
+               'accent_color',
                'locale',
                'mfa_enabled',
-               'refresh_token',
+               'premium_type',
+               'public_flags',
                'role',
+               'roles',
        ];
 
        /**
@@ -213,7 +220,6 @@ class User extends Authenticatable
        protected $hidden = [
                'email',
                'mfa_enabled',
-               'refresh_token',
                'remember_token',
        ];
 
index 4a8569ca0bbedcf4cd75b6e622fbb532319a3c53..4b2522ac840c4190a456bf71f65985f8d466f935 100644 (file)
@@ -8,7 +8,7 @@
         "php": "^8.1",
         "doctrine/dbal": "^3.3",
         "guzzlehttp/guzzle": "^7.2",
-        "jakyeru/larascord": "^3.0",
+        "jakyeru/larascord": "^6.0",
         "laravel/breeze": "^2.0",
         "laravel/framework": "^11.0",
         "laravel/reverb": "^1.2",
index 9f1ff9eed6efbd0dfbe1f14f0bcc580c2facbc8b..f6b7cf0cfa36df67098898ef088f1a730f8edfc7 100644 (file)
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "79d306d3125d944d59bac955ffb58da5",
+    "content-hash": "647b3c7cc8a622512318ec67392acbf8",
     "packages": [
         {
             "name": "brick/math",
         },
         {
             "name": "jakyeru/larascord",
-            "version": "v3.2.0",
+            "version": "v6.0.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/JakyeRU/Larascord.git",
-                "reference": "1ad8ac83e973b4b646ecdc78dcf94930131bada3"
+                "reference": "1673d0223a14114d7a1b09f6baa7d808b49f40ac"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/JakyeRU/Larascord/zipball/1ad8ac83e973b4b646ecdc78dcf94930131bada3",
-                "reference": "1ad8ac83e973b4b646ecdc78dcf94930131bada3",
+                "url": "https://api.github.com/repos/JakyeRU/Larascord/zipball/1673d0223a14114d7a1b09f6baa7d808b49f40ac",
+                "reference": "1673d0223a14114d7a1b09f6baa7d808b49f40ac",
                 "shasum": ""
             },
             "require": {
-                "guzzlehttp/guzzle": "^7.4"
+                "guzzlehttp/guzzle": "^7.5",
+                "laravel/breeze": "^v2.0",
+                "laravel/framework": "^11",
+                "php": "^8.2|^8.3"
             },
             "require-dev": {
-                "orchestra/testbench": "^6.22"
+                "orchestra/testbench": "^9"
             },
             "type": "library",
             "extra": {
             "description": "Larascord is a package that allows you to authenticate users in your Laravel application using Discord.",
             "support": {
                 "issues": "https://github.com/JakyeRU/Larascord/issues",
-                "source": "https://github.com/JakyeRU/Larascord/tree/v3.2.0"
+                "source": "https://github.com/JakyeRU/Larascord/tree/v6.0.1"
             },
-            "time": "2022-06-26T11:09:59+00:00"
+            "time": "2024-08-06T13:58:27+00:00"
         },
         {
             "name": "laravel/breeze",
index ee52e708bd0bad3d00bd8308e46297ee61ab0c43..8696fc640009e835cc50dc4a00b24d52f1e7f04f 100644 (file)
@@ -101,4 +101,8 @@ return [
         'database_error' => 'There was an error with the database. Please try again later.',
     ],
 
+       'guilds' => [],
+
+       'guild_roles' => [],
+
 ];
diff --git a/database/migrations/2023_05_25_121158_add_remember_token_to_users_table.php b/database/migrations/2023_05_25_121158_add_remember_token_to_users_table.php
new file mode 100644 (file)
index 0000000..13f0499
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->rememberToken()->after('refresh_token');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->dropRememberToken();
+        });
+    }
+};
diff --git a/database/migrations/2023_05_26_165816_create_discord_access_tokens_table.php b/database/migrations/2023_05_26_165816_create_discord_access_tokens_table.php
new file mode 100644 (file)
index 0000000..2f8afec
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::create('discord_access_tokens', function (Blueprint $table) {
+            $table->id();
+            $table->string('access_token');
+            $table->string('refresh_token');
+            $table->string('token_type');
+            $table->integer('expires_in');
+            $table->timestamp('expires_at');
+            $table->string('scope');
+            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::dropIfExists('discord_access_tokens');
+    }
+};
diff --git a/database/migrations/2023_05_27_055058_remove_refresh_token_from_users_table.php b/database/migrations/2023_05_27_055058_remove_refresh_token_from_users_table.php
new file mode 100644 (file)
index 0000000..a6c14da
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->dropColumn('refresh_token');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->string('refresh_token')->nullable();
+        });
+    }
+};
diff --git a/database/migrations/2023_06_11_062809_update_users_table.php b/database/migrations/2023_06_11_062809_update_users_table.php
new file mode 100644 (file)
index 0000000..9b903d9
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->string('global_name')->nullable()->after('username');
+            $table->string('discriminator')->nullable()->change();
+            $table->string('banner')->nullable()->after('verified');
+            $table->string('banner_color')->nullable()->after('banner');
+            $table->string('accent_color')->nullable()->after('banner_color');
+            $table->string('premium_type')->nullable()->after('mfa_enabled');
+            $table->string('public_flags')->nullable()->after('premium_type');
+            $table->boolean('verified')->nullable()->change();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->dropColumn('global_name');
+            $table->string('discriminator')->change();
+            $table->dropColumn('banner');
+            $table->dropColumn('banner_color');
+            $table->dropColumn('accent_color');
+            $table->dropColumn('premium_type');
+            $table->dropColumn('public_flags');
+            $table->boolean('verified')->change();
+        });
+    }
+};
index 53597afa3bc10144504e95a0c972bde7c3ab409a..2dcf2980f5a5cde028d0eaeb78d3145a700e4aee 100644 (file)
@@ -1,11 +1,11 @@
 <?php
 
-use App\Http\Controllers\DiscordController;
 use App\Http\Controllers\SitemapXmlController;
 use App\Http\Controllers\TechniqueController;
 use App\Models\Event;
 use App\Models\Technique;
 use Illuminate\Support\Facades\Route;
+use Jakyeru\Larascord\Http\Controllers\DiscordController;
 
 /*
 |--------------------------------------------------------------------------
@@ -43,11 +43,3 @@ Route::get('/tech/{name}', function($name) {
 Route::get('/twitch/guessing-game-leaderboard/{channel:twitch_id}/{type}', 'App\Http\Controllers\ChannelController@getGuessingGameLeaderboard');
 
 Route::view('/{path?}', 'app')->where('path', '.*');
-
-Route::group(['prefix' => config('larascord.prefix'), 'middleware' => ['web']], function() {
-       Route::get('/callback', [DiscordController::class, 'handle'])
-               ->name('larascord.login');
-
-       Route::redirect('/refresh-token', '/login')
-               ->name('larascord.refresh_token');
-});