copied the code from the working repo

This commit is contained in:
2024-11-30 16:00:48 +03:00
parent f22b92869b
commit 15ac0cb9b8
148 changed files with 23342 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
'use client';
import React, { createContext, useContext, useState, useEffect } from 'react';
interface AuthContextType {
isAuthenticated: boolean;
loading: boolean;
login: (token: string, expiresAt: number) => void;
logout: () => void;
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [loading, setLoading] = useState(true);
useEffect(() => {
const token = localStorage.getItem('token');
const expiresAt = localStorage.getItem('expiresAt');
if (token && expiresAt && new Date().getTime() < Number(expiresAt)) {
setIsAuthenticated(true);
} else {
setIsAuthenticated(false);
}
setLoading(false); // завершение проверки
}, []);
const login = (token: string, expiresInSeconds: number) => {
const expiresAt = new Date().getTime() + expiresInSeconds * 1000;
localStorage.setItem('expiresAt', expiresAt.toString());
setIsAuthenticated(true);
};
const logout = () => {
localStorage.removeItem('token');
localStorage.removeItem('expiresAt');
setIsAuthenticated(false);
};
return (
<AuthContext.Provider value={{ isAuthenticated, loading, login, logout }}>
{children}
</AuthContext.Provider>
);
};
export const useAuth = (): AuthContextType => {
const context = useContext(AuthContext);
if (!context) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};

View File

@@ -0,0 +1,30 @@
'use client';
import { useRouter, usePathname } from 'next/navigation';
import { useEffect } from 'react';
import { useAuth } from '@/fsd/app/provider/AuthContext';
const AuthGuard: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const { isAuthenticated, loading } = useAuth();
const router = useRouter();
const pathname = usePathname();
useEffect(() => {
if (!loading) {
if (!isAuthenticated && pathname !== '/login') {
router.replace('/login');
}
}
}, [loading, isAuthenticated, pathname, router]);
if (loading) {
return <div>Loading...</div>; // Показываем загрузку
}
if (!isAuthenticated && pathname !== '/login') {
return null; // Не рендерим контент до завершения редиректа
}
return <>{children}</>;
};
export default AuthGuard;

View File

@@ -0,0 +1,14 @@
'use client';
import { PropsWithChildren } from 'react';
import { QueryClient, QueryClientProvider } from 'react-query';
export const queryClient = new QueryClient();
const ClientProvider = ({ children }: PropsWithChildren) => {
return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
};
export default ClientProvider;

View File

@@ -0,0 +1,25 @@
'use client';
import React, { useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useAuth } from '@/fsd/app/provider/AuthContext';
const AuthGuard: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const { isAuthenticated } = useAuth();
const router = useRouter();
useEffect(() => {
if (!isAuthenticated) {
router.replace('/login');
}
}, [isAuthenticated, router]);
if (!isAuthenticated) {
return null; // Или можно отобразить загрузочный экран
}
return <>{children}</>;
};
export default AuthGuard;