// src/store/authStore.js
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useAuthStore = defineStore('auth', () => {
    const token = ref(localStorage.getItem('token') || null)

    // 计算属性：是否已登录
    const isLoggedIn = computed(() => !!token.value)

    // 登录方法
    function login(newToken) {
        token.value = newToken
        localStorage.setItem('token', newToken)
    }

    // 退出登录方法
    function logout() {
        token.value = null
        localStorage.removeItem('token')
    }

    return { token, isLoggedIn, login, logout }
})
