From 60694258f26893a71c70fff0381154b68878089e Mon Sep 17 00:00:00 2001 From: mohadeseh safari Date: Thu, 17 Apr 2025 16:19:22 -0400 Subject: [PATCH] Add backend reachability check method --- src/Utils/NetworkService.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Utils/NetworkService.js b/src/Utils/NetworkService.js index 7c4ffba9f..7ac76f89e 100644 --- a/src/Utils/NetworkService.js +++ b/src/Utils/NetworkService.js @@ -504,6 +504,41 @@ class NetworkService { return this.axiosInstance.get("/auth/users/superadmin"); } + /** + * ************************************ + * Checks if the backend server is reachable + * ************************************ + * + * @async + * @returns {Promise} True if backend is reachable, false otherwise + */ + async checkBackendReachability() { + try { + // Create a temporary instance that doesn't follow redirects and has a short timeout + const tempInstance = axios.create({ + timeout: 5000, + maxRedirects: 0, + baseURL: this.axiosInstance.defaults.baseURL, + }); + + // Make a simple GET request to the root endpoint + await tempInstance.get('/', { + headers: { + Authorization: undefined // Prevent sending the auth token + } + }); + + return true; + } catch (error) { + // If we get a successful HTTP status or redirect, the backend is reachable + if (error.response) { + return true; // Server responded with any status code, so it's reachable + } + console.error("Backend connection failed:", error); + return false; + } + } + /** * ************************************ * Get all users