Skip to content

Commit

Permalink
Handle 401 errors by redirecting to the sign-in page in usePrompts an…
Browse files Browse the repository at this point in the history
…d apiClient
  • Loading branch information
Precious-Macaulay committed Nov 15, 2024
1 parent 622f504 commit c91359d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/hooks/usePrompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useCallback, useRef } from "react";
import { useStore } from "@/store/promptStore";
import { fetchGalleryPrompts, fetchUserPrompts } from "@/api/prompts";
import type { Prompt } from "../types";
import { AxiosError } from "axios";


export type PromptType = "user" | "gallery" | null;
Expand Down Expand Up @@ -42,8 +43,11 @@ const usePrompts = (promptId?: number): UsePromptsResult => {
addUserPrompts(prompts);
}
return true;
} catch (error) {
} catch (error: unknown) {
console.error(`Error fetching ${isGallery ? "gallery" : "user"} prompts:`, error);
if ((error as AxiosError).response?.status === 401) {
window.location.href = "https://www.astria.ai/users/sign_in";
}
return false;
}
}, [galleryOffset, userOffset, limit, addGalleryPrompts, addUserPrompts]);
Expand Down
8 changes: 1 addition & 7 deletions src/services/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,8 @@ export const post = async <T>(url: string, data: object): Promise<ApiResponse<T>

const handleApiError = (error: AxiosError): void => {
if (error.response?.status === 401) {
window.location.href = 'https://astria.ai/login';
window.location.href = 'https://www.astria.ai/users/sign_in';
return;
} else if (error.response) {
console.error('API Error:', error.response.status, error.response.data);
} else if (error.request) {
console.error('No response received:', error.request);
} else {
console.error('Error:', error.message);
}
};

Expand Down

0 comments on commit c91359d

Please sign in to comment.