All checks were successful
Deploy Development / deploy (push) Successful in 34s
Test Suite / pytest-backend (push) Successful in 23s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 7s
Test Suite / playwright-tests (push) Successful in 24s
- Added functions to determine production environment and OpenAPI exposure settings, improving API documentation control. - Updated FastAPI initialization to conditionally set OpenAPI and documentation URLs based on environment variables. - Refactored health check response to limit detail exposure in production environments, enhancing security. - Streamlined profile management by removing legacy ID retrieval and ensuring session-based profile access for security improvements.
76 lines
2.1 KiB
JavaScript
76 lines
2.1 KiB
JavaScript
import { createContext, useContext, useState, useEffect } from 'react'
|
|
import { useAuth } from './AuthContext'
|
|
import { getCurrentProfile, listProfiles } from '../utils/api'
|
|
|
|
const ProfileContext = createContext(null)
|
|
|
|
export function ProfileProvider({ children }) {
|
|
const { user, isAuthenticated } = useAuth()
|
|
const [profiles, setProfiles] = useState([])
|
|
const [activeProfile, setActiveProfileState] = useState(null)
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
const loadProfiles = async (authUser) => {
|
|
try {
|
|
if (!authUser?.id) return []
|
|
const admin = authUser.role === 'admin' || authUser.role === 'superadmin'
|
|
if (admin) {
|
|
try {
|
|
return await listProfiles()
|
|
} catch {
|
|
const me = await getCurrentProfile()
|
|
return me ? [me] : []
|
|
}
|
|
}
|
|
const me = await getCurrentProfile()
|
|
return me ? [me] : []
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (!isAuthenticated || !user?.id) {
|
|
setActiveProfileState(null)
|
|
setProfiles([])
|
|
setLoading(false)
|
|
return
|
|
}
|
|
setLoading(true)
|
|
loadProfiles(user).then((data) => {
|
|
const rows = Array.isArray(data) ? data : []
|
|
setProfiles(rows)
|
|
const uid = user.id
|
|
const match = rows.find((p) => String(p.id) === String(uid))
|
|
setActiveProfileState(match || rows[0] || null)
|
|
setLoading(false)
|
|
})
|
|
}, [isAuthenticated, user?.id, user?.role])
|
|
|
|
const setActiveProfile = (profile) => {
|
|
setActiveProfileState(profile)
|
|
localStorage.setItem('shinkan_active_profile', String(profile.id))
|
|
}
|
|
|
|
const refreshProfiles = () =>
|
|
loadProfiles(user).then((data) => {
|
|
setProfiles(Array.isArray(data) ? data : [])
|
|
if (activeProfile) {
|
|
const updated = data.find((p) => String(p.id) === String(activeProfile.id))
|
|
if (updated) setActiveProfileState(updated)
|
|
}
|
|
})
|
|
|
|
return (
|
|
<ProfileContext.Provider
|
|
value={{ profiles, activeProfile, setActiveProfile, refreshProfiles, loading }}
|
|
>
|
|
{children}
|
|
</ProfileContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useProfile() {
|
|
return useContext(ProfileContext)
|
|
}
|