/
lizardking
/
react_final
Обзор
Документация
Войти
/
lizardking
/
react_final
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
src/widgets/auth/ui/WithProtection.tsx
25 строк
830 B
LizardKing
Рефакторинг на FSD
24 ноя 2025, 16:05
24 ноя 2025, 16:05
eedf5c5
Код
Авторство
О чём код?
import { userSelectors } from 'features/user/slice/user' import type { ComponentType, FC } from 'react' import { Navigate, useLocation } from 'react-router-dom' import { useAppSelector } from 'shared/store/utils' export const WithProtection = <P extends object>(WrappedComponent: ComponentType<P>) => { const ReturnedComponent: FC<P> = (props) => { const accessToken = useAppSelector(userSelectors.getAccessToken) const location = useLocation() const authPaths = ['/signin', '/signup'] if (!accessToken && !authPaths.includes(location.pathname)) { return ( <Navigate to="/signin" state={{ from: location.pathname }} /> ) } return <WrappedComponent {...props} /> } ReturnedComponent.displayName = `withProtection${WrappedComponent.displayName}` return ReturnedComponent }