From 2423b9c2764609df912adbd780013ee3587d334f Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Mon, 10 Jan 2022 15:15:57 +0200 Subject: [PATCH 01/31] Added configuration. --- README.md | 3 +++ src/LarascordServiceProvider.php | 20 ++++++++++++++------ src/config/larascord.php | 1 + 3 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 src/config/larascord.php diff --git a/README.md b/README.md index 0de2dd1..0477150 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,9 @@ Larascord is a package that allows you to authenticate users in your Laravel app Your application should now be able to authenticate users using Discord. +You can publish Larascord's configuration using the following command: +* `php artisan vendor:publish --provider="JakyeRU\Larascord\LarascordServiceProvider" --tag="config"` + --- # Larascord Routes > 💡 These routes can be found in the `routes/auth.php` file. diff --git a/src/LarascordServiceProvider.php b/src/LarascordServiceProvider.php index b5f4689..4cbab01 100644 --- a/src/LarascordServiceProvider.php +++ b/src/LarascordServiceProvider.php @@ -8,7 +8,10 @@ class LarascordServiceProvider extends ServiceProvider { public function register() { - $this->registerCommands(); + if ($this->app->runningInConsole()) { + $this->registerCommands(); + $this->registerConfiguration(); + } } public function boot() @@ -18,10 +21,15 @@ public function boot() protected function registerCommands() { - if ($this->app->runningInConsole()) { - $this->commands([ - Console\Commands\InstallCommand::class, - ]); - } + $this->commands([ + Console\Commands\InstallCommand::class, + ]); + } + + protected function registerConfiguration() + { + $this->publishes([ + __DIR__.'/../config/larascord.php' => config_path('larascord.php'), + ], 'config'); } } \ No newline at end of file diff --git a/src/config/larascord.php b/src/config/larascord.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/src/config/larascord.php @@ -0,0 +1 @@ + Date: Mon, 10 Jan 2022 15:21:20 +0200 Subject: [PATCH 02/31] Updated README.md. --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0477150..945ce8c 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,13 @@ Larascord is a package that allows you to authenticate users in your Laravel app ## Laravel > :warning: You must use this package in a fresh Laravel application otherwise unexpected behavior may occur. ### Step 1: Install Larascord -* `composer require jakyeru/larascord` +```shell +composer require jakyeru/larascord +``` ### Step 2: Run the installation command -* `php artisan larascord:install` +```shell +php artisan larascord:install +``` ### Step 3: Follow Larascord's instructions * You can get your Discord application's `CLIENT ID` and `CLIENT SECRET` from the "OAuth2" tab of your application. * ![](https://i.imgur.com/YJnM4H5.png) @@ -42,8 +46,11 @@ Larascord is a package that allows you to authenticate users in your Laravel app Your application should now be able to authenticate users using Discord. +## Configuration You can publish Larascord's configuration using the following command: -* `php artisan vendor:publish --provider="JakyeRU\Larascord\LarascordServiceProvider" --tag="config"` +```shell +php artisan vendor:publish --provider="JakyeRU\Larascord\LarascordServiceProvider" --tag="config" +``` --- # Larascord Routes From 78e6940ec37b12d100971a08ee80e0960f8c55a7 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Mon, 10 Jan 2022 15:33:31 +0200 Subject: [PATCH 03/31] Renamed larascord.php to config.php. --- src/config/{larascord.php => config.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/config/{larascord.php => config.php} (100%) diff --git a/src/config/larascord.php b/src/config/config.php similarity index 100% rename from src/config/larascord.php rename to src/config/config.php From 58b38f86170ff13796f4e96b04ff919497685a47 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Mon, 10 Jan 2022 15:34:26 +0200 Subject: [PATCH 04/31] Moved registerConfiguration to the boot method and fixed the config path. --- src/LarascordServiceProvider.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/LarascordServiceProvider.php b/src/LarascordServiceProvider.php index 4cbab01..c6a7d7a 100644 --- a/src/LarascordServiceProvider.php +++ b/src/LarascordServiceProvider.php @@ -16,7 +16,9 @@ public function register() public function boot() { -// $this->loadRoutesFrom(__DIR__.'/../routes/web.php'); + if ($this->app->runningInConsole()) { + $this->registerConfiguration(); + } } protected function registerCommands() @@ -29,7 +31,7 @@ protected function registerCommands() protected function registerConfiguration() { $this->publishes([ - __DIR__.'/../config/larascord.php' => config_path('larascord.php'), + __DIR__.'./config/config.php' => config_path('larascord.php'), ], 'config'); } } \ No newline at end of file From 7691daf2f83b190cf317d5424699cc26dda4a6fe Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Mon, 10 Jan 2022 15:35:01 +0200 Subject: [PATCH 05/31] Updated the publish command in README.md. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 945ce8c..af0c33c 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ Your application should now be able to authenticate users using Discord. ## Configuration You can publish Larascord's configuration using the following command: ```shell -php artisan vendor:publish --provider="JakyeRU\Larascord\LarascordServiceProvider" --tag="config" +php artisan vendor:publish --provider="Jakyeru\Larascord\LarascordServiceProvider" --tag="config" ``` --- From 9251e22c7afc8e61606f1ef37fb9a7ddbf4893d1 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 18:33:18 +0200 Subject: [PATCH 06/31] Added PublishCommand.php. --- src/Console/Commands/PublishCommand.php | 48 +++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/Console/Commands/PublishCommand.php diff --git a/src/Console/Commands/PublishCommand.php b/src/Console/Commands/PublishCommand.php new file mode 100644 index 0000000..d5489cb --- /dev/null +++ b/src/Console/Commands/PublishCommand.php @@ -0,0 +1,48 @@ +info('Publishing Larascord\'s settings...'); + + // Publish the configuration file. + try { + shell_exec('php artisan vendor:publish --provider="Jakyeru\Larascord\LarascordServiceProvider" --tag=config'); + } catch (\Exception $e) { + $this->error('Could not publish the configuration file.'); + $this->error($e->getMessage()); + } + + // Inform the user that the command has finished. + $this->info('Larascord\'s settings published successfully.'); + } +} \ No newline at end of file From c02da2dbf4fc1f19f3a21f16ad3c0a0df6246f1f Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 18:42:52 +0200 Subject: [PATCH 07/31] Added the --force option to the publish command to overwrite existing configuration. --- src/Console/Commands/PublishCommand.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Console/Commands/PublishCommand.php b/src/Console/Commands/PublishCommand.php index d5489cb..f70c4ad 100644 --- a/src/Console/Commands/PublishCommand.php +++ b/src/Console/Commands/PublishCommand.php @@ -15,7 +15,7 @@ class PublishCommand extends Command * @var string */ protected $signature = 'larascord:publish - {--composer=global : Absolute path to the Composer binary which should be used to install packages}'; + {--force : Overwrite any existing files.}'; /** * The console command description. @@ -31,18 +31,28 @@ class PublishCommand extends Command */ public function handle() { - // Inform the user that the command is starting. - $this->info('Publishing Larascord\'s settings...'); + // Checking if the configuration has already been published. + if (file_exists(config_path('larascord.php')) && !$this->option('force')) { + $this->error('The configuration file has already been published.'); + $this->error('If you want to overwrite the existing file, use the --force option.'); + + return; + } + + // Checking if the command should be run forcefully. + if ($this->option('force')) { + $this->warn('This command is running in force mode. Any existing configuration file will be overwritten.'); + } // Publish the configuration file. try { - shell_exec('php artisan vendor:publish --provider="Jakyeru\Larascord\LarascordServiceProvider" --tag=config'); + shell_exec('php artisan vendor:publish --provider="Jakyeru\Larascord\LarascordServiceProvider" --tag=config' . ($this->option('force') ? ' --force' : '')); } catch (\Exception $e) { $this->error('Could not publish the configuration file.'); $this->error($e->getMessage()); } // Inform the user that the command has finished. - $this->info('Larascord\'s settings published successfully.'); + $this->info('Larascord\'s settings have been published successfully.'); } } \ No newline at end of file From bd70664ce32062fbd5a044aa9e37a67b8e352a70 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 18:43:40 +0200 Subject: [PATCH 08/31] Removed unused classes from PublishCommand.php. --- src/Console/Commands/PublishCommand.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Console/Commands/PublishCommand.php b/src/Console/Commands/PublishCommand.php index f70c4ad..6abd7a5 100644 --- a/src/Console/Commands/PublishCommand.php +++ b/src/Console/Commands/PublishCommand.php @@ -3,9 +3,6 @@ namespace Jakyeru\Larascord\Console\Commands; use Illuminate\Console\Command; -use Illuminate\Filesystem\Filesystem; -use Illuminate\Support\Facades\Validator; -use Symfony\Component\Process\Process; class PublishCommand extends Command { From fe05844720d1d617814770bf1f3587e4984ba195 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 18:45:05 +0200 Subject: [PATCH 09/31] Added the publish command to the LarascordServiceProvider.php --- src/LarascordServiceProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/LarascordServiceProvider.php b/src/LarascordServiceProvider.php index c6a7d7a..e710e71 100644 --- a/src/LarascordServiceProvider.php +++ b/src/LarascordServiceProvider.php @@ -25,6 +25,7 @@ protected function registerCommands() { $this->commands([ Console\Commands\InstallCommand::class, + Console\Commands\PublishCommand::class, ]); } From 97d91c13c3352cef62b61f0bf12212791c4d7024 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 18:45:47 +0200 Subject: [PATCH 10/31] Updated config.php with the required values. --- src/config/config.php | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/config/config.php b/src/config/config.php index b3d9bbc..8b3d762 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -1 +1,72 @@ env('DISCORD_CLIENT_ID', null), + + /* + |-------------------------------------------------------------------------- + | Application Secret + |-------------------------------------------------------------------------- + | + | This is the secret of your Discord application. + | + */ + + 'client_secret' => env('DISCORD_CLIENT_SECRET', null), + + /* + |-------------------------------------------------------------------------- + | Grant Type + |-------------------------------------------------------------------------- + | + | This is the grant type of your Discord application. It must be set to "authorization_code". + | + */ + + 'grant_type' => env('DISCORD_GRANT_TYPE', 'authorization_code'), + + /* + |-------------------------------------------------------------------------- + | Redirect URI + |-------------------------------------------------------------------------- + | + | This is the URI that Discord will redirect to after the user authorizes your application. + | + */ + + 'redirect_uri' => env('APP_URL', 'http://localhost:8000') . '/' . config('larascord.prefix') . '/callback', + + /* + |-------------------------------------------------------------------------- + | Scopes + |-------------------------------------------------------------------------- + | + | These are the OAuth2 scopes of your Discord application. + | + */ + + 'scopes' => env('DISCORD_SCOPES', 'identify&email'), + + /* + |-------------------------------------------------------------------------- + | Route Prefix + |-------------------------------------------------------------------------- + | + | This is the prefix that Larascord will use for its routes. + | For example, the prefix "larascord" will result in the route "https://domain.com/larascord/login". + | + */ + + 'prefix' => 'larascord', + +]; \ No newline at end of file From 9cb8823be3001e9f718f9b3b99be606b82134119 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 18:53:57 +0200 Subject: [PATCH 11/31] No longer using .env to get the scopes in config.php. --- src/config/config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/config.php b/src/config/config.php index 8b3d762..c4b0835 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -55,7 +55,7 @@ | */ - 'scopes' => env('DISCORD_SCOPES', 'identify&email'), + 'scopes' => 'identify&email', /* |-------------------------------------------------------------------------- From 9dabf9d16105dc9048b50ca7a32c0cea21eea3f9 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 19:04:15 +0200 Subject: [PATCH 12/31] Prefix is now an env variable in config.php. --- src/config/config.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/config.php b/src/config/config.php index c4b0835..6d65034 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -44,7 +44,7 @@ | */ - 'redirect_uri' => env('APP_URL', 'http://localhost:8000') . '/' . config('larascord.prefix') . '/callback', + 'redirect_uri' => env('APP_URL', 'http://localhost:8000') . env('DISCORD_PREFIX', 'larascord') . '/callback', /* |-------------------------------------------------------------------------- @@ -67,6 +67,6 @@ | */ - 'prefix' => 'larascord', + 'prefix' => env('DISCORD_PREFIX', 'larascord'), ]; \ No newline at end of file From 81ef58b56fd3e01a0f4be93e82f1cfb292004f19 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 19:05:35 +0200 Subject: [PATCH 13/31] Loading routes from web.php in LarascordServiceProvider.php. --- src/LarascordServiceProvider.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/LarascordServiceProvider.php b/src/LarascordServiceProvider.php index e710e71..732886f 100644 --- a/src/LarascordServiceProvider.php +++ b/src/LarascordServiceProvider.php @@ -19,6 +19,8 @@ public function boot() if ($this->app->runningInConsole()) { $this->registerConfiguration(); } + + $this->registerRoutes(); } protected function registerCommands() @@ -35,4 +37,9 @@ protected function registerConfiguration() __DIR__.'./config/config.php' => config_path('larascord.php'), ], 'config'); } + + protected function registerRoutes() + { + $this->loadRoutesFrom(__DIR__.'/routes/web.php'); + } } \ No newline at end of file From 96261621d0432dc3336467cb8ce65aea48d5ec51 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 19:06:06 +0200 Subject: [PATCH 14/31] Removed auth.php. --- src/routes/auth.php | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 src/routes/auth.php diff --git a/src/routes/auth.php b/src/routes/auth.php deleted file mode 100644 index b1f5d79..0000000 --- a/src/routes/auth.php +++ /dev/null @@ -1,31 +0,0 @@ -name('login'); - -Route::get('/confirm-password', [ConfirmablePasswordController::class, 'show']) - ->middleware('auth') - ->name('password.confirm'); - -Route::post('/logout', [AuthenticatedSessionController::class, 'destroy']) - ->middleware('auth') - ->name('logout'); - -Route::group(['prefix' => 'larascord'], function() { - Route::get('/callback', [DiscordController::class, 'handle']) - ->name('larascord.login'); - - Route::redirect('/refresh-token', '/login') - ->name('larascord.refresh_token'); -}); \ No newline at end of file From f5d86437cff07436d8017ff55a8c31f9cb30186a Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 19:15:20 +0200 Subject: [PATCH 15/31] Fixed config.php redirect_uri. --- src/config/config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/config.php b/src/config/config.php index 6d65034..e1e6f71 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -44,7 +44,7 @@ | */ - 'redirect_uri' => env('APP_URL', 'http://localhost:8000') . env('DISCORD_PREFIX', 'larascord') . '/callback', + 'redirect_uri' => env('APP_URL', 'http://localhost:8000') . '/' . env('DISCORD_PREFIX', 'larascord') . '/callback', /* |-------------------------------------------------------------------------- From 6c23d9586db3bfa8313593f74a577f18f6c966c4 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 19:24:14 +0200 Subject: [PATCH 16/31] Updated namespace. --- src/Http/Controllers/Controller.php | 2 +- src/Http/Controllers/DiscordController.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Http/Controllers/Controller.php b/src/Http/Controllers/Controller.php index 7b918a0..a88cf93 100644 --- a/src/Http/Controllers/Controller.php +++ b/src/Http/Controllers/Controller.php @@ -1,6 +1,6 @@ Date: Tue, 11 Jan 2022 19:26:42 +0200 Subject: [PATCH 17/31] Updated web.php with Larascord's routes. --- src/routes/web.php | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/routes/web.php b/src/routes/web.php index d25495e..c4058ad 100644 --- a/src/routes/web.php +++ b/src/routes/web.php @@ -13,12 +13,28 @@ | */ -Route::get('/', function () { - return view('welcome'); -}); +use App\Http\Controllers\Auth\AuthenticatedSessionController; +use App\Http\Controllers\Auth\ConfirmablePasswordController; +use Jakyeru\Larascord\Http\Controllers\DiscordController; -Route::get('/dashboard', function () { - return view('dashboard'); -})->middleware(['auth'])->name('dashboard'); +Route::redirect('/login', 'https://discord.com/oauth2/authorize?client_id=' . config('larascord.client_id') + . '&redirect_uri=' . config('larascord.redirect_uri') + . '&response_type=code&scope=' . implode('%20', explode('&', config('larascord.scopes'))) + . '&prompt=none') + ->name('login'); -require __DIR__.'/auth.php'; \ No newline at end of file +Route::get('/confirm-password', [ConfirmablePasswordController::class, 'show']) + ->middleware('auth') + ->name('password.confirm'); + +Route::post('/logout', [AuthenticatedSessionController::class, 'destroy']) + ->middleware('auth') + ->name('logout'); + +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'); +}); \ No newline at end of file From 65365e26ca799f9525717670d0798db34be8c000 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 19:31:37 +0200 Subject: [PATCH 18/31] Replaced string|null with ?string in InstallCommand.php. --- src/Console/Commands/InstallCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index 3e7fc3e..f0d22f3 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -24,9 +24,9 @@ class InstallCommand extends Command */ protected $description = 'Use this command to install Larascord.'; - private string|null $clientId; - private string|null $clientSecret; - private string|null $redirectUri; + private ?string $clientId; + private ?string $clientSecret; + private ?string $redirectUri; /** * Execute the console command. From 1bce03bb73011663af25c8af6f9a881c0afbb28a Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 19:34:24 +0200 Subject: [PATCH 19/31] Changed redirectUri to prefix in InstallCommand.php. --- src/Console/Commands/InstallCommand.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index f0d22f3..6a4a1a0 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -26,7 +26,7 @@ class InstallCommand extends Command private ?string $clientId; private ?string $clientSecret; - private ?string $redirectUri; + private ?string $prefix; /** * Execute the console command. @@ -38,7 +38,7 @@ public function handle() // Getting the user's input $this->clientId = $this->ask('What is your Discord application\'s client id?'); $this->clientSecret = $this->ask('What is your Discord application\'s client secret?'); - $this->redirectUri = $this->ask('What is your Discord application\'s redirect uri?', 'http://localhost:8000/larascord/callback'); + $this->prefix = $this->ask('What route prefix should Larascord use?', 'larascord'); // Validating the user's input try {$this->validateInput();} catch (\Exception $e) {$this->error($e->getMessage()); return;} @@ -104,13 +104,13 @@ protected function validateInput() $rules = [ 'clientId' => ['required', 'numeric'], 'clientSecret' => ['required', 'string'], - 'redirectUri' => ['required', 'url'], + 'redirectUri' => ['required', 'string'], ]; $validator = Validator::make([ 'clientId' => $this->clientId, 'clientSecret' => $this->clientSecret, - 'redirectUri' => $this->redirectUri, + 'prefix' => $this->prefix, ], $rules); $validator->validate(); @@ -136,7 +136,7 @@ protected function appendToEnvFile() (new Filesystem())->append('.env','DISCORD_GRANT_TYPE=authorization_code'); (new Filesystem())->append('.env',PHP_EOL); - (new Filesystem())->append('.env','DISCORD_REDIRECT_URI='.$this->redirectUri); + (new Filesystem())->append('.env','DISCORD_PREFIX='.$this->prefix); (new Filesystem())->append('.env',PHP_EOL); (new Filesystem())->append('.env','DISCORD_SCOPE=identify&email'); From fb249e4e7f80349d19dc5730b619f7c879d36a5b Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 19:37:45 +0200 Subject: [PATCH 20/31] Renamed DISCORD_* to LARASCORD_*. --- src/Console/Commands/InstallCommand.php | 10 +++++----- src/Http/Controllers/DiscordController.php | 10 +++++----- src/config/config.php | 10 +++++----- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index 6a4a1a0..3c5b7ff 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -127,19 +127,19 @@ protected function appendToEnvFile() (new Filesystem())->append('.env',PHP_EOL); (new Filesystem())->append('.env',PHP_EOL); - (new Filesystem())->append('.env','DISCORD_CLIENT_ID='.$this->clientId); + (new Filesystem())->append('.env','LARASCORD_CLIENT_ID='.$this->clientId); (new Filesystem())->append('.env',PHP_EOL); - (new Filesystem())->append('.env','DISCORD_CLIENT_SECRET='.$this->clientSecret); + (new Filesystem())->append('.env','LARASCORD_CLIENT_SECRET='.$this->clientSecret); (new Filesystem())->append('.env',PHP_EOL); - (new Filesystem())->append('.env','DISCORD_GRANT_TYPE=authorization_code'); + (new Filesystem())->append('.env','LARASCORD_GRANT_TYPE=authorization_code'); (new Filesystem())->append('.env',PHP_EOL); - (new Filesystem())->append('.env','DISCORD_PREFIX='.$this->prefix); + (new Filesystem())->append('.env','LARASCORD_PREFIX='.$this->prefix); (new Filesystem())->append('.env',PHP_EOL); - (new Filesystem())->append('.env','DISCORD_SCOPE=identify&email'); + (new Filesystem())->append('.env','LARASCORD_SCOPE=identify&email'); } /** diff --git a/src/Http/Controllers/DiscordController.php b/src/Http/Controllers/DiscordController.php index fe3c51c..37f5751 100644 --- a/src/Http/Controllers/DiscordController.php +++ b/src/Http/Controllers/DiscordController.php @@ -28,11 +28,11 @@ class DiscordController extends Controller */ public function __construct() { - $this->tokenData['client_id'] = env('DISCORD_CLIENT_ID'); - $this->tokenData['client_secret'] = env('DISCORD_CLIENT_SECRET'); - $this->tokenData['grant_type'] = env('DISCORD_GRANT_TYPE'); - $this->tokenData['redirect_uri'] = env('DISCORD_REDIRECT_URI'); - $this->tokenData['scope'] = env('DISCORD_SCOPE'); + $this->tokenData['client_id'] = config('larascord.client_id'); + $this->tokenData['client_secret'] = config('larascord.client_secret'); + $this->tokenData['grant_type'] = config('larascord.grant_type'); + $this->tokenData['redirect_uri'] = config('larascord.redirect_uri'); + $this->tokenData['scope'] = config('larascord.scopes'); } /** diff --git a/src/config/config.php b/src/config/config.php index e1e6f71..02a0dbc 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -11,7 +11,7 @@ | */ - 'client_id' => env('DISCORD_CLIENT_ID', null), + 'client_id' => env('LARASCORD_CLIENT_ID', null), /* |-------------------------------------------------------------------------- @@ -22,7 +22,7 @@ | */ - 'client_secret' => env('DISCORD_CLIENT_SECRET', null), + 'client_secret' => env('LARASCORD_CLIENT_SECRET', null), /* |-------------------------------------------------------------------------- @@ -33,7 +33,7 @@ | */ - 'grant_type' => env('DISCORD_GRANT_TYPE', 'authorization_code'), + 'grant_type' => env('LARASCORD_GRANT_TYPE', 'authorization_code'), /* |-------------------------------------------------------------------------- @@ -44,7 +44,7 @@ | */ - 'redirect_uri' => env('APP_URL', 'http://localhost:8000') . '/' . env('DISCORD_PREFIX', 'larascord') . '/callback', + 'redirect_uri' => env('APP_URL', 'http://localhost:8000') . '/' . env('LARASCORD_PREFIX', 'larascord') . '/callback', /* |-------------------------------------------------------------------------- @@ -67,6 +67,6 @@ | */ - 'prefix' => env('DISCORD_PREFIX', 'larascord'), + 'prefix' => env('LARASCORD_PREFIX', 'larascord'), ]; \ No newline at end of file From 5d4e9e58434ac21fa5a996ba17333fe5e5e52c16 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 19:51:25 +0200 Subject: [PATCH 21/31] Updated README.md. --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index af0c33c..134765e 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,10 @@ php artisan larascord:install Your application should now be able to authenticate users using Discord. ## Configuration -You can publish Larascord's configuration using the following command: +You can publish Larascord's configuration using the following commands: +```shell +php artisan larascord:publish +``` ```shell php artisan vendor:publish --provider="Jakyeru\Larascord\LarascordServiceProvider" --tag="config" ``` From 2aee550467785cf5a789f34536b754f3b7cc24ac Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 19:54:51 +0200 Subject: [PATCH 22/31] Replace Laravel Breeze's default routes. --- src/Console/Commands/InstallCommand.php | 54 +++++++++++++------------ src/LarascordServiceProvider.php | 2 +- src/routes/larascord.php | 40 ++++++++++++++++++ src/routes/web.php | 26 +----------- 4 files changed, 71 insertions(+), 51 deletions(-) create mode 100644 src/routes/larascord.php diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index 3c5b7ff..bd1e464 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -56,15 +56,12 @@ public function handle() // Create the model files $this->createModelFiles(); - // Create the controller file - $this->createControllerFiles(); - - // Create the route files - $this->createRouteFiles(); - // Create the view files $this->createViewFiles(); + // Remove Laravel Breeze routes + $this->replaceBreezeRoutes(); + // Asking the user to build the assets if ($this->confirm('Do you want to build the assets?', true)) { try { @@ -92,6 +89,22 @@ public function handle() $this->comment('php artisan migrate'); } + // Asking the user to publish Larascord's configuration + if ($this->confirm('Do you want to publish the Larascord\'s configuration?', true)) { + try { + $this->call('larascord:publish'); + } catch (\Exception $e) { + $this->error($e->getMessage()); + $this->comment('You can publish the Larascord\'s configuration later by running the command:'); + $this->comment('php artisan larascord:publish'); + } + } else { + $this->comment('You can publish the Larascord\'s configuration later by running the command:'); + $this->comment('php artisan larascord:publish'); + } + + $this->alert('Please make sure you add "' . config('larascord.redirect_uri') . '" to your Discord application\'s redirect urls in the OAuth2 tab.'); + $this->info('Larascord has been successfully installed!'); } @@ -104,7 +117,7 @@ protected function validateInput() $rules = [ 'clientId' => ['required', 'numeric'], 'clientSecret' => ['required', 'string'], - 'redirectUri' => ['required', 'string'], + 'prefix' => ['required', 'string'], ]; $validator = Validator::make([ @@ -165,35 +178,24 @@ public function createModelFiles() } /** - * Create the controller files. + * Create the view files. * * @return void */ - public function createControllerFiles() + public function createViewFiles() { - (new Filesystem())->ensureDirectoryExists(app_path('Http/Controllers')); - (new Filesystem())->copyDirectory(__DIR__ . '/../../Http/Controllers/', app_path('Http/Controllers/')); - } - - /** - * Create the route files. - * - * @return void - */ - public function createRouteFiles() { - (new Filesystem())->ensureDirectoryExists(base_path('routes')); - (new Filesystem())->copyDirectory(__DIR__ . '/../../routes', base_path('routes')); + (new Filesystem())->ensureDirectoryExists(resource_path('views')); + (new Filesystem())->copyDirectory(__DIR__ . '/../../resources/views', resource_path('views')); } /** - * Create the view files. - * + * Removes Laravel Breeze's default routes and replaces them with Larascord's routes. * @return void */ - public function createViewFiles() + public function replaceBreezeRoutes() { - (new Filesystem())->ensureDirectoryExists(resource_path('views')); - (new Filesystem())->copyDirectory(__DIR__ . '/../../resources/views', resource_path('views')); + (new Filesystem())->copy(__DIR__ . '../../routes/web.php', base_path('routes/web.php')); + (new Filesystem())->delete(base_path('routes/auth.php')); } /** diff --git a/src/LarascordServiceProvider.php b/src/LarascordServiceProvider.php index 732886f..34923f5 100644 --- a/src/LarascordServiceProvider.php +++ b/src/LarascordServiceProvider.php @@ -40,6 +40,6 @@ protected function registerConfiguration() protected function registerRoutes() { - $this->loadRoutesFrom(__DIR__.'/routes/web.php'); + $this->loadRoutesFrom(__DIR__.'/routes/larascord.php'); } } \ No newline at end of file diff --git a/src/routes/larascord.php b/src/routes/larascord.php new file mode 100644 index 0000000..c4058ad --- /dev/null +++ b/src/routes/larascord.php @@ -0,0 +1,40 @@ +name('login'); + +Route::get('/confirm-password', [ConfirmablePasswordController::class, 'show']) + ->middleware('auth') + ->name('password.confirm'); + +Route::post('/logout', [AuthenticatedSessionController::class, 'destroy']) + ->middleware('auth') + ->name('logout'); + +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'); +}); \ No newline at end of file diff --git a/src/routes/web.php b/src/routes/web.php index c4058ad..326956d 100644 --- a/src/routes/web.php +++ b/src/routes/web.php @@ -13,28 +13,6 @@ | */ -use App\Http\Controllers\Auth\AuthenticatedSessionController; -use App\Http\Controllers\Auth\ConfirmablePasswordController; -use Jakyeru\Larascord\Http\Controllers\DiscordController; - -Route::redirect('/login', 'https://discord.com/oauth2/authorize?client_id=' . config('larascord.client_id') - . '&redirect_uri=' . config('larascord.redirect_uri') - . '&response_type=code&scope=' . implode('%20', explode('&', config('larascord.scopes'))) - . '&prompt=none') - ->name('login'); - -Route::get('/confirm-password', [ConfirmablePasswordController::class, 'show']) - ->middleware('auth') - ->name('password.confirm'); - -Route::post('/logout', [AuthenticatedSessionController::class, 'destroy']) - ->middleware('auth') - ->name('logout'); - -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'); +Route::get('/', function () { + return view('welcome'); }); \ No newline at end of file From bb5c22c5ae0d4de31bb92e9c2954a833bd57f8b0 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 20:08:06 +0200 Subject: [PATCH 23/31] Automatically publishing the configuration file --- README.md | 7 ++++++- src/Console/Commands/InstallCommand.php | 15 ++------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 134765e..f3e5401 100644 --- a/README.md +++ b/README.md @@ -47,13 +47,18 @@ php artisan larascord:install Your application should now be able to authenticate users using Discord. ## Configuration -You can publish Larascord's configuration using the following commands: +#### You can publish Larascord's configuration using the following commands: ```shell php artisan larascord:publish ``` ```shell php artisan vendor:publish --provider="Jakyeru\Larascord\LarascordServiceProvider" --tag="config" ``` +#### You can also forcefully publish Larascord's configuration, overwriting any existing configuration: +```shell +php artisan larascord:publish --force +``` +_You shouldn't need these commands as the configuration is automatically published when the package is installed._ --- # Larascord Routes diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index bd1e464..146a105 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -89,19 +89,8 @@ public function handle() $this->comment('php artisan migrate'); } - // Asking the user to publish Larascord's configuration - if ($this->confirm('Do you want to publish the Larascord\'s configuration?', true)) { - try { - $this->call('larascord:publish'); - } catch (\Exception $e) { - $this->error($e->getMessage()); - $this->comment('You can publish the Larascord\'s configuration later by running the command:'); - $this->comment('php artisan larascord:publish'); - } - } else { - $this->comment('You can publish the Larascord\'s configuration later by running the command:'); - $this->comment('php artisan larascord:publish'); - } + // Automatically publishing the configuration file + $this->call('larascord:publish'); $this->alert('Please make sure you add "' . config('larascord.redirect_uri') . '" to your Discord application\'s redirect urls in the OAuth2 tab.'); From d4e0288db7e0c80b39fd3c3b0db507b0a47dbafd Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 20:19:39 +0200 Subject: [PATCH 24/31] Fixed replaceBreezeRoutes method paths. --- src/Console/Commands/InstallCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index 146a105..c5f8be6 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -183,7 +183,8 @@ public function createViewFiles() */ public function replaceBreezeRoutes() { - (new Filesystem())->copy(__DIR__ . '../../routes/web.php', base_path('routes/web.php')); + (new Filesystem())->ensureDirectoryExists(resource_path('routes')); + (new Filesystem())->copy(__DIR__ . '/../../routes/web.php', base_path('routes/web.php')); (new Filesystem())->delete(base_path('routes/auth.php')); } From 73ef46a15605de543959910e77e1acab1ca40f6e Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 20:22:32 +0200 Subject: [PATCH 25/31] Fixed 404 on dashboard. --- src/routes/web.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/routes/web.php b/src/routes/web.php index 326956d..07609f7 100644 --- a/src/routes/web.php +++ b/src/routes/web.php @@ -15,4 +15,8 @@ Route::get('/', function () { return view('welcome'); -}); \ No newline at end of file +}); + +Route::get('/dashboard', function () { + return view('dashboard'); +})->middleware(['auth'])->name('dashboard'); \ No newline at end of file From 2d5adbcfe9a0efe971a2ead1e54cd3637e672b5f Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 20:33:26 +0200 Subject: [PATCH 26/31] Composer & npm are now silent/quiet. --- src/Console/Commands/InstallCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index c5f8be6..cdc6ef3 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -44,7 +44,7 @@ public function handle() try {$this->validateInput();} catch (\Exception $e) {$this->error($e->getMessage()); return;} // Installing laravel/breeze - $this->requireComposerPackages('laravel/breeze:^1.4'); + $this->requireComposerPackages('laravel/breeze:^1.4', '-q'); shell_exec('php artisan breeze:install'); // Appending the secrets to the .env file @@ -65,8 +65,8 @@ public function handle() // Asking the user to build the assets if ($this->confirm('Do you want to build the assets?', true)) { try { - shell_exec('npm install'); - shell_exec('npm run dev'); + shell_exec('npm install --silent'); + shell_exec('npm run dev --silent'); } catch (\Exception $e) { $this->error($e->getMessage()); $this->comment('Please execute the "npm install && npm run dev" command to build your assets.'); From 9dc19d0134e0d56c3e28d75c7599a52e08d93490 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 20:34:10 +0200 Subject: [PATCH 27/31] Fixed redirect_uri alert. --- src/Console/Commands/InstallCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index cdc6ef3..6e7ff00 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -92,7 +92,7 @@ public function handle() // Automatically publishing the configuration file $this->call('larascord:publish'); - $this->alert('Please make sure you add "' . config('larascord.redirect_uri') . '" to your Discord application\'s redirect urls in the OAuth2 tab.'); + $this->alert('Please make sure you add "' . env('APP_URL', 'http://localhost:8000') . '/' . env('LARASCORD_PREFIX', 'larascord') . '/callback' . '" to your Discord application\'s redirect urls in the OAuth2 tab.'); $this->info('Larascord has been successfully installed!'); } From 9450904f833c6fc3506319df9cbaca523f24f2bd Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 20:35:05 +0200 Subject: [PATCH 28/31] Added installation message before requireComposerPackages because it might take some time to finish. --- src/Console/Commands/InstallCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index 6e7ff00..bc7ad01 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -44,6 +44,7 @@ public function handle() try {$this->validateInput();} catch (\Exception $e) {$this->error($e->getMessage()); return;} // Installing laravel/breeze + $this->info('Please wait while we install Larascord...'); $this->requireComposerPackages('laravel/breeze:^1.4', '-q'); shell_exec('php artisan breeze:install'); From 1e6e0c1b26d099068480d89f641368029c0ccbaf Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 20:41:38 +0200 Subject: [PATCH 29/31] Added warning message. --- src/Console/Commands/InstallCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index bc7ad01..869179f 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -94,6 +94,7 @@ public function handle() $this->call('larascord:publish'); $this->alert('Please make sure you add "' . env('APP_URL', 'http://localhost:8000') . '/' . env('LARASCORD_PREFIX', 'larascord') . '/callback' . '" to your Discord application\'s redirect urls in the OAuth2 tab.'); + $this->warn('If the domain doesn\'t match your current environment\'s domain you need to set it manually in the .env file. (APP_URL)'); $this->info('Larascord has been successfully installed!'); } From 5d33ae973a5d5cbe0e72a6bde925785a9301cdab Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 20:54:50 +0200 Subject: [PATCH 30/31] Fixed logout issue. --- src/routes/larascord.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/larascord.php b/src/routes/larascord.php index c4058ad..5da0665 100644 --- a/src/routes/larascord.php +++ b/src/routes/larascord.php @@ -24,11 +24,11 @@ ->name('login'); Route::get('/confirm-password', [ConfirmablePasswordController::class, 'show']) - ->middleware('auth') + ->middleware(['web', 'auth']) ->name('password.confirm'); Route::post('/logout', [AuthenticatedSessionController::class, 'destroy']) - ->middleware('auth') + ->middleware(['web', 'auth']) ->name('logout'); Route::group(['prefix' => config('larascord.prefix'), 'middleware' => ['web']], function() { From e2e773612cc146aa33c9067a6ddd2ddc7e0da2e4 Mon Sep 17 00:00:00 2001 From: JakyeRU Date: Tue, 11 Jan 2022 21:12:37 +0200 Subject: [PATCH 31/31] Updated README.md. --- README.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f3e5401..0b549e4 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,7 @@ php artisan larascord:install ``` ### Step 3: Follow Larascord's instructions * You can get your Discord application's `CLIENT ID` and `CLIENT SECRET` from the "OAuth2" tab of your application. - * ![](https://i.imgur.com/YJnM4H5.png) -* The `REDIRECT URI` has to be the **same** as the one you provided in your application's OAuth2 redirect. +![](https://i.imgur.com/YJnM4H5.png) Your application should now be able to authenticate users using Discord. @@ -62,11 +61,19 @@ _You shouldn't need these commands as the configuration is automatically publish --- # Larascord Routes -> 💡 These routes can be found in the `routes/auth.php` file. - | Route Name | URI | Description | Action | Method | | ---------- | ---- | ----------- | ------ | ------ | | `login` | `/login` | Redirects the user to Discord's OAuth2 authorization page. | REDIRECT | `GET` | | `logout` | `/logout` | Invalidates the current session. | `AuthenticatedSessionController@destroy` | `POST` | | `larascord.login` | `/larascord/callback` | Callback route for Discord OAuth2 authentication. | `DiscordController@handle` | `GET` | | `larascord.refresh_token` | `/larascord/refresh-token` | Redirects to the login page. (Used to access secure parts of the application through the middleware `password.confirm`.) | REDIRECT | `GET` | + +# Possible Errors +## `Invalid OAuth2 redirect_uri` +This error occurs when your `redirect_uri` is not listed in your application's OAuth2 redirects. + +If you are sure your `redirect_uri` is correct, make sure that `APP_URL` is correct in `config/app.php`. + +--- + +If you encounter any other error(s), please open an issue on [GitHub](https://github.com/JakyeRU/Larascord/issues/new/choose). \ No newline at end of file