import { defineStore } from 'pinia';
import { computed, ref } from 'vue';

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

    const isAuthenticated = computed(() => !!token.value);

    function setToken(newToken) {
        token.value = newToken;
        localStorage.setItem('__token', newToken);
    }


    function logout() {
        token.value = null;
        user.value = null;
        localStorage.removeItem('__token');
    }

    return {
        token,
        user,
        isAuthenticated,
        logout,
        setToken
    };
});
