Files
onboard/resources/js/Stores/auth.js
brusnitsyn cb43c74a72 * добавлены операции и услуги операций
* добавлена выборка и подсчет по датам для роли зав.
* переключатель ролей
* выбор отделений для роли зав.
2026-01-22 17:58:27 +09:00

82 lines
2.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {ref, computed, watch} from 'vue'
import { defineStore } from 'pinia'
import axios from 'axios'
import {usePage} from "@inertiajs/vue3";
export const useAuthStore = defineStore('authStore', () => {
const page = usePage()
const user = ref(page.props.user)
const token = computed(() => user.value?.token)
const role = computed(() => user.value?.role)
const permissions = computed(() => user.value?.permissions)
const availableDepartments = computed(() => user.value?.available_departments)
const availableRoles = computed(() => user.value?.available_roles)
watch(
() => page.props.user,
(newUser) => {
user.value = newUser
},
{ deep: true, immediate: true })
// Инициализация axios с токеном
if (token?.value) {
axios.defaults.headers.common['Authorization'] = `Bearer ${token.value}`
}
// Вычисляемые свойства
const isAuthenticated = computed(() => !!user.value && !!token.value)
const isAdmin = computed(() => role.value?.slug === 'admin')
const isDoctor = computed(() => role.value?.slug === 'doctor')
const isHeadOfDepartment = computed(() => role.value?.slug === 'head_of_department')
const userDepartment = computed(() => user.value?.current_department || '')
const clearAuthData = () => {
user.value = null
token.value = null
permissions.value = {}
availableDepartments.value = []
localStorage.removeItem('token')
localStorage.removeItem('user')
delete axios.defaults.headers.common['Authorization']
}
const logout = async () => {
try {
await axios.post('/api/auth/logout')
} catch (error) {
console.error('Ошибка при выходе:', error)
} finally {
clearAuthData()
}
}
// Проверка прав
const hasPermission = (permission) => {
return permissions.value[permission] === true
}
const canAccessDepartment = (department) => {
if (isAdmin.value || isHeadOfDepartment.value) return true
return availableDepartments.value.includes(department)
}
return {
user,
token,
permissions,
availableDepartments,
availableRoles,
isAuthenticated,
isAdmin,
isDoctor,
isHeadOfDepartment,
userDepartment,
clearAuthData,
hasPermission,
canAccessDepartment
}
})