frontend 2026-06-10 bontva a 2 felület
This commit is contained in:
@@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import router from '../router'
|
||||
import api from '../api/axios'
|
||||
import type { OrganizationItem } from '../types/organization'
|
||||
|
||||
/**
|
||||
* Backend now sends address fields directly on the person object
|
||||
@@ -76,6 +77,8 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
const token = ref<string | null>(localStorage.getItem('access_token'))
|
||||
const isLoading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
const myOrganizations = ref<OrganizationItem[]>([])
|
||||
const isInitialized = ref(false)
|
||||
|
||||
// ────────────────────────────── Getters ────────────────────────────
|
||||
const isAuthenticated = computed(() => !!token.value && !!user.value)
|
||||
@@ -182,6 +185,9 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
// Immediately fetch the user profile
|
||||
await fetchUser()
|
||||
|
||||
// Fetch organizations after successful login so the header can show them
|
||||
await fetchMyOrganizations()
|
||||
|
||||
// Clear pending verification email after successful login
|
||||
localStorage.removeItem('pending_verification_email')
|
||||
|
||||
@@ -341,10 +347,14 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
if (token.value) {
|
||||
try {
|
||||
await fetchUser()
|
||||
// Load organizations so the header switcher has data immediately
|
||||
await fetchMyOrganizations()
|
||||
} catch {
|
||||
// Token invalid — already cleared by fetchUser → logout
|
||||
}
|
||||
}
|
||||
// Mark initialization as complete so the router guard can await it
|
||||
isInitialized.value = true
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -471,12 +481,94 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
error.value = null
|
||||
}
|
||||
|
||||
// ──────────────────────── Organization Actions ──────────────────────
|
||||
|
||||
/**
|
||||
* Fetch the current user's organizations from GET /api/v1/organizations/my.
|
||||
* Stores them in myOrganizations state.
|
||||
*/
|
||||
async function fetchMyOrganizations() {
|
||||
try {
|
||||
const res = await api.get('/organizations/my')
|
||||
myOrganizations.value = res.data as OrganizationItem[]
|
||||
return myOrganizations.value
|
||||
} catch (err: any) {
|
||||
console.error('Failed to fetch organizations:', err)
|
||||
myOrganizations.value = []
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's first business organization (org_type === 'business' or similar).
|
||||
* Returns the organization item or null if none found.
|
||||
*/
|
||||
const businessOrganization = computed(() => {
|
||||
return myOrganizations.value.find(
|
||||
(org) => org.org_type === 'business' || org.org_type === 'fleet_owner'
|
||||
) || null
|
||||
})
|
||||
|
||||
/**
|
||||
* Check if the user has any business organization.
|
||||
*/
|
||||
const hasBusinessOrganization = computed(() => {
|
||||
return !!businessOrganization.value
|
||||
})
|
||||
|
||||
/**
|
||||
* Check if the user is currently in corporate mode
|
||||
* (active_organization_id is set and matches a business org).
|
||||
*/
|
||||
const isCorporateMode = computed(() => {
|
||||
return !!user.value?.active_organization_id
|
||||
})
|
||||
|
||||
/**
|
||||
* Switch the user's active organization.
|
||||
* PATCH /api/v1/users/me/active-organization with { organization_id }.
|
||||
* On success, updates the token and user state, then navigates.
|
||||
*/
|
||||
async function switchOrganization(orgId: number | null) {
|
||||
isLoading.value = true
|
||||
error.value = null
|
||||
|
||||
try {
|
||||
const res = await api.patch('/users/me/active-organization', {
|
||||
organization_id: orgId,
|
||||
})
|
||||
|
||||
const data = res.data as {
|
||||
user: UserProfile
|
||||
access_token: string
|
||||
token_type: string
|
||||
}
|
||||
|
||||
// Update the stored token
|
||||
localStorage.setItem('access_token', data.access_token)
|
||||
token.value = data.access_token
|
||||
|
||||
// Update the user state with the fresh profile
|
||||
user.value = data.user
|
||||
} catch (err: any) {
|
||||
error.value =
|
||||
err.response?.data?.detail ||
|
||||
err.response?.data?.message ||
|
||||
'Failed to switch organization'
|
||||
throw err
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
user,
|
||||
token,
|
||||
isLoading,
|
||||
error,
|
||||
myOrganizations,
|
||||
isInitialized,
|
||||
|
||||
// Getters
|
||||
isAuthenticated,
|
||||
@@ -484,6 +576,9 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
userRole,
|
||||
userName,
|
||||
userId,
|
||||
businessOrganization,
|
||||
hasBusinessOrganization,
|
||||
isCorporateMode,
|
||||
|
||||
// Actions
|
||||
login,
|
||||
@@ -499,5 +594,7 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
logout,
|
||||
init,
|
||||
clearError,
|
||||
fetchMyOrganizations,
|
||||
switchOrganization,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user