From cb4ade5a5d6f8712e8fb4a9cd62e370c59e2842e Mon Sep 17 00:00:00 2001 From: AVMG20 Date: Fri, 16 Jul 2021 09:21:30 +0200 Subject: [PATCH] Fixed 500 error when linking discord accounts --- .../Controllers/Auth/SocialiteController.php | 26 +++++++++---------- app/Models/DiscordUser.php | 16 ++++++++++-- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Auth/SocialiteController.php b/app/Http/Controllers/Auth/SocialiteController.php index 757900c60..c70a21c55 100644 --- a/app/Http/Controllers/Auth/SocialiteController.php +++ b/app/Http/Controllers/Auth/SocialiteController.php @@ -5,6 +5,7 @@ use App\Http\Controllers\Controller; use App\Models\Configuration; use App\Models\DiscordUser; +use App\Models\Voucher; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Http; use Laravel\Socialite\Facades\Socialite; @@ -28,11 +29,22 @@ public function callback() $discord = Socialite::driver('discord')->user(); $discordUser = DiscordUser::find($discord->id); - $botToken = env('DISCORD_BOT_TOKEN'); $guildId = env('DISCORD_GUILD_ID'); $roleId = env('DISCORD_ROLE_ID'); + //save / update discord_users + if (is_null($discordUser)) { + //create discord user in db + DiscordUser::create(array_merge($discord->user, ['user_id' => Auth::user()->id])); + //update user + Auth::user()->increment('credits', Configuration::getValueByKey('CREDITS_REWARD_AFTER_VERIFY_DISCORD')); + Auth::user()->increment('server_limit', Configuration::getValueByKey('SERVER_LIMIT_REWARD_AFTER_VERIFY_DISCORD')); + Auth::user()->update(['discord_verified_at' => now()]); + } else { + $discordUser->update($discord->user); + } + //force user into discord server //TODO Add event on failure, to notify ppl involved if (!empty($guildId) && !empty($botToken)) { @@ -56,18 +68,6 @@ public function callback() } } - - if (is_null($discordUser)) { - //create discord user in db - DiscordUser::create(array_merge($discord->user, ['user_id' => Auth::user()->id])); - //update user - Auth::user()->increment('credits', Configuration::getValueByKey('CREDITS_REWARD_AFTER_VERIFY_DISCORD')); - Auth::user()->increment('server_limit', Configuration::getValueByKey('SERVER_LIMIT_REWARD_AFTER_VERIFY_DISCORD')); - Auth::user()->update(['discord_verified_at' => now()]); - } else { - $discordUser->update($discord->user); - } - return redirect()->route('profile.index')->with( 'success', 'Discord account linked!' diff --git a/app/Models/DiscordUser.php b/app/Models/DiscordUser.php index 4c08ae4e3..ab53dba7b 100644 --- a/app/Models/DiscordUser.php +++ b/app/Models/DiscordUser.php @@ -5,13 +5,25 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; -use Spatie\Activitylog\Traits\LogsActivity; class DiscordUser extends Model { use HasFactory; - protected $guarded = []; + protected $fillable = [ + "id", + "user_id", + "username", + "avatar", + "discriminator", + "public_flags", + "flags", + "locale", + "mfa_enabled", + "premium_type", + "email", + "verified", + ]; public $incrementing = false;