first commit

This commit is contained in:
lendry
2026-06-24 14:37:15 +03:00
commit 995adeedd4
188 changed files with 28810 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
'use client';
import { useCallback, useEffect, useState } from 'react';
import { useAuth } from '@/components/id/auth-provider';
import { apiFetch } from '@/lib/api';
interface AvatarAccessResponse {
accessUrl: string;
expiresAt: string;
}
export function useAvatarUrl(userId: string | undefined, hasAvatar: boolean | undefined, token: string | null) {
const { isPinLocked } = useAuth();
const [avatarUrl, setAvatarUrl] = useState<string | null>(null);
const refresh = useCallback(async () => {
if (!userId || !token || !hasAvatar || isPinLocked) {
setAvatarUrl(null);
return;
}
try {
const response = await apiFetch<AvatarAccessResponse>(`/media/avatars/${userId}/url`, {}, token);
setAvatarUrl(response.accessUrl);
} catch {
setAvatarUrl(null);
}
}, [hasAvatar, isPinLocked, token, userId]);
useEffect(() => {
void refresh();
if (!hasAvatar) return;
const timer = window.setInterval(() => {
void refresh();
}, 14 * 60 * 1000);
return () => window.clearInterval(timer);
}, [hasAvatar, refresh]);
return { avatarUrl, refreshAvatarUrl: refresh };
}

View File

@@ -0,0 +1,25 @@
'use client';
import { useEffect } from 'react';
import { usePathname, useRouter } from 'next/navigation';
import { useAuth } from '@/components/id/auth-provider';
export function useRequireAuth() {
const router = useRouter();
const pathname = usePathname();
const { user, isLoading, isPinLocked, hasStoredSession } = useAuth();
useEffect(() => {
if (isLoading) return;
if (user || isPinLocked || hasStoredSession) return;
if (pathname.startsWith('/auth/')) return;
router.replace('/auth/login');
}, [hasStoredSession, isLoading, isPinLocked, pathname, router, user]);
return {
user,
isLoading,
isPinLocked,
isReady: !isLoading && Boolean(user || isPinLocked || hasStoredSession)
};
}