|
10 | 10 |
|
11 | 11 | const api: string = process.env.REACT_APP_API_BASE_URL as string; |
12 | 12 |
|
| 13 | +const isAuthEnabled = (): boolean => |
| 14 | + process.env.REACT_APP_AUTH_ENABLED?.toLowerCase() !== 'false'; |
| 15 | + |
| 16 | +const isUsableToken = (token: string | null): token is string => { |
| 17 | + if (!token) return false; |
| 18 | + const value = token.trim(); |
| 19 | + if (!value) return false; |
| 20 | + if (value.toLowerCase() === 'null' || value.toLowerCase() === 'undefined') { |
| 21 | + return false; |
| 22 | + } |
| 23 | + if (value.startsWith('APP_')) return false; |
| 24 | + return true; |
| 25 | +}; |
| 26 | + |
13 | 27 | interface FetchResponse<T> { |
14 | 28 | data: T | null; |
15 | 29 | status: number; |
@@ -73,11 +87,14 @@ const fetchWithAuth = async <T>( |
73 | 87 | const token = localStorage.getItem('token'); |
74 | 88 |
|
75 | 89 | const headers: Record<string, string> = { |
76 | | - 'Authorization': `Bearer ${token}`, |
77 | 90 | 'Accept': 'application/json', |
78 | 91 | 'Cache-Control': 'no-cache', |
79 | 92 | }; |
80 | 93 |
|
| 94 | + if (isAuthEnabled() && isUsableToken(token)) { |
| 95 | + headers['Authorization'] = `Bearer ${token}`; |
| 96 | + } |
| 97 | + |
81 | 98 | let processedBody: BodyInit | null = null; |
82 | 99 | if (body instanceof FormData) { |
83 | 100 | processedBody = body; |
@@ -132,12 +149,15 @@ const fetchHeadersWithAuth = async <T>( |
132 | 149 | const token = localStorage.getItem('token'); |
133 | 150 |
|
134 | 151 | const headers: Record<string, string> = { |
135 | | - 'Authorization': `Bearer ${token}`, |
136 | 152 | 'Content-Type': 'application/json', |
137 | 153 | 'Accept': 'application/json', |
138 | 154 | 'Cache-Control': 'no-cache', |
139 | 155 | }; |
140 | 156 |
|
| 157 | + if (isAuthEnabled() && isUsableToken(token)) { |
| 158 | + headers['Authorization'] = `Bearer ${token}`; |
| 159 | + } |
| 160 | + |
141 | 161 | if (body instanceof FormData) { |
142 | 162 | delete headers['Content-Type']; |
143 | 163 | } else { |
|
0 commit comments