no more 401 error for publicly browsing user

This commit is contained in:
jackiettran
2025-10-07 11:43:05 -04:00
parent 9a9e96d007
commit 299522b3a6
8 changed files with 1272 additions and 249 deletions

View File

@@ -230,7 +230,7 @@ const AuthModal: React.FC<AuthModalProps> = ({
disabled={loading}
>
<i className="bi bi-google me-2"></i>
Sign in with Google
Continue with Google
</button>
<div className="text-center mt-3">

View File

@@ -44,14 +44,16 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
const checkAuth = async () => {
try {
// The axios interceptor will automatically handle token refresh if needed
const response = await userAPI.getProfile();
setUser(response.data);
// Use the status endpoint which returns 200 for both authenticated and unauthenticated states
const response = await authAPI.getStatus();
if (response.data.authenticated) {
setUser(response.data.user);
} else {
setUser(null);
}
} catch (error: any) {
// If we get here, either:
// 1. User is not logged in (expected for public browsing)
// 2. Token refresh failed (user needs to login again)
// In both cases, silently set user to null without logging errors
// If we get here, there's a network or server error
setUser(null);
}
};

View File

@@ -157,6 +157,7 @@ export const authAPI = {
logout: () => api.post("/auth/logout"),
refresh: () => api.post("/auth/refresh"),
getCSRFToken: () => api.get("/auth/csrf-token"),
getStatus: () => api.get("/auth/status"),
};
export const userAPI = {
@@ -280,23 +281,4 @@ export const conditionCheckAPI = {
getAvailableChecks: () => api.get("/condition-checks"),
};
export const notificationAPI = {
getNotifications: (params?: { limit?: number; page?: number }) =>
api.get("/notifications", { params }),
getUnreadCount: () => api.get("/notifications/unread-count"),
markAsRead: (notificationId: string) =>
api.patch(`/notifications/${notificationId}/read`),
markAllAsRead: () => api.patch("/notifications/mark-all-read"),
// Development endpoints
createTestNotification: (data: {
type?: string;
title: string;
message: string;
metadata?: any;
}) => api.post("/notifications/test", data),
triggerConditionReminders: () =>
api.post("/notifications/test/condition-reminders"),
cleanupExpired: () => api.post("/notifications/test/cleanup-expired"),
};
export default api;