49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
'use client';
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
//! Imports
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
// -------------------------------------------------- React & Next -----------------------------------------------------
|
|
import { useEffect, useState } from 'react';
|
|
import { redirect, usePathname } from 'next/navigation';
|
|
import Navbar from '@/components/Navbar/Navbar';
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
const Provider = ({ children }) => {
|
|
const pathname = usePathname();
|
|
const [isSuccess, setIsSuccess] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (pathname.startsWith('/auth')) {
|
|
setIsSuccess(true);
|
|
return;
|
|
}
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
if (!token) {
|
|
redirect('/auth/login');
|
|
}
|
|
|
|
setIsSuccess(true);
|
|
}, [pathname]);
|
|
|
|
if (!isSuccess) {
|
|
return;
|
|
}
|
|
|
|
if (pathname.startsWith('/auth')) {
|
|
return children;
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{children}
|
|
<Navbar />
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default Provider;
|