forked from RiseUP/riseup_squad_03
modified: src/App.jsx
modified: src/components/AppShell.jsx modified: src/config/api.js modified: src/config/permissions.js modified: src/data/mockData.js modified: src/hooks/useAgenda.js modified: src/hooks/useAuth.js modified: src/mappers/appointmentMapper.js modified: src/pages/AgendaPage.jsx modified: src/pages/AuthPages.jsx modified: src/pages/HomePage.jsx modified: src/pages/MedicalRecordsPage.jsx modified: src/pages/MessagesPage.jsx modified: src/pages/NotFoundPage.jsx modified: src/pages/PatientsPage.jsx modified: src/pages/ReportsPage.jsx modified: src/pages/TeamPage.jsx modified: src/pages/UsersPage.jsx modified: src/pages/VisitsPage.jsx modified: src/repositories/authRepository.js new file: src/repositories/availabilityRepository.js modified: src/repositories/communicationRepository.js modified: src/repositories/patientRepository.js modified: src/repositories/professionalRepository.js modified: src/repositories/profileRepository.js modified: src/repositories/reportRepository.js modified: src/repositories/repositoryUtils.js modified: src/repositories/settingsRepository.js modified: src/repositories/userRepository.js modified: src/repositories/visitRepository.js
This commit is contained in:
@@ -1,24 +1,23 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
import { getAuthSession, saveAuthSession } from '../config/api.js'
|
||||
import { AUTH_SESSION_CHANGED_EVENT, getAuthSession, saveAuthSession } from '../config/api.js'
|
||||
import { normalizeRole } from '../config/permissions.js'
|
||||
import { authRepository } from '../repositories/authRepository.js'
|
||||
|
||||
export function useAuth() {
|
||||
const [state, setState] = useState(() => {
|
||||
const session = getAuthSession()
|
||||
return {
|
||||
user: session?.user ?? null,
|
||||
role: session?.role ?? null,
|
||||
profile: session?.profile ?? null,
|
||||
isAuthenticated: !!session?.access_token,
|
||||
loading: !!session?.access_token && !session?.role,
|
||||
}
|
||||
})
|
||||
const [state, setState] = useState(() => getStateFromSession(getAuthSession()))
|
||||
|
||||
useEffect(() => {
|
||||
function syncSession() {
|
||||
setState(getStateFromSession(getAuthSession()))
|
||||
}
|
||||
|
||||
window.addEventListener(AUTH_SESSION_CHANGED_EVENT, syncSession)
|
||||
return () => window.removeEventListener(AUTH_SESSION_CHANGED_EVENT, syncSession)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
// Se não está autenticado ou já tem role salvo, não busca
|
||||
if (!state.isAuthenticated || state.role) {
|
||||
setState((s) => ({ ...s, loading: false }))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -29,22 +28,16 @@ export function useAuth() {
|
||||
.then((data) => {
|
||||
if (cancelled || !data) return
|
||||
|
||||
// Suporta diferentes formatos de resposta da API
|
||||
const role =
|
||||
Array.isArray(data.roles) ? data.roles[0]
|
||||
: (data.role ?? data.user_metadata?.role ?? data.app_metadata?.role ?? null)
|
||||
|
||||
const profile = data.profile ?? null
|
||||
const user = data.user ?? data ?? null
|
||||
|
||||
// Persiste na sessão para evitar nova busca a cada reload
|
||||
const profile = data.profile ?? data.perfil ?? null
|
||||
const user = data.user ?? data.usuario ?? data ?? null
|
||||
const role = resolveRole(data)
|
||||
const session = getAuthSession()
|
||||
saveAuthSession({ ...session, role, profile, user: user || session?.user })
|
||||
|
||||
setState((s) => ({ ...s, role, profile, user: user || s.user, loading: false }))
|
||||
saveAuthSession({ ...session, role, profile, user: user || session?.user })
|
||||
setState((current) => ({ ...current, role, profile, user: user || current.user, loading: false }))
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setState((s) => ({ ...s, loading: false }))
|
||||
if (!cancelled) setState((current) => ({ ...current, loading: false }))
|
||||
})
|
||||
|
||||
return () => {
|
||||
@@ -53,4 +46,56 @@ export function useAuth() {
|
||||
}, [state.isAuthenticated, state.role])
|
||||
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
function getStateFromSession(session) {
|
||||
const role = normalizeRole(session?.role)
|
||||
|
||||
return {
|
||||
user: session?.user ?? null,
|
||||
role,
|
||||
profile: session?.profile ?? null,
|
||||
isAuthenticated: !!session?.access_token,
|
||||
loading: !!session?.access_token && !role,
|
||||
}
|
||||
}
|
||||
|
||||
function resolveRole(data) {
|
||||
const user = data?.user ?? data?.usuario ?? {}
|
||||
const profile = data?.profile ?? data?.perfil ?? {}
|
||||
const metadata = {
|
||||
...user?.user_metadata,
|
||||
...user?.app_metadata,
|
||||
...user?.metadata,
|
||||
...data?.user_metadata,
|
||||
...data?.app_metadata,
|
||||
...data?.metadata,
|
||||
}
|
||||
|
||||
const candidates = [
|
||||
...(Array.isArray(data?.roles) ? data.roles : []),
|
||||
...(Array.isArray(user?.roles) ? user.roles : []),
|
||||
data?.role,
|
||||
data?.cargo,
|
||||
profile?.role,
|
||||
profile?.cargo,
|
||||
user?.role,
|
||||
user?.cargo,
|
||||
metadata.role,
|
||||
metadata.cargo,
|
||||
]
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const role = normalizeRole(candidate)
|
||||
if (role) return role
|
||||
}
|
||||
|
||||
const permissions = data?.permissions ?? data?.permissoes ?? {}
|
||||
if (permissions.isAdmin) return 'admin'
|
||||
if (permissions.isManager) return 'gestor'
|
||||
if (permissions.isDoctor) return 'medico'
|
||||
if (permissions.isSecretary) return 'secretaria'
|
||||
if (permissions.isPatient) return 'paciente'
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user