localhost-front/src/stores/useAuthStore.js
2025-02-04 14:22:49 +09:00

51 lines
1.2 KiB
JavaScript

/*
작성자 : 박지윤
작성일 : 2025-02-04
수정자 :
수정일 :
설명 : 로그인 상태, 로그아웃
*/
import { ref, computed } from 'vue';
import { defineStore } from 'pinia';
import $api from "@api";
export const useAuthStore = defineStore('auth', () => {
const user = ref(null);
// 로그인 여부 확인
const isAuthenticated = computed(() => user.value !== null);
// 로그인 상태 확인
const checkAuthStatus = async () => {
const response = await $api.get('user/isLogin');
if (response.data.status === "OK" && response.data.data) {
user.value = response.data.data;
} else {
user.value = null;
}
};
// 로그아웃
const logout = async () => {
const response = await $api.get('user/logout');
// 로그아웃 성공 시 사용자 상태 초기화
if (response.data.status === "OK") {
user.value = null;
return true;
} else {
console.error("로그아웃 실패:", response.data.data);
return false;
}
};
return {
user,
isAuthenticated,
checkAuthStatus,
logout
};
});