@@ -28,6 +28,7 @@ interface AuthResponse {
2828
2929class ApiService {
3030 private baseURL : string ;
31+ private onUnauthorized ?: ( ) => Promise < void > ;
3132
3233 constructor ( ) {
3334 this . baseURL = Config . API_BASE_URL ;
@@ -37,6 +38,10 @@ class ApiService {
3738 this . setupDefaultTimeout ( ) ;
3839 }
3940
41+ setUnauthorizedHandler ( handler : ( ) => Promise < void > ) {
42+ this . onUnauthorized = handler ;
43+ }
44+
4045 // Setup default timeout for all requests
4146 private setupDefaultTimeout ( ) {
4247 axios . defaults . timeout = 15000 ; // 15 second timeout
@@ -68,10 +73,10 @@ class ApiService {
6873 if ( token && isApiRequest ) {
6974 config . headers . Authorization = `Bearer ${ token } ` ;
7075 console . log ( `✅ Adding auth token to request: ${ config . url } ` ) ;
71- console . log ( `🔑 Token preview: ${ token . substring ( 0 , 20 ) } ...` ) ;
7276 } else if ( isApiRequest ) {
7377 console . log ( `❌ No token found for API request: ${ config . url } ` ) ;
7478 }
79+
7580 return config ;
7681 } ,
7782 ( error ) => {
@@ -83,10 +88,12 @@ class ApiService {
8388 axios . interceptors . response . use (
8489 ( response ) => response ,
8590 async ( error ) => {
86- if ( error . response ?. status === 401 ) {
91+ if ( error . response ?. status === 401 || error . response ?. status === 500 || error . response ?. status === 403 ) {
8792 // Token expired or invalid
8893 await this . clearToken ( ) ;
89- // Could trigger logout here if needed
94+ if ( this . onUnauthorized ) {
95+ await this . onUnauthorized ( ) ;
96+ }
9097 }
9198 return Promise . reject ( error ) ;
9299 }
@@ -139,10 +146,6 @@ class ApiService {
139146 async register ( userData : RegisterRequest ) : Promise < AuthResponse > {
140147 const response = await axios . post ( `${ this . baseURL } /auth/register` , userData ) ;
141148 const authData = response . data ;
142- console . log ( 'Register response:' , {
143- token_preview : authData . token ?. access_token ?. substring ( 0 , 20 ) + '...' ,
144- user : authData . user
145- } ) ;
146149 await this . setToken ( authData . token . access_token ) ;
147150 console . log ( 'Token stored successfully' ) ;
148151 return authData ;
0 commit comments