@@ -32,7 +32,11 @@ import TextArea from "antd/es/input/TextArea";
32
32
import { useSearchParams } from "next/navigation" ;
33
33
import { ProgrammingLanguageOptions } from "@/utils/SelectOptions" ;
34
34
import { isAuthenticated } from "@/utils/Auth" ;
35
- import { redirect } from 'next/navigation' ;
35
+ import {
36
+ ValidateUser ,
37
+ VerifyTokenResponseType ,
38
+ } from "../../services/user" ;
39
+ import { useRouter } from 'next/navigation' ;
36
40
37
41
export default function Home ( ) {
38
42
const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ; // Store the states related to table's loading
@@ -60,12 +64,31 @@ export default function Home() {
60
64
const [ description , setDescription ] = useState < string | undefined > ( undefined ) ;
61
65
const [ selectedItem , setSelectedItem ] = useState ( "python" ) ; // State to hold the selected language item
62
66
67
+ // used to check if user JWT is verified
68
+
69
+ const [ userId , setUserId ] = useState < string | undefined > ( undefined ) ;
70
+ const [ email , setEmail ] = useState < string | undefined > ( undefined ) ;
71
+ const [ username , setUsername ] = useState < string | undefined > ( undefined ) ;
72
+ const [ isAdmin , setIsAdmin ] = useState < boolean | undefined > ( undefined ) ;
73
+
74
+ const router = useRouter ( ) ;
75
+
63
76
useLayoutEffect ( ( ) => {
64
- const isAuth = isAuthenticated ;
65
- if ( ! isAuth ) {
66
- redirect ( "/login" )
67
- }
68
- } , [ ] )
77
+ var isAuth = false ;
78
+
79
+ ValidateUser ( ) . then ( ( data : VerifyTokenResponseType ) => {
80
+ setUserId ( data . data . id ) ;
81
+ setEmail ( data . data . email ) ;
82
+ setUsername ( data . data . username ) ;
83
+ setIsAdmin ( data . data . isAdmin ) ;
84
+ isAuth = true ;
85
+ } ) . finally ( ( ) => {
86
+ if ( ! isAuth ) {
87
+ // cannot verify
88
+ router . push ( '/login' ) ; // Client-side redirect using router.push
89
+ }
90
+ } ) ;
91
+ } , [ router ] )
69
92
70
93
// When code editor page is initialised, fetch the particular question, and display in code editor
71
94
useEffect ( ( ) => {
0 commit comments