130 lines
3.5 KiB
TypeScript
Executable File
130 lines
3.5 KiB
TypeScript
Executable File
import { ref, computed } from 'vue'
|
|
import { defineStore } from 'pinia'
|
|
import { dispatchNotification } from '@/components/Notification'
|
|
import { readData, removeData, writeData, writeDataJson } from '@/utils/storage'
|
|
import { authenticateUser } from '@/utils/api/api.rest'
|
|
|
|
export const useAuthStore = defineStore('auth', () => {
|
|
const token = ref(readData('token') || '')
|
|
const username = ref('')
|
|
const password = ref('')
|
|
const isLoading = ref(false)
|
|
const isLoggedIn = computed(() => token.value !== '')
|
|
const filterPresets: any = ref({})
|
|
|
|
const login = async () => {
|
|
if (username.value == '' || password.value == '') {
|
|
dispatchNotification({
|
|
title: 'Perhatian',
|
|
content: 'Username atau password tidak boleh kosong',
|
|
type: 'warning'
|
|
})
|
|
} else {
|
|
if (username.value == 'demo' && password.value == 'demo') {
|
|
isLoading.value = true
|
|
setTimeout(() => {
|
|
isLoading.value = false
|
|
if (username.value == 'demo' && password.value == 'demo') {
|
|
writeData('token', 'secret-token')
|
|
|
|
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'
|
|
})
|
|
}
|
|
}, 3000)
|
|
} else {
|
|
isLoading.value = true
|
|
|
|
try {
|
|
const response = await authenticateUser(username.value, password.value)
|
|
|
|
if (response.status) {
|
|
const data = response.user
|
|
|
|
writeData('token', 'secret-token')
|
|
writeData('user_name', data.username)
|
|
writeData('user_access', 'Petugas')
|
|
writeData('user_uid', data.namaUid)
|
|
|
|
handlePresets(data, filterPresets)
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const handlePresets = (data: any, filterPresets: any) => {
|
|
const keys = ['regional', 'uid', 'up3', 'posko', 'ulp']
|
|
let isChanged = false
|
|
|
|
for (const key of keys) {
|
|
const idKey = `id${key.charAt(0).toUpperCase() + key.slice(1)}`
|
|
const nameKey = `nama${key.charAt(0).toUpperCase() + key.slice(1)}`
|
|
|
|
if (data[nameKey] && data[idKey]) {
|
|
filterPresets.value[key] = {
|
|
id: data[idKey],
|
|
name: data[nameKey]
|
|
}
|
|
isChanged = true
|
|
}
|
|
}
|
|
|
|
console.log(filterPresets.value)
|
|
|
|
if (isChanged) {
|
|
writeDataJson('filterPresets', filterPresets.value)
|
|
}
|
|
}
|
|
|
|
const logout = () => {
|
|
removeData('token')
|
|
removeData('filterPresets')
|
|
removeData('user_name')
|
|
removeData('user_access')
|
|
removeData('user_uid')
|
|
}
|
|
|
|
return {
|
|
token,
|
|
username,
|
|
password,
|
|
isLoggedIn,
|
|
isLoading,
|
|
login,
|
|
logout
|
|
}
|
|
})
|