All files / atlas/src/hooks useAuthorizedRequests.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                                                           
// import axios, { AxiosError, AxiosPromise, AxiosRequestConfig, AxiosResponse } from "axios"
// import { API_URL, reportAxiosError } from "../globals";
// import { authStore } from "../stores/AuthStore";
 
// export const useAuthorizedRequests = () => {
//     const sendAuthorizedRequestAsync = async (requestConfig: AxiosRequestConfig, errorMessage: string):  => {
//         try {
//             const response = fetch(requestConfig);
//             return response;
//         } catch (error) {
//             if (error.response.status == 401) {
//                 console.info("An authorized request failed, trying to refresh access token...")
 
//                 if (authStore.refreshToken) {
//                     const tokenData = new URLSearchParams();
//                     tokenData.append('grant_type', 'refresh_token');
//                     tokenData.append('refresh_token', authStore.refreshToken);
//                     tokenData.append('client_id', 'atlas.mobile');
 
//                     try {
//                         const { data: refreshResponseData } = await axios.post(API_URL + "/o/token/", tokenData, {
//                             headers: { 'Content-Type': "application/x-www-form-urlencoded" }
//                         });   
 
//                         await authStore.setRefreshTokenAsync(refreshResponseData.refresh_token);
//                         await authStore.setAccessTokenAsync(refreshResponseData.access_token);
 
//                         console.info('Successfully refreshed access token, re-attempting initial request...')
 
//                         try {
//                             const response = await axios(requestConfig);  
//                         } catch (error) {
//                             reportAxiosError("Something went wrong when re-attempting initial request", error);                
//                         } 
//                     } catch (error) {
//                         reportAxiosError("Error when trying to refresh access token", error);            
//                     }
//                 }
 
//                 // no refresh token, user will be directed back to login
//                 else {
//                     await authStore.setAccessTokenAsync(null);
//                     await authStore.setRefreshTokenAsync(null);
//                     return
//                 }
//             }
//             else {
//                 reportAxiosError(errorMessage, error);
//             }
//         }
//     }
 
//     return { sendAuthorizedRequestAsync }
// }