feat: enhance API and profile management with environment configurations
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
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.
This commit is contained in:
1 parent
18fa4de055
commit
c2d9eac151
12 files changed
+351
-57
No files matched your search
@@ -1,58 +1,70 @@
|
||||
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 { session } = useAuth()
|
||||
const [profiles, setProfiles] = useState([])
|
||||
const { user, isAuthenticated } = useAuth()
|
||||
const [profiles, setProfiles] = useState([])
|
||||
const [activeProfile, setActiveProfileState] = useState(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
const loadProfiles = async () => {
|
||||
const loadProfiles = async (authUser) => {
|
||||
try {
|
||||
const token = localStorage.getItem('bodytrack_token') || ''
|
||||
const res = await fetch('/api/profiles', {
|
||||
headers: { 'X-Auth-Token': token }
|
||||
})
|
||||
if (!res.ok) return []
|
||||
return await res.json()
|
||||
} catch(e) { return [] }
|
||||
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 []
|
||||
}
|
||||
}
|
||||
|
||||
// Re-load whenever session changes (login/logout/switch)
|
||||
useEffect(() => {
|
||||
if (!session) {
|
||||
if (!isAuthenticated || !user?.id) {
|
||||
setActiveProfileState(null)
|
||||
setProfiles([])
|
||||
setLoading(false)
|
||||
return
|
||||
}
|
||||
setLoading(true)
|
||||
loadProfiles().then(data => {
|
||||
setProfiles(data)
|
||||
// Always use the profile_id from the session token – not localStorage
|
||||
const match = data.find(p => p.id === session.profile_id)
|
||||
setActiveProfileState(match || data[0] || null)
|
||||
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)
|
||||
})
|
||||
}, [session?.profile_id]) // re-runs when profile changes
|
||||
}, [isAuthenticated, user?.id, user?.role])
|
||||
|
||||
const setActiveProfile = (profile) => {
|
||||
setActiveProfileState(profile)
|
||||
localStorage.setItem('bodytrack_active_profile', profile.id)
|
||||
localStorage.setItem('shinkan_active_profile', String(profile.id))
|
||||
}
|
||||
|
||||
const refreshProfiles = () => loadProfiles().then(data => {
|
||||
setProfiles(data)
|
||||
if (activeProfile) {
|
||||
const updated = data.find(p => p.id === activeProfile.id)
|
||||
if (updated) setActiveProfileState(updated)
|
||||
}
|
||||
})
|
||||
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 }}>
|
||||
<ProfileContext.Provider
|
||||
value={{ profiles, activeProfile, setActiveProfile, refreshProfiles, loading }}
|
||||
>
|
||||
{children}
|
||||
</ProfileContext.Provider>
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user