@@ -7,13 +7,17 @@ import { Context } from '../context';
7
7
import { useEffect , useState } from 'react' ;
8
8
import request from '@/utils/request' ;
9
9
import { Analytics } from "@vercel/analytics/react"
10
+ import { usePathname , useRouter } from 'next/navigation' ;
11
+
10
12
11
13
export default function App ( { Component, pageProps } ) {
12
14
const [ username , setUsername ] = useState ( '' ) ;
13
15
const [ role , setRole ] = useState ( '' ) ;
14
16
const [ accountType , setAccountType ] = useState ( '' ) ;
15
17
const [ profilePic , setProfilePic ] = useState ( '' ) ;
16
18
const [ points , setPoints ] = useState ( 0 ) ;
19
+ const path = usePathname ( ) ;
20
+ const router = useRouter ( ) ;
17
21
18
22
const appState = {
19
23
role, setRole,
@@ -26,7 +30,14 @@ export default function App({ Component, pageProps }) {
26
30
const fetchUser = async ( ) => {
27
31
const url = process . env . NEXT_PUBLIC_API_URL + "/account" ;
28
32
const user = await request ( url , "GET" , null ) ;
29
- console . log ( user ) ;
33
+
34
+ if ( user && user . error && user . error === "Not authorized" ) {
35
+ const pathNames = [ "/" , "/login" , "/careers" , "/register" ,
36
+ "/onboarding" , "/forgot-password" , "/education" , "/userrs" ,
37
+ "/privacy-policy" , "/404" , "/terms-of-service" , "/learn" ] ;
38
+ if ( ! pathNames . includes ( path ) ) router . push ( "/login" ) ;
39
+ }
40
+
30
41
if ( user && user . username ) {
31
42
setRole ( user . role ) ;
32
43
setUsername ( user . username ) ;
0 commit comments