Skip to content

Commit

Permalink
Add Discord OAuth (#1714)
Browse files Browse the repository at this point in the history
* Add discord OAuth

* Remove the ability to edit discord_id

* Update OAuthController

* Apply fixes from StyleCI

* Update discord redirect url

* Retrieve the discord_private_channel_id in OAuthController

* Apply fixes from StyleCI

* Move discord token to config

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Nabeel S <nabeelio@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 14, 2023
1 parent 4da81de commit 8e0e8f7
Show file tree
Hide file tree
Showing 10 changed files with 380 additions and 28 deletions.
68 changes: 68 additions & 0 deletions app/Http/Controllers/Auth/OAuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Contracts\Controller;
use GuzzleHttp\Client;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Laravel\Socialite\Facades\Socialite;

class OAuthController extends Controller
{
public function redirectToDiscordProvider(): RedirectResponse
{
return Socialite::driver('discord')->scopes(['identify'])->redirect();
}

public function handleDiscordProviderCallback(): RedirectResponse
{
$user = Socialite::driver('discord')->user();

if ($user->getId()) {
// Let's retrieve the private_channel_id
if (!is_null(config('services.discord.token'))) {
try {
$httpClient = new Client();
$response = $httpClient->request('POST', 'https://discord.com/api/users/@me/channels', [
'headers' => [
'Authorization' => 'Bot '.config('services.discord.token'),
],
'json' => [
'recipient_id' => $user->getId(),
],
]);

$privateChannel = json_decode($response->getBody()->getContents(), true)['id'];
} catch (\Exception $e) {
Log::error('Discord OAuth Error: '.$e->getMessage());
$privateChannel = null;
}
}

Auth::user()?->update([
'discord_id' => $user->getId(),
'discord_private_channel_id' => $privateChannel ?? null,
]);

flash()->success('Discord account linked!');
} else {
flash()->error('Unable to link Discord account!');
}

return redirect()->route('frontend.profile.index');
}

public function logoutDiscordProvider(): RedirectResponse
{
Auth::user()?->update([
'discord_id' => null,
'discord_private_channel_id' => null,
]);

flash()->success('Discord account unlinked!');

return redirect()->route('frontend.profile.index');
}
}
10 changes: 0 additions & 10 deletions app/Http/Controllers/Frontend/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,6 @@ public function update(Request $request): RedirectResponse
$req_data['password'] = Hash::make($req_data['password']);
}

// Find out the user's private channel id
/*
// TODO: Uncomment when Discord API functionality is enabled
if ($request->filled('discord_id')) {
$discord_id = $request->post('discord_id');
if ($discord_id !== $user->discord_id) {
$req_data['discord_private_channel_id'] = Discord::getPrivateChannelId($discord_id);
}
}*/

if ($request->hasFile('avatar')) {
if ($user->avatar !== null) {
Storage::delete($user->avatar);
Expand Down
6 changes: 6 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class EventServiceProvider extends ServiceProvider
],

ProfileUpdated::class => [],

// For discord OAuth
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
\SocialiteProviders\Discord\DiscordExtendSocialite::class.'@handle',
],

];

protected $subscribe = [
Expand Down
11 changes: 11 additions & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ private function mapWebRoutes()
Route::get('lang/{lang}', 'LanguageController@switchLang')->name('lang.switch');
});

Route::group([
'namespace' => 'Auth',
'prefix' => 'auth',
'as' => 'auth.',
'middleware' => 'auth',
], function () {
Route::get('discord/redirect', 'OAuthController@redirectToDiscordProvider')->name('discord.redirect');
Route::get('discord/callback', 'OAuthController@handleDiscordProviderCallback')->name('discord.callback');
Route::get('discord/logout', 'OAuthController@logoutDiscordProvider')->name('discord.logout');
});

Route::get('/logout', 'Auth\LoginController@logout')->name('auth.logout');
Auth::routes(['verify' => true]);
});
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@
"spatie/laravel-ignition": "^2.0",
"kyslik/column-sortable": "^6.5",
"jlorente/laravel-data-migrations": "^2.0",
"spatie/laravel-backup": "*"
"spatie/laravel-backup": "*",
"laravel/socialite": "^5.11",
"socialiteproviders/discord": "^4.2"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.8.1",
Expand Down
Loading

0 comments on commit 8e0e8f7

Please sign in to comment.