copied the code from the working repo
This commit is contained in:
56
mtucijobsweb2/fsd/app/provider/AuthContext.tsx
Normal file
56
mtucijobsweb2/fsd/app/provider/AuthContext.tsx
Normal 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;
|
||||
};
|
||||
30
mtucijobsweb2/fsd/app/provider/AuthGuard.tsx
Normal file
30
mtucijobsweb2/fsd/app/provider/AuthGuard.tsx
Normal 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;
|
||||
14
mtucijobsweb2/fsd/app/provider/QueryClient.tsx
Normal file
14
mtucijobsweb2/fsd/app/provider/QueryClient.tsx
Normal 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;
|
||||
25
mtucijobsweb2/fsd/app/provider/withAuth.tsx
Normal file
25
mtucijobsweb2/fsd/app/provider/withAuth.tsx
Normal 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;
|
||||
Reference in New Issue
Block a user