3 namespace App\Http\Controllers;
6 use App\Providers\RouteServiceProvider;
7 use Illuminate\Http\Request;
8 use Illuminate\Support\Facades\Auth;
9 use Illuminate\Support\Facades\Http;
11 class DiscordController extends \Jakyeru\Larascord\Http\Controllers\DiscordController
15 * Handles the Discord OAuth2 login.
17 * @param Request $request
18 * @return \Illuminate\Http\JsonResponse
20 public function handle(Request $request)//: \Illuminate\Http\JsonResponse
22 // Checking if the authorization code is present in the request.
23 if ($request->missing('code')) {
24 if (env('APP_DEBUG')) {
25 return response()->json([
26 'larascord_message' => config('larascord.error_messages.missing_code', 'The authorization code is missing.'),
30 return redirect('/')->with('error', config('larascord.error_messages.missing_code', 'The authorization code is missing.'));
34 // Getting the access_token from the Discord API.
36 $accessToken = $this->getDiscordAccessToken($request->get('code'));
37 } catch (\Exception $e) {
38 if (env('APP_DEBUG')) {
39 return response()->json([
40 'larascord_message' => config('larascord.error_messages.invalid_code', 'The authorization code is invalid.'),
41 'message' => $e->getMessage(),
42 'code' => $e->getCode()
45 return redirect('/')->with('error', config('larascord.error_messages.invalid_code', 'The authorization code is invalid.'));
49 // Using the access_token to get the user's Discord ID.
51 $user = $this->getDiscordUser($accessToken->access_token);
52 } catch (\Exception $e) {
53 if (env('APP_DEBUG')) {
54 return response()->json([
55 'larascord_message' => config('larascord.error_messages.authorization_failed', 'The authorization failed.'),
56 'message' => $e->getMessage(),
57 'code' => $e->getCode()
60 return redirect('/')->with('error', config('larascord.error_messages.authorization_failed', 'The authorization failed.'));
64 // Making sure the current logged-in user's ID is matching the ID retrieved from the Discord API.
65 if (Auth::check() && (Auth::id() !== $user->id)) {
67 return redirect('/')->with('error', config('larascord.error_messages.invalid_user', 'The user ID doesn\'t match the logged-in user.'));
70 // Confirming the session in case the user was redirected from the password.confirm middleware.
72 $request->session()->put('auth.password_confirmed_at', time());
75 // Trying to create or update the user in the database.
77 $user = $this->createOrUpdateUser($user, $accessToken->refresh_token);
78 } catch (\Exception $e) {
79 if (env('APP_DEBUG')) {
80 return response()->json([
81 'larascord_message' => config('larascord.error_messages.database_error', 'There was an error while trying to create or update the user.'),
82 'message' => $e->getMessage(),
83 'code' => $e->getCode()
86 return redirect('/')->with('error', config('larascord.error_messages.database_error', 'There was an error while trying to create or update the user.'));
90 // Authenticating the user if the user is not logged in.
95 // Redirecting the user to the intended page or to the home page.
96 return redirect()->intended(RouteServiceProvider::HOME);
100 * Handles the Discord OAuth2 callback.
102 * @param string $code
104 * @throws \Illuminate\Http\Client\RequestException
106 private function getDiscordAccessToken(string $code): object
108 $this->tokenData['code'] = $code;
110 $response = Http::asForm()->post($this->tokenURL, $this->tokenData);
114 return json_decode($response->body());
118 * Handles the Discord OAuth2 login.
120 * @param string $access_token
122 * @throws \Illuminate\Http\Client\RequestException
124 private function getDiscordUser(string $access_token): object
126 $response = Http::withToken($access_token)->get($this->apiURLBase);
130 return json_decode($response->body());
134 * Handles the creation or update of the user.
136 * @param object $user
137 * @param string $refresh_token
141 private function createOrUpdateUser(object $user, string $refresh_token): User
143 return User::updateOrCreate(
148 'username' => $user->username,
149 'discriminator' => $user->discriminator,
150 'email' => isset($user->email) ? $user->email : NULL,
151 'avatar' => $user->avatar ?: NULL,
152 'verified' => isset($user->verified) ? $user->verified : 0,
153 'locale' => $user->locale,
154 'mfa_enabled' => $user->mfa_enabled,
155 'refresh_token' => $refresh_token