Skip to content

Commit

Permalink
Fixed 500 error when linking discord accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
AVMG20 committed Jul 16, 2021
1 parent 4d0c9de commit cb4ade5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
26 changes: 13 additions & 13 deletions app/Http/Controllers/Auth/SocialiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
Expand All @@ -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!'
Expand Down
16 changes: 14 additions & 2 deletions app/Models/DiscordUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit cb4ade5

Please sign in to comment.