add file
This commit is contained in:
@ -1,11 +1,12 @@
|
||||
import { ref, computed } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { dispatchNotification } from '@/components/Notification'
|
||||
import { readData, removeData, writeData } from './storage'
|
||||
import router from '@/router'
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
// token from localStorage
|
||||
const token = ref(localStorage.getItem('token') || '')
|
||||
const token = ref(readData('token') || '')
|
||||
// create a shared state
|
||||
const username = ref('')
|
||||
const password = ref('')
|
||||
@ -13,7 +14,7 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
const isLoggedIn = computed(() => token.value !== '')
|
||||
|
||||
// define your actions
|
||||
function login() {
|
||||
const login = () => {
|
||||
if (username.value == '' || password.value == '') {
|
||||
dispatchNotification({ title: 'Perhatian', content: 'Username atau password tidak boleh kosong', type: 'warning' })
|
||||
} else {
|
||||
@ -22,11 +23,10 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
isLoading.value = false
|
||||
if (username.value == 'demo' && password.value == 'demo') {
|
||||
// store token in localStorage
|
||||
localStorage.setItem('token', 'secret-token')
|
||||
writeData('token', 'secret-token')
|
||||
dispatchNotification({ title: 'Berhasil', content: 'Login berhasil, selamat datang kembali!', type: 'success' })
|
||||
// redirect to home page after login
|
||||
// router.replace('/')
|
||||
router.go(0)
|
||||
router.push('/home')
|
||||
} else {
|
||||
dispatchNotification({ title: 'Login Gagal', content: 'Username atau password salah', type: 'error' })
|
||||
}
|
||||
@ -34,10 +34,15 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
}
|
||||
}
|
||||
|
||||
function logout() {
|
||||
localStorage.removeItem('token')
|
||||
}
|
||||
const logout = () => removeData('token')
|
||||
|
||||
// expose everything
|
||||
return { token, username, password, isLoggedIn, login, logout, isLoading }
|
||||
return {
|
||||
token,
|
||||
username,
|
||||
password,
|
||||
isLoggedIn,
|
||||
isLoading,
|
||||
login,
|
||||
logout,
|
||||
}
|
||||
})
|
Reference in New Issue
Block a user