import { ref, computed } from 'vue' import { defineStore } from 'pinia' import { dispatchNotification } from '@/components/Notification' import { readData, removeData, writeData, writeDataJson } from '@/utils/storage' import router from '@/router' export const useAuthStore = defineStore('auth', () => { // token from localStorage const token = ref(readData('token') || '') // create a shared state const username = ref('') const password = ref('') const isLoading = ref(false) const isLoggedIn = computed(() => token.value !== '') const filterPresets: any = ref({}) // define your actions const login = () => { if (username.value == '' || password.value == '') { dispatchNotification({ title: 'Perhatian', content: 'Username atau password tidak boleh kosong', type: 'warning' }) } else { // filterPresets.value = { // uid: { // id: 2, // name: 'DISTRIBUSI JAKARTA RAYA' // }, // up3: { // id: 4, // name: 'UP3 MENTENG' // }, // posko: { // id: 541101, // name: 'POSKO MENTENG' // } // } isLoading.value = true setTimeout(() => { isLoading.value = false if (username.value == 'demo' && password.value == 'demo') { // store token in localStorage writeData('token', 'secret-token') if (Object.keys(filterPresets.value).length > 0) { writeDataJson('filterPresets', filterPresets.value) } dispatchNotification({ title: 'Berhasil', content: 'Login berhasil, selamat datang kembali!', type: 'success' }) // redirect to home page after login window.location.reload() } else { dispatchNotification({ title: 'Login Gagal', content: 'Username atau password salah', type: 'error' }) } }, 3000) } } const logout = () => { removeData('token') removeData('filterPresets') } return { token, username, password, isLoggedIn, isLoading, login, logout } })