@@ -8,19 +8,19 @@ import Habits_page from "./pages/Habits/HabitsPage";
88import TasksPage from "./pages/Tasks/TasksPage" ;
99//@ts -ignore
1010import MatricPage from "./pages/MatricPage/MatricPage" ;
11- import Premium from "./pages/main/pages/Premium" ;
1211import PremiumPage from "./pages/main/pages/Premium" ;
1312import Lending from "./pages/main/pages/Main" ;
1413import LoginPage from "./pages/AuthPages/Login" ;
1514import RegisterPage from "./pages/AuthPages/Register" ;
16- import { AuthCheck } from "./ui/props/Auth/AuthCheck" ;
15+ import { AuthCheck } from "./ui/props/Auth/AuthCheck" ;
1716import HabitCalendar from "./pages/Statistic/StatisticPage" ;
1817
1918const RouterDom = ( ) => {
20- const [ isAuthenticated , setIsAuthenticated ] = useState < boolean | null > ( null ) ;
19+ const [ isAuthenticated , setIsAuthenticated ] = useState < boolean | null > ( null ) ;
2120 const location = useLocation ( ) ;
2221 const navigate = useNavigate ( ) ;
2322
23+ // Сохраняем последнюю защищённую страницу
2424 useEffect ( ( ) => {
2525 const allowedPages = [
2626 '/tasks' ,
@@ -30,92 +30,131 @@ const RouterDom = () => {
3030 '/profile' ,
3131 '/profile/statistics'
3232 ] ;
33-
3433 if ( allowedPages . includes ( location . pathname . toLowerCase ( ) ) ) {
3534 localStorage . setItem ( 'last_page' , location . pathname ) ;
3635 }
3736 } , [ location . pathname ] ) ;
3837
38+
3939 useEffect ( ( ) => {
4040 const checkAuth = async ( ) => {
4141 try {
4242 const res = await fetch ( '/api/auth/check' ) ;
4343 const ok = res . ok ;
4444 setIsAuthenticated ( ok ) ;
4545
46+ if ( ok ) {
47+ console . log ( 'Зов пениса' ) ;
48+ subscribePush ( ) ;
49+ }
50+
4651 if ( ok && location . pathname === '/' ) {
47- const lastPage = localStorage . getItem ( 'last_page' ) || '/Tasks ' ;
52+ const lastPage = localStorage . getItem ( 'last_page' ) || '/tasks ' ;
4853 navigate ( lastPage , { replace : true } ) ;
4954 }
55+
56+ const protectedPages = [
57+ '/tasks' ,
58+ '/habits' ,
59+ '/matric' ,
60+ '/pomodoro' ,
61+ '/profile' ,
62+ '/profile/statistics' ,
63+ '/premium'
64+ ] ;
65+ if ( ! ok && protectedPages . includes ( location . pathname . toLowerCase ( ) ) ) {
66+ navigate ( '/users/login' , { replace : true } ) ;
67+ }
68+
5069 } catch {
5170 setIsAuthenticated ( false ) ;
71+ if ( ! [ '/users/login' , '/users/register' ] . includes ( location . pathname . toLowerCase ( ) ) ) {
72+ navigate ( '/users/login' , { replace : true } ) ;
73+ }
5274 }
5375 } ;
5476 checkAuth ( ) ;
55- } , [ ] ) ;
77+ } , [ location . pathname , navigate ] ) ;
5678
57- if ( isAuthenticated === null ) return < Loading /> ;
5879
59- return (
60- < Routes >
61- < Route path = "/" element = { < Lending /> } />
6280
63- < Route
64- path = "/profile"
65- element = {
66- < AuthCheck isAuthenticated = { isAuthenticated } >
67- < Profile />
68- </ AuthCheck >
69- }
70- />
71-
72- < Route
73- path = "/pomodoro"
74- element = {
75- < AuthCheck isAuthenticated = { isAuthenticated } >
76- < Pomodoro />
77- </ AuthCheck >
78- }
79- />
80-
81- < Route
82- path = "/habits"
83- element = {
84- < AuthCheck isAuthenticated = { isAuthenticated } >
85- < Habits_page />
86- </ AuthCheck >
87- }
88- />
8981
90- < Route path = "/users/register" element = { < RegisterPage /> } />
91- < Route path = "/users/login" element = { < LoginPage /> } />
9282
93- < Route
94- path = "/tasks"
95- element = {
96- < AuthCheck isAuthenticated = { isAuthenticated } >
97- < TasksPage />
98- </ AuthCheck >
99- }
100- />
83+ const getPlatform = ( ) => {
84+ const ua = navigator . userAgent ;
10185
86+ let os = 'Unknown OS' ;
87+ if ( / W i n d o w s N T / . test ( ua ) ) os = 'Windows' ;
88+ else if ( / M a c i n t o s h / . test ( ua ) ) os = 'Mac' ;
89+ else if ( / A n d r o i d / . test ( ua ) ) os = 'Android' ;
90+ else if ( / i P h o n e | i P a d | i P o d / . test ( ua ) ) os = 'iOS' ;
91+ else if ( / L i n u x / . test ( ua ) ) os = 'Linux' ;
10292
93+ let browser = 'Unknown Browser' ;
94+ if ( / C h r o m e / . test ( ua ) && / M o b i l e / . test ( ua ) ) browser = 'Chrome Mobile' ;
95+ else if ( / C h r o m e / . test ( ua ) ) browser = 'Chrome' ;
96+ else if ( / F i r e f o x / . test ( ua ) ) browser = 'Firefox' ;
97+ else if ( / S a f a r i / . test ( ua ) && ! / C h r o m e / . test ( ua ) ) browser = 'Safari' ;
98+ else if ( / E d g / . test ( ua ) ) browser = 'Edge' ;
99+ else if ( / O P R / . test ( ua ) ) browser = 'Opera' ;
103100
104- < Route
105- path = "/premium"
106- element = {
107- < PremiumPage isAuthenticated = { isAuthenticated } />
108- }
109- />
110-
111- < Route
112- path = "/profile/statistics"
113- element = {
114- < AuthCheck isAuthenticated = { isAuthenticated } >
115- < HabitCalendar />
116- </ AuthCheck >
117- }
118- />
101+ return `${ browser } _${ os } ` ;
102+ } ;
103+
104+
105+
106+ const subscribePush = async ( ) => {
107+ if ( 'serviceWorker' in navigator ) {
108+ navigator . serviceWorker . register ( '/serviceWorker/ServiceWorker.js' )
109+ . then ( registration => console . log ( 'Service Worker зарегистрирован:' , registration ) )
110+ . catch ( err => console . error ( 'Не удалось зарегистрировать Service Worker:' , err ) ) ;
111+ }
112+
113+ try {
114+ console . log ( 'Дошлм епта' )
115+ const registration = await navigator . serviceWorker . ready ;
116+ const subscription = await registration . pushManager . subscribe ( {
117+ userVisibleOnly : true ,
118+ applicationServerKey : urlBase64ToUint8Array ( import . meta. env . VITE_PUSH_PUBLIC_KEY )
119+ } ) ;
120+
121+ const pushData = {
122+ platform : getPlatform ( ) ,
123+ endpoint : subscription . endpoint ,
124+ keys : subscription . toJSON ( ) . keys
125+ } ;
126+
127+ console . log ( pushData ) ;
128+
129+
130+ await fetch ( '/api/save/subscription/web' , {
131+ method : 'POST' ,
132+ headers : {
133+ 'Content-Type' : 'application/json' ,
134+ 'Authorization' : `Bearer ${ localStorage . getItem ( 'jwt' ) } `
135+ } ,
136+ body : JSON . stringify ( pushData )
137+ } ) ;
138+ } catch ( err ) {
139+ console . error ( 'Не удалось подписать на пуши:' , err ) ;
140+ }
141+ } ;
142+
143+
144+ if ( isAuthenticated === null ) return < Loading /> ;
145+
146+ return (
147+ < Routes >
148+ < Route path = "/" element = { < Lending /> } />
149+ < Route path = "/profile" element = { < AuthCheck isAuthenticated = { isAuthenticated } > < Profile /> </ AuthCheck > } />
150+ < Route path = "/pomodoro" element = { < AuthCheck isAuthenticated = { isAuthenticated } > < Pomodoro /> </ AuthCheck > } />
151+ < Route path = "/habits" element = { < AuthCheck isAuthenticated = { isAuthenticated } > < Habits_page /> </ AuthCheck > } />
152+ < Route path = "/tasks" element = { < AuthCheck isAuthenticated = { isAuthenticated } > < TasksPage /> </ AuthCheck > } />
153+ < Route path = "/profile/statistics" element = { < AuthCheck isAuthenticated = { isAuthenticated } > < HabitCalendar /> </ AuthCheck > } />
154+ < Route path = "/matric" element = { < AuthCheck isAuthenticated = { isAuthenticated } > < MatricPage /> </ AuthCheck > } />
155+ < Route path = "/premium" element = { < AuthCheck isAuthenticated = { isAuthenticated } > < PremiumPage isAuthenticated = { isAuthenticated } /> </ AuthCheck > } />
156+ < Route path = "/users/register" element = { < RegisterPage /> } />
157+ < Route path = "/users/login" element = { < LoginPage /> } />
119158 </ Routes >
120159 ) ;
121160} ;
@@ -127,3 +166,11 @@ const WrappedRouterDom = () => (
127166) ;
128167
129168export default WrappedRouterDom ;
169+
170+
171+ function urlBase64ToUint8Array ( base64String : string ) {
172+ const padding = '=' . repeat ( ( 4 - ( base64String . length % 4 ) ) % 4 ) ;
173+ const base64 = ( base64String + padding ) . replace ( / - / g, '+' ) . replace ( / _ / g, '/' ) ;
174+ const rawData = atob ( base64 ) ;
175+ return new Uint8Array ( [ ...rawData ] . map ( c => c . charCodeAt ( 0 ) ) ) ;
176+ }
0 commit comments