Initial commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { computed, ref } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
export interface AuthUser {
|
||||
id: string
|
||||
email?: string | null
|
||||
firstName?: string | null
|
||||
lastName?: string | null
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const isInitialized = ref(false)
|
||||
const user = ref<AuthUser | null>(null)
|
||||
|
||||
const roles = computed(() => user.value?.roles ?? [])
|
||||
const isAuthenticated = computed(() => !!user.value)
|
||||
const displayName = computed(() => {
|
||||
if (!user.value) {
|
||||
return ''
|
||||
}
|
||||
|
||||
const fullName = [user.value.firstName, user.value.lastName].filter(Boolean).join(' ').trim()
|
||||
|
||||
return fullName || user.value.email || user.value.id
|
||||
})
|
||||
|
||||
function initialize(nextUser: AuthUser | null = null) {
|
||||
user.value = nextUser
|
||||
isInitialized.value = true
|
||||
}
|
||||
|
||||
function setUser(nextUser: AuthUser) {
|
||||
user.value = nextUser
|
||||
isInitialized.value = true
|
||||
}
|
||||
|
||||
function clearSession() {
|
||||
user.value = null
|
||||
isInitialized.value = true
|
||||
}
|
||||
|
||||
function hasRole(role: string) {
|
||||
return roles.value.includes(role)
|
||||
}
|
||||
|
||||
function hasAnyRole(requiredRoles: string[]) {
|
||||
return requiredRoles.some((role) => roles.value.includes(role))
|
||||
}
|
||||
|
||||
return {
|
||||
displayName,
|
||||
hasAnyRole,
|
||||
hasRole,
|
||||
initialize,
|
||||
isAuthenticated,
|
||||
isInitialized,
|
||||
roles,
|
||||
setUser,
|
||||
clearSession,
|
||||
user
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,61 @@
|
||||
import { computed, ref } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
export type UiNotificationTone = 'info' | 'success' | 'warning' | 'error'
|
||||
|
||||
export interface UiNotification {
|
||||
id: string
|
||||
title: string
|
||||
message?: string
|
||||
tone?: UiNotificationTone
|
||||
}
|
||||
|
||||
export const useUiStore = defineStore('ui', () => {
|
||||
const activeRequests = ref(0)
|
||||
const notifications = ref<UiNotification[]>([])
|
||||
|
||||
const isPageLoading = computed(() => activeRequests.value > 0)
|
||||
|
||||
function setLoading(value: boolean) {
|
||||
activeRequests.value = value ? 1 : 0
|
||||
}
|
||||
|
||||
function startLoading() {
|
||||
activeRequests.value += 1
|
||||
}
|
||||
|
||||
function stopLoading() {
|
||||
activeRequests.value = Math.max(0, activeRequests.value - 1)
|
||||
}
|
||||
|
||||
function pushNotification(notification: Omit<UiNotification, 'id'> & { id?: string }) {
|
||||
const nextNotification = {
|
||||
...notification,
|
||||
id: notification.id ?? crypto.randomUUID(),
|
||||
tone: notification.tone ?? 'info'
|
||||
} satisfies UiNotification
|
||||
|
||||
notifications.value = [...notifications.value, nextNotification]
|
||||
|
||||
return nextNotification.id
|
||||
}
|
||||
|
||||
function removeNotification(id: string) {
|
||||
notifications.value = notifications.value.filter((notification) => notification.id !== id)
|
||||
}
|
||||
|
||||
function clearNotifications() {
|
||||
notifications.value = []
|
||||
}
|
||||
|
||||
return {
|
||||
clearNotifications,
|
||||
isPageLoading,
|
||||
notifications,
|
||||
pushNotification,
|
||||
removeNotification,
|
||||
setLoading,
|
||||
startLoading,
|
||||
stopLoading
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user