Previously the stored Discord identity only updated when a member manually unlinked and relinked their account, so a changed Discord avatar never showed up. The OAuth exchange now also captures the refresh_token; discord_sync_avatar_if_stale() uses it to silently pull the current username/avatar (throttled to once per hour) and is called from profile.php, so opening "Mein Profil" keeps it current without any re-consent flow. Adds discord_refresh_token and discord_avatar_synced_at columns. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
37 lines
1.4 KiB
PHP
37 lines
1.4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/bootstrap.php';
|
|
require_once __DIR__ . '/includes/discord.php';
|
|
require_login();
|
|
|
|
$state = (string) ($_GET['state'] ?? '');
|
|
$code = (string) ($_GET['code'] ?? '');
|
|
$expectedState = (string) ($_SESSION['discord_oauth_state'] ?? '');
|
|
unset($_SESSION['discord_oauth_state']);
|
|
|
|
if (!empty($_GET['error'])) {
|
|
set_flash('error', 'Discord-Verknüpfung wurde abgebrochen.');
|
|
redirect('/dashboard.php');
|
|
}
|
|
|
|
if ($code === '' || $state === '' || $expectedState === '' || !hash_equals($expectedState, $state)) {
|
|
set_flash('error', 'Ungültige Antwort von Discord. Bitte erneut versuchen.');
|
|
redirect('/dashboard.php');
|
|
}
|
|
|
|
try {
|
|
$discordUser = discord_exchange_code($code);
|
|
$user = current_user();
|
|
|
|
$stmt = db()->prepare(
|
|
'UPDATE users SET discord_id = ?, discord_username = ?, discord_avatar = ?, discord_refresh_token = ?, discord_avatar_synced_at = NOW() WHERE id = ?'
|
|
);
|
|
$stmt->execute([$discordUser['id'], $discordUser['username'], $discordUser['avatar'], $discordUser['refresh_token'], $user['id']]);
|
|
|
|
set_flash('success', 'Discord-Account "' . $discordUser['username'] . '" wurde verknüpft.');
|
|
} catch (PDOException $e) {
|
|
set_flash('error', 'Dieser Discord-Account ist bereits mit einem anderen Benutzer verknüpft.');
|
|
} catch (Throwable $e) {
|
|
set_flash('error', 'Discord-Verknüpfung fehlgeschlagen: ' . $e->getMessage());
|
|
}
|
|
|
|
redirect('/dashboard.php');
|