mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-28 04:01:10 -04:00
75 lines
1.9 KiB
JavaScript
75 lines
1.9 KiB
JavaScript
import axios from 'axios'
|
|
import { defineStore } from 'pinia'
|
|
import { useNotificationStore } from './notification'
|
|
import { useResetStore } from './reset'
|
|
import router from '@/scripts/router'
|
|
import { handleError } from '@/scripts/helpers/error-handling'
|
|
|
|
export const useAuthStore = (useWindow = false) => {
|
|
const defineStoreFunc = useWindow ? window.pinia.defineStore : defineStore
|
|
const { global } = window.i18n
|
|
const resetStore = useResetStore()
|
|
|
|
return defineStoreFunc({
|
|
id: 'auth',
|
|
state: () => ({
|
|
status: '',
|
|
|
|
loginData: {
|
|
email: '',
|
|
password: '',
|
|
remember: '',
|
|
},
|
|
}),
|
|
|
|
actions: {
|
|
login(data) {
|
|
return new Promise((resolve, reject) => {
|
|
axios.get('/sanctum/csrf-cookie').then((response) => {
|
|
if (response) {
|
|
axios
|
|
.post('/login', data)
|
|
.then((response) => {
|
|
resolve(response)
|
|
|
|
setTimeout(() => {
|
|
this.loginData.email = ''
|
|
this.loginData.password = ''
|
|
}, 1000)
|
|
})
|
|
.catch((err) => {
|
|
handleError(err)
|
|
reject(err)
|
|
})
|
|
}
|
|
})
|
|
})
|
|
},
|
|
|
|
logout() {
|
|
return new Promise((resolve, reject) => {
|
|
axios
|
|
.get('/auth/logout')
|
|
.then((response) => {
|
|
const notificationStore = useNotificationStore()
|
|
|
|
notificationStore.showNotification({
|
|
type: 'success',
|
|
message: 'Logged out successfully.',
|
|
})
|
|
|
|
router.push('/login')
|
|
resetStore.clearPinia()
|
|
resolve(response)
|
|
})
|
|
.catch((err) => {
|
|
handleError(err)
|
|
router.push('/')
|
|
reject(err)
|
|
})
|
|
})
|
|
},
|
|
},
|
|
})()
|
|
}
|