diff --git a/src/stores/auth.ts b/src/stores/auth.ts index 1f4b5c6..57d5b94 100755 --- a/src/stores/auth.ts +++ b/src/stores/auth.ts @@ -2,20 +2,17 @@ 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' +import { authenticateUser } from '@/utils/api/api.rest' 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 = () => { + const login = async () => { if (username.value == '' || password.value == '') { dispatchNotification({ title: 'Perhatian', @@ -23,19 +20,57 @@ export const useAuthStore = defineStore('auth', () => { 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 + + // try { + // const response = await authenticateUser(username.value, password.value) + + // if (response.success) { + // writeData('token', response.token) + + // filterPresets.value = { + // regional + // uid: { + // id: 2, + // name: 'DISTRIBUSI JAKARTA RAYA' + // }, + // up3: { + // id: 4, + // name: 'UP3 MENTENG' + // }, + // posko: { + // id: 541101, + // name: 'POSKO MENTENG' + // }, + // ulp, + // } + + // if (Object.keys(filterPresets.value).length > 0) { + // writeDataJson('filterPresets', filterPresets.value) + // } + + // dispatchNotification({ + // title: 'Berhasil', + // content: 'Login berhasil, selamat datang kembali!', + // type: 'success' + // }) + + // window.location.reload() + // } else { + // dispatchNotification({ + // title: 'Login Gagal', + // content: 'Username atau password salah', + // type: 'error' + // }) // } + // } catch (error) { + // dispatchNotification({ + // title: 'Login Gagal', + // content: 'Terjadi kesalahan saat melakukan login', + // type: 'error' + // }) + // } finally { + // isLoading.value = false // } isLoading.value = true diff --git a/src/utils/api/api.rest.ts b/src/utils/api/api.rest.ts index 20977cf..d9cf5d7 100755 --- a/src/utils/api/api.rest.ts +++ b/src/utils/api/api.rest.ts @@ -18,6 +18,16 @@ const getUp3 = async (uid: number) => await instance.get('/up3?uid=' + uid) const getUlp = async (up3: number) => await instance.get('/ulp?up3=' + up3) const getPosko = async (uppp: number) => await instance.get('/posko?up3=' + uppp) const getVersion = async () => await ax.get('/version.json') + +const authenticateUser = async (username: string, password: string) => { + const res = await instance.post('/auth', { + username, + password + }) + + return res.data +} + export { getUid, getUp3, @@ -27,5 +37,6 @@ export { getJenisTransaksi, getUidRegional, getRegional, - getVersion + getVersion, + authenticateUser }