From b918c32c3f3a6cf82d2ad614e5fadcdc24e411a9 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 2 Sep 2024 13:46:04 +0200 Subject: [PATCH] upgrade larascord to v6 --- app/Http/Controllers/DiscordController.php | 161 ------------------ app/Models/User.php | 12 +- composer.json | 2 +- composer.lock | 21 ++- config/larascord.php | 4 + ...1158_add_remember_token_to_users_table.php | 28 +++ ...816_create_discord_access_tokens_table.php | 34 ++++ ..._remove_refresh_token_from_users_table.php | 28 +++ .../2023_06_11_062809_update_users_table.php | 42 +++++ routes/web.php | 10 +- 10 files changed, 159 insertions(+), 183 deletions(-) delete mode 100644 app/Http/Controllers/DiscordController.php create mode 100644 database/migrations/2023_05_25_121158_add_remember_token_to_users_table.php create mode 100644 database/migrations/2023_05_26_165816_create_discord_access_tokens_table.php create mode 100644 database/migrations/2023_05_27_055058_remove_refresh_token_from_users_table.php create mode 100644 database/migrations/2023_06_11_062809_update_users_table.php diff --git a/app/Http/Controllers/DiscordController.php b/app/Http/Controllers/DiscordController.php deleted file mode 100644 index 1625e32..0000000 --- a/app/Http/Controllers/DiscordController.php +++ /dev/null @@ -1,161 +0,0 @@ -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 - ] - ); - } - -} diff --git a/app/Models/User.php b/app/Models/User.php index db834b7..9360255 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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', ]; diff --git a/composer.json b/composer.json index 4a8569c..4b2522a 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 9f1ff9e..f6b7cf0 100644 --- a/composer.lock +++ b/composer.lock @@ -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", @@ -1730,23 +1730,26 @@ }, { "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": { @@ -1774,9 +1777,9 @@ "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", diff --git a/config/larascord.php b/config/larascord.php index ee52e70..8696fc6 100644 --- a/config/larascord.php +++ b/config/larascord.php @@ -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 index 0000000..13f0499 --- /dev/null +++ b/database/migrations/2023_05_25_121158_add_remember_token_to_users_table.php @@ -0,0 +1,28 @@ +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 index 0000000..2f8afec --- /dev/null +++ b/database/migrations/2023_05_26_165816_create_discord_access_tokens_table.php @@ -0,0 +1,34 @@ +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 index 0000000..a6c14da --- /dev/null +++ b/database/migrations/2023_05_27_055058_remove_refresh_token_from_users_table.php @@ -0,0 +1,28 @@ +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 index 0000000..9b903d9 --- /dev/null +++ b/database/migrations/2023_06_11_062809_update_users_table.php @@ -0,0 +1,42 @@ +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(); + }); + } +}; diff --git a/routes/web.php b/routes/web.php index 53597af..2dcf298 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,11 +1,11 @@ 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'); -}); -- 2.39.2