30 lines
636 B
JavaScript
30 lines
636 B
JavaScript
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
|
|
};
|
|
});
|