user_menu_dropdown.html 91 lignes · 2726 octets
<!-- User Information -->
<li class="menu-title">
    <div class="flex items-center gap-3 px-2 py-2">
        <!-- Avatar -->
        <div class="avatar placeholder">
            <div class="bg-primary text-primary-content rounded-full w-10 h-10"
                 style="background-color: {{ avatar_color }} !important;">
                <span class="text-sm font-semibold">{{ initials }}</span>
            </div>
        </div>
        
        <!-- User Info -->
        <div class="flex-1 min-w-0">
            <div class="text-sm font-medium text-base-content truncate">{{ username }}</div>
            <div class="text-xs text-base-content/60 truncate">{{ email }}</div>
        </div>
    </div>
    
    <!-- Roles -->
    {% if !roles_text.is_empty() %}
    <div class="px-2 mt-1">
        <span class="badge badge-primary badge-sm">{{ roles_text }}</span>
    </div>
    {% endif %}
</li>

<!-- Menu Items -->
<li>
    <a href="/profile" class="gap-2">
        <span>👤</span>
        <span>{{ user_menu_profile }}</span>
    </a>
</li>

<!-- Divider -->
<li><hr class="my-2"></li>

<!-- Logout -->
<li>
    <button 
        type="button" 
        onclick="handleLogout()" 
        class="w-full text-left text-error hover:bg-error/10 gap-2"
    >
        <span>🚪</span>
        <span>{{ user_menu_logout }}</span>
    </button>
</li>

<script>
function handleLogout() {
    // Clean up remember-me / auto-refresh state
    localStorage.removeItem('refresh_token');
    localStorage.removeItem('remember_me');
    localStorage.removeItem('jwt_expires_in');

    // Send logout request with credentials (cookies)
    fetch('/api/v1/auth/logout', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'text/html,application/json',
            'X-CSRF-Token': '{{ csrf_token }}',
        },
        credentials: 'include'
    })
    .then(response => {
        if (response.ok) {
            // Check if response is a redirect
            if (response.redirected || response.status === 200) {
                // Redirect to login page
                window.location.href = '/login';
            } else {
                return response.json().then(data => {
                    // JSON response from API
                    window.location.href = '/login';
                });
            }
        } else {
            // Error - still redirect to login
            console.error('Logout error:', response.status);
            window.location.href = '/login';
        }
    })
    .catch(error => {
        console.error('Logout error:', error);
        // Even on error, redirect to login
        window.location.href = '/login';
    });
}
</script>