Migração completa para Logto - Remoção de Clerk finalizada
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import React, { useState, useEffect, useCallback } from 'react';
|
||||
import React, { useState, useEffect, useCallback, useMemo } from 'react';
|
||||
import { useLogto } from '@logto/react';
|
||||
import type { AppUser } from '../types';
|
||||
import { AuthContext } from './AuthContextType';
|
||||
import { getToken, getUser, setUser, login as logtoLogin } from '../main';
|
||||
import { setUser } from '../main';
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL || '/api';
|
||||
|
||||
@@ -10,41 +11,53 @@ interface AuthProviderProps {
|
||||
}
|
||||
|
||||
export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
||||
const { isAuthenticated, getAccessToken, fetchUserInfo, isLoading: isLogtoLoading } = useLogto();
|
||||
const [appUser, setAppUser] = useState<AppUser | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isAppLoading, setIsAppLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [isSignedIn, setIsSignedIn] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const token = getToken();
|
||||
const user = getUser();
|
||||
|
||||
if (token && user) {
|
||||
setAppUser(user as AppUser);
|
||||
setIsSignedIn(true);
|
||||
}
|
||||
setIsLoading(false);
|
||||
}, []);
|
||||
|
||||
const syncUser = useCallback(async () => {
|
||||
const token = getToken();
|
||||
if (!token) {
|
||||
if (!isAuthenticated) {
|
||||
setAppUser(null);
|
||||
setIsSignedIn(false);
|
||||
setIsLoading(false);
|
||||
setIsAppLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setIsLoading(true);
|
||||
setIsAppLoading(true);
|
||||
setError(null);
|
||||
|
||||
const token = await getAccessToken();
|
||||
if (!token) throw new Error('Token não disponível');
|
||||
|
||||
// Busca dados básicos do Logto se necessário
|
||||
const logtoUserInfo = await fetchUserInfo();
|
||||
|
||||
const response = await fetch(`${API_URL}/users/me`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status === 404 && logtoUserInfo) {
|
||||
// Usuário não existe no banco (provavelmente redirecionamento pós-login)
|
||||
// Vamos tentar sincronizar/provisionar
|
||||
const syncResp = await fetch(`${API_URL}/users/sync`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
email: logtoUserInfo.email,
|
||||
name: logtoUserInfo.name || logtoUserInfo.username || 'Usuário Logto',
|
||||
logto_id: logtoUserInfo.sub
|
||||
})
|
||||
});
|
||||
|
||||
if (!syncResp.ok) throw new Error('Falha ao sincronizar usuário');
|
||||
|
||||
// Tenta buscar novamente após o sync
|
||||
return syncUser();
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Falha ao carregar usuário');
|
||||
}
|
||||
@@ -60,16 +73,20 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
||||
|
||||
setUser(token, user);
|
||||
setAppUser(user);
|
||||
setIsSignedIn(true);
|
||||
} catch (err) {
|
||||
console.error('Error loading user:', err);
|
||||
setError('Erro ao carregar dados do usuário');
|
||||
setAppUser(null);
|
||||
setIsSignedIn(false);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
setIsAppLoading(false);
|
||||
}
|
||||
}, []);
|
||||
}, [isAuthenticated, getAccessToken, fetchUserInfo]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLogtoLoading) {
|
||||
syncUser();
|
||||
}
|
||||
}, [isLogtoLoading, syncUser]);
|
||||
|
||||
const refetchUser = useCallback(async () => {
|
||||
await syncUser();
|
||||
@@ -84,21 +101,23 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
||||
const isGuest = useCallback(() => appUser?.role === 'guest' && !isDeveloper(), [appUser, isDeveloper]);
|
||||
const canEdit = useCallback(() => (appUser?.role !== 'guest' && appUser?.role !== undefined) || isDeveloper(), [appUser, isDeveloper]);
|
||||
|
||||
const isLoading = isLogtoLoading || isAppLoading;
|
||||
|
||||
const value = useMemo(() => ({
|
||||
appUser,
|
||||
isLoading,
|
||||
isSignedIn: isAuthenticated,
|
||||
error,
|
||||
isAdmin,
|
||||
isUser,
|
||||
isGuest,
|
||||
isDeveloper,
|
||||
canEdit,
|
||||
refetchUser,
|
||||
}), [appUser, isLoading, isAuthenticated, error, isAdmin, isUser, isGuest, isDeveloper, canEdit, refetchUser]);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider
|
||||
value={{
|
||||
appUser,
|
||||
isLoading,
|
||||
isSignedIn,
|
||||
error,
|
||||
isAdmin,
|
||||
isUser,
|
||||
isGuest,
|
||||
isDeveloper,
|
||||
canEdit,
|
||||
refetchUser,
|
||||
}}
|
||||
>
|
||||
<AuthContext.Provider value={value}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user