feat: enhance user profile management and MFA login flow

- Refactor user profile loading and updating logic to improve state management and reduce unnecessary checks.
- Update MFA login gate to streamline verification checks and improve user experience.
- Ensure consistent handling of user ID across profile context and components.
- Improve error handling and loading states in user-related API calls.
This commit is contained in:
willchen96 2026-06-10 18:55:33 +08:00
parent 3a10943200
commit 444d1d38e4
3 changed files with 576 additions and 474 deletions

View file

@ -96,6 +96,7 @@ export function UserProfileProvider({ children }: { children: ReactNode }) {
const { user, isAuthenticated } = useAuth();
const [profile, setProfile] = useState<UserProfile | null>(null);
const [loading, setLoading] = useState(true);
const userId = user?.id ?? null;
const loadProfile = useCallback(async () => {
try {
@ -125,14 +126,14 @@ export function UserProfileProvider({ children }: { children: ReactNode }) {
}, []);
useEffect(() => {
if (isAuthenticated && user) {
if (isAuthenticated && userId) {
setLoading(true);
loadProfile();
} else {
setProfile(null);
setLoading(false);
}
}, [isAuthenticated, user, loadProfile]);
}, [isAuthenticated, userId, loadProfile]);
const updateDisplayName = useCallback(
async (displayName: string): Promise<boolean> => {
@ -241,10 +242,10 @@ export function UserProfileProvider({ children }: { children: ReactNode }) {
);
const reloadProfile = useCallback(async () => {
if (user) {
if (userId) {
await loadProfile();
}
}, [user, loadProfile]);
}, [userId, loadProfile]);
const incrementMessageCredits = useCallback(async (): Promise<boolean> => {
if (!user || !profile) {