-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
4da81de
commit 8e0e8f7
Showing
10 changed files
with
380 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.