본문 바로가기
카테고리 없음

vue

by 두잇건강 2025. 8. 14.
반응형


대쉬보드에서 로그아웃하는 기능... 사용자 아이콘 누르고 Sign Out 누르기..

<template>
  <div class="dashboard-container">
    <header class="dashboard-header">
      <h1>대시보드</h1>
      <div class="header-actions">
        <button @click="handleLogout" class="logout-btn" :disabled="loggingOut">
          {{ loggingOut ? '로그아웃 중...' : '로그아웃' }}
        </button>
      </div>
    </header>
    <main class="dashboard-content">
      <div class="welcome-message">
        <h2>환영합니다!</h2>
        <p v-if="userInfo">{{ userInfo }}님이 로그인했습니다.</p>
      </div>
      <div class="dashboard-actions">
        <button @click="fetchUserData" class="action-btn" :disabled="loading">
          {{ loading ? '로딩 중...' : '사용자 정보 가져오기' }}
        </button>
      </div>
      <div v-if="userData" class="user-data">
        <h3>사용자 데이터:</h3>
        <p>{{ userData }}</p>
      </div>
      
      <!-- 로그아웃 성공/실패 메시지 -->
      <div v-if="logoutMessage" class="message" :class="logoutMessageType">
        {{ logoutMessage }}
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useAuth } from '@/composables/useAuth'
import axios from 'axios'

const { user, logout } = useAuth()

// 반응형 데이터
const userInfo = ref('')
const userData = ref('')
const loading = ref(false)
const loggingOut = ref(false)
const logoutMessage = ref('')
const logoutMessageType = ref('success')

// 컴포넌트 마운트 시 실행
onMounted(() => {
  const token = localStorage.getItem('jwt-token')
  if (token) {
    userInfo.value = '사용자'
  }
})

// 사용자 데이터 가져오기
const fetchUserData = async () => {
  loading.value = true
  try {
    const response = await axios.get('/api/user')
    userData.value = response.data
  } catch (error) {
    console.error('사용자 데이터 가져오기 실패:', error)
    userData.value = '데이터를 가져오는 중 오류가 발생했습니다.'
  } finally {
    loading.value = false
  }
}

// 로그아웃 처리
const handleLogout = async () => {
  loggingOut.value = true
  logoutMessage.value = ''
  
  try {
    const result = await logout()
    
    if (result.success) {
      logoutMessage.value = result.message || '로그아웃이 완료되었습니다.'
      logoutMessageType.value = 'success'
      // 2초 후 로그인 페이지로 이동 (이미 useAuth에서 처리됨)
    } else {
      logoutMessage.value = result.error || '로그아웃 처리 중 오류가 발생했습니다.'
      logoutMessageType.value = 'error'
    }
  } catch (error) {
    console.error('로그아웃 실패:', error)
    logoutMessage.value = '로그아웃 처리 중 오류가 발생했습니다.'
    logoutMessageType.value = 'error'
  } finally {
    loggingOut.value = false
    
    // 3초 후 메시지 제거
    setTimeout(() => {
      logoutMessage.value = ''
    }, 3000)
  }
}
</script>

<style scoped>
.dashboard-container {
  min-height: 100vh;
  background-color: #f8f9fa;
}

.dashboard-header {
  background: white;
  padding: 1rem 2rem;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dashboard-header h1 {
  margin: 0;
  color: #333;
}

.header-actions {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.logout-btn {
  padding: 0.5rem 1rem;
  background-color: #dc3545;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.logout-btn:hover:not(:disabled) {
  background-color: #c82333;
}

.logout-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.dashboard-content {
  padding: 2rem;
  max-width: 800px;
  margin: 0 auto;
}

.welcome-message {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 2rem;
  text-align: center;
}

.dashboard-actions {
  margin-bottom: 2rem;
}

.action-btn {
  padding: 0.75rem 1.5rem;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.3s;
}

.action-btn:hover:not(:disabled) {
  background-color: #0056b3;
}

.action-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.user-data {
  background: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.message {
  margin-top: 1rem;
  padding: 0.75rem;
  border-radius: 4px;
  text-align: center;
  font-weight: bold;
}

.message.success {
  background-color: #d4edda;
  color: #155724;
  border: 1px solid #c3e6cb;
}

.message.error {
  background-color: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
}
</style>
반응형