1+ // auth-context.tsx
12"use client" ;
23
3- import { useState , useEffect , useRef } from "react" ;
4+ import React , { createContext , useContext , useEffect , useRef , useState } from "react" ;
45import { useLazyQuery } from "@apollo/client" ;
56import { CHECK_TOKEN_QUERY } from "@/graphql/request" ;
6- import { LocalStore } from "@/lib/storage" ;
77import { LoadingPage } from "@/components/global-loading" ;
88
9- interface AuthProviderProps {
10- children : React . ReactNode ;
9+ interface AuthContextValue {
10+ isAuthorized : boolean ;
11+ isChecking : boolean ;
12+ setIsAuthorized : React . Dispatch < React . SetStateAction < boolean > > ;
1113}
1214
13- export const AuthProvider = ( { children } : AuthProviderProps ) => {
15+ const AuthContext = createContext < AuthContextValue > ( {
16+ isAuthorized : false ,
17+ isChecking : false ,
18+ setIsAuthorized : ( ) => { } ,
19+ } ) ;
20+
21+ export const useAuthContext = ( ) => useContext ( AuthContext ) ;
22+
23+ export function AuthProvider ( { children } : { children : React . ReactNode } ) {
1424 const [ isAuthorized , setIsAuthorized ] = useState ( false ) ;
1525 const [ isChecking , setIsChecking ] = useState ( true ) ;
16- const [ showSignInModal , setShowSignInModal ] = useState ( false ) ;
1726
1827 const [ checkToken ] = useLazyQuery ( CHECK_TOKEN_QUERY ) ;
19- const timeoutRef = useRef < NodeJS . Timeout > ( ) ;
28+ const timeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
2029
2130 useEffect ( ( ) => {
2231 let isMounted = true ;
2332
2433 async function validateToken ( ) {
2534 setIsChecking ( true ) ;
2635
27- const token = localStorage . getItem ( LocalStore . accessToken ) ;
36+ // If you want to store the token in sessionStorage, do:
37+ // const token = sessionStorage.getItem("accessToken");
38+ // Otherwise, if you still prefer localStorage:
39+ const token = sessionStorage . getItem ( "accessToken" ) ;
40+
2841 if ( ! token ) {
29- // No token => not authorized, but don't block the page
42+ // No token => user is not authorized
3043 if ( isMounted ) {
3144 setIsAuthorized ( false ) ;
3245 setIsChecking ( false ) ;
33- // Optionally show sign-in modal:
34- setShowSignInModal ( true ) ;
3546 }
3647 return ;
3748 }
@@ -40,20 +51,18 @@ export const AuthProvider = ({ children }: AuthProviderProps) => {
4051 timeoutRef . current = setTimeout ( ( ) => {
4152 if ( isMounted ) {
4253 console . error ( "Token validation timeout" ) ;
43- localStorage . removeItem ( LocalStore . accessToken ) ;
54+ sessionStorage . removeItem ( " accessToken" ) ;
4455 setIsAuthorized ( false ) ;
4556 setIsChecking ( false ) ;
46- setShowSignInModal ( true ) ;
4757 }
4858 } , 5000 ) ;
4959
5060 try {
5161 const { data } = await checkToken ( { variables : { input : { token } } } ) ;
5262 if ( isMounted ) {
5363 if ( ! data ?. checkToken ) {
54- localStorage . removeItem ( LocalStore . accessToken ) ;
64+ sessionStorage . removeItem ( " accessToken" ) ;
5565 setIsAuthorized ( false ) ;
56- setShowSignInModal ( true ) ;
5766 } else {
5867 console . log ( "Token valid" ) ;
5968 setIsAuthorized ( true ) ;
@@ -62,9 +71,8 @@ export const AuthProvider = ({ children }: AuthProviderProps) => {
6271 } catch ( error ) {
6372 if ( isMounted ) {
6473 console . error ( "Token validation error:" , error ) ;
65- localStorage . removeItem ( LocalStore . accessToken ) ;
74+ sessionStorage . removeItem ( " accessToken" ) ;
6675 setIsAuthorized ( false ) ;
67- setShowSignInModal ( true ) ;
6876 }
6977 } finally {
7078 if ( timeoutRef . current ) {
@@ -86,16 +94,14 @@ export const AuthProvider = ({ children }: AuthProviderProps) => {
8694 } ;
8795 } , [ checkToken ] ) ;
8896
97+ // While checking token, show loading screen
8998 if ( isChecking ) {
9099 return < LoadingPage /> ;
91100 }
92101
93- // Always render main page, authorized or not
94102 return (
95- < >
103+ < AuthContext . Provider value = { { isAuthorized , isChecking , setIsAuthorized } } >
96104 { children }
97- { /* Show sign-in modal if unauthorized */ }
98- { /* <SignInModal isOpen={showSignInModal} onClose={() => setShowSignInModal(false)} /> */ }
99- </ >
105+ </ AuthContext . Provider >
100106 ) ;
101- } ;
107+ }
0 commit comments