+++ /dev/null
-<?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
- ]
- );
- }
-
-}
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) {
'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',
];
/**
protected $hidden = [
'email',
'mfa_enabled',
- 'refresh_token',
'remember_token',
];
"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",
"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",
'database_error' => 'There was an error with the database. Please try again later.',
],
+ 'guilds' => [],
+
+ 'guild_roles' => [],
+
];
--- /dev/null
+<?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();
+ });
+ }
+};
--- /dev/null
+<?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');
+ }
+};
--- /dev/null
+<?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();
+ });
+ }
+};
--- /dev/null
+<?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();
+ });
+ }
+};
<?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;
/*
|--------------------------------------------------------------------------
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');
-});