Skip to content

Commit

Permalink
Merge pull request #132 from ControlPanel-gg/development
Browse files Browse the repository at this point in the history
Fixed 500 error when linking discord accounts
  • Loading branch information
AVMG20 authored Jul 16, 2021
2 parents c7e6684 + cb4ade5 commit 88b3120
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 88b3120

Please sign in to comment.