develop #83
@ -89,22 +89,7 @@ export default function AssignmentForm({ patientId, open, onClose, onSaved }: Pr
|
|||||||
</Select>
|
</Select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
{/* role input removed - only professional select remains; role defaults to 'medico' on submit */}
|
||||||
<Label>Role</Label>
|
|
||||||
<Input
|
|
||||||
value={role}
|
|
||||||
onChange={(e) => {
|
|
||||||
const v = String(e.target.value || '').toLowerCase().trim();
|
|
||||||
// Map common english values to portuguese expected by backend
|
|
||||||
if (v === 'doctor') return setRole('medico');
|
|
||||||
if (v === 'nurse') return setRole('enfermeiro');
|
|
||||||
if (v === 'medico' || v === 'enfermeiro') return setRole(v as PatientAssignmentRole);
|
|
||||||
// fallback: keep current role (ignore unknown input)
|
|
||||||
return setRole(role);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<div className="text-xs text-muted-foreground mt-1">Ex: medico, enfermeiro (inglês: doctor → medico)</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{existing && existing.length > 0 && (
|
{existing && existing.length > 0 && (
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
import { createContext, useContext, useEffect, useState, ReactNode, useCallback, useMemo, useRef } from 'react'
|
import { createContext, useContext, useEffect, useState, ReactNode, useCallback, useMemo, useRef } from 'react'
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter } from 'next/navigation'
|
||||||
import { loginUser, logoutUser, AuthenticationError } from '@/lib/auth'
|
import { loginUser, logoutUser, AuthenticationError } from '@/lib/auth'
|
||||||
|
import { getUserInfo } from '@/lib/api'
|
||||||
import { ENV_CONFIG } from '@/lib/env-config'
|
import { ENV_CONFIG } from '@/lib/env-config'
|
||||||
import { isExpired, parseJwt } from '@/lib/jwt'
|
import { isExpired, parseJwt } from '@/lib/jwt'
|
||||||
import { httpClient } from '@/lib/http'
|
import { httpClient } from '@/lib/http'
|
||||||
@ -131,6 +132,35 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||||||
// Restaurar sessão válida
|
// Restaurar sessão válida
|
||||||
const userData = JSON.parse(storedUser) as UserData
|
const userData = JSON.parse(storedUser) as UserData
|
||||||
setToken(storedToken)
|
setToken(storedToken)
|
||||||
|
// Tentar buscar profile consolidado (user-info) e mesclar
|
||||||
|
try {
|
||||||
|
const info = await getUserInfo()
|
||||||
|
if (info?.profile) {
|
||||||
|
const mapped = {
|
||||||
|
cpf: (info.profile as any).cpf ?? userData.profile?.cpf,
|
||||||
|
crm: (info.profile as any).crm ?? userData.profile?.crm,
|
||||||
|
telefone: info.profile.phone ?? userData.profile?.telefone,
|
||||||
|
foto_url: info.profile.avatar_url ?? userData.profile?.foto_url,
|
||||||
|
}
|
||||||
|
if (userData.profile) {
|
||||||
|
userData.profile = { ...userData.profile, ...mapped }
|
||||||
|
} else {
|
||||||
|
userData.profile = mapped
|
||||||
|
}
|
||||||
|
// Persistir o usuário atualizado no localStorage para evitar
|
||||||
|
// que 'auth_user.profile' fique vazio após um reload completo
|
||||||
|
try {
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
localStorage.setItem(AUTH_STORAGE_KEYS.USER, JSON.stringify(userData))
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[AUTH] Falha ao persistir user (profile) no localStorage:', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('[AUTH] Falha ao buscar user-info na restauração de sessão:', err)
|
||||||
|
}
|
||||||
|
|
||||||
setUser(userData)
|
setUser(userData)
|
||||||
setAuthStatus('authenticated')
|
setAuthStatus('authenticated')
|
||||||
|
|
||||||
@ -195,6 +225,26 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||||||
console.warn('[AUTH] Erro ao buscar user-info após login (não crítico):', err)
|
console.warn('[AUTH] Erro ao buscar user-info após login (não crítico):', err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Após login, tentar buscar profile consolidado e mesclar antes de persistir
|
||||||
|
try {
|
||||||
|
const info = await getUserInfo()
|
||||||
|
if (info?.profile && response.user) {
|
||||||
|
const mapped = {
|
||||||
|
cpf: (info.profile as any).cpf ?? response.user.profile?.cpf,
|
||||||
|
crm: (info.profile as any).crm ?? response.user.profile?.crm,
|
||||||
|
telefone: info.profile.phone ?? response.user.profile?.telefone,
|
||||||
|
foto_url: info.profile.avatar_url ?? response.user.profile?.foto_url,
|
||||||
|
}
|
||||||
|
if (response.user.profile) {
|
||||||
|
response.user.profile = { ...response.user.profile, ...mapped }
|
||||||
|
} else {
|
||||||
|
response.user.profile = mapped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('[AUTH] Falha ao buscar user-info após login (não crítico):', err)
|
||||||
|
}
|
||||||
|
|
||||||
saveAuthData(
|
saveAuthData(
|
||||||
response.access_token,
|
response.access_token,
|
||||||
response.user,
|
response.user,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user