fix: security fix
This commit is contained in:
@@ -31,7 +31,7 @@ export const authApi = {
|
|||||||
payload,
|
payload,
|
||||||
{ ...withTenantHeader(options?.tenantId), successMessage: "Account created", errorMessage: "Failed to create account" }
|
{ ...withTenantHeader(options?.tenantId), successMessage: "Account created", errorMessage: "Failed to create account" }
|
||||||
),
|
),
|
||||||
logout: () => apiClient.post<LogoutResponse>("/api/auth/logout", { successMessage: "Signed out", errorMessage: "Failed to sign out" }),
|
logout: () => apiClient.post<LogoutResponse>("/api/auth/logout", null, { successMessage: "Signed out", errorMessage: "Failed to sign out" }),
|
||||||
me: () => apiClient.get<AuthUser>("/api/auth/me"),
|
me: () => apiClient.get<AuthUser>("/api/auth/me"),
|
||||||
resetPassword: (oldPassword: string, newPassword: string) =>
|
resetPassword: (oldPassword: string, newPassword: string) =>
|
||||||
apiClient.post<{ message: string }>("/api/auth/reset-password", {
|
apiClient.post<{ message: string }>("/api/auth/reset-password", {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export default function SignInForm() {
|
|||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [errorMessage, setErrorMessage] = useState("");
|
const [errorMessage, setErrorMessage] = useState("");
|
||||||
|
|
||||||
const { login } = useAuth(); // Retrieve login function from context
|
const { login } = useAuth();
|
||||||
|
|
||||||
const handleSignIn = async (event: React.FormEvent) => {
|
const handleSignIn = async (event: React.FormEvent) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@@ -27,10 +27,8 @@ export default function SignInForm() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Use the context login function to ensure state is updated
|
|
||||||
await login(payload, isChecked);
|
await login(payload, isChecked);
|
||||||
|
|
||||||
// Redirect to the dashboard after successful signin.
|
|
||||||
navigate("/dashboard");
|
navigate("/dashboard");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message =
|
const message =
|
||||||
|
|||||||
@@ -70,6 +70,10 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
|
try {
|
||||||
|
await authApi.logout();
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
clearAuthCookies();
|
clearAuthCookies();
|
||||||
setUser(null);
|
setUser(null);
|
||||||
window.location.href = "/signin";
|
window.location.href = "/signin";
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
import { toast } from "react-toastify";
|
import { toast } from "react-toastify";
|
||||||
import { API_BASE_URL } from "../constant";
|
import { API_BASE_URL } from "../constant";
|
||||||
import { AUTH_COOKIE_KEYS, clearAuthCookies, getCookie } from "./authCookies";
|
import { AUTH_COOKIE_KEYS, clearAuthCookies, getCookie, setAuthCookies } from "./authCookies";
|
||||||
import { isTokenExpired } from "./jwt";
|
import { isTokenExpired } from "./jwt";
|
||||||
|
|
||||||
type ApiRequestOptions = Omit<RequestInit, "body"> & {
|
type ApiRequestOptions = Omit<RequestInit, "body"> & {
|
||||||
body?: unknown;
|
body?: unknown;
|
||||||
toast?: boolean;
|
toast?: boolean;
|
||||||
/** Optional custom success message to show in a toast */
|
|
||||||
successMessage?: string;
|
successMessage?: string;
|
||||||
/** Optional custom error message to show in a toast */
|
|
||||||
errorMessage?: string;
|
errorMessage?: string;
|
||||||
/** If true, silences all toast notifications for this request */
|
|
||||||
silent?: boolean;
|
silent?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -39,7 +36,9 @@ const refreshAccessToken = async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!res.ok) throw new Error("Refresh failed");
|
if (!res.ok) throw new Error("Refresh failed");
|
||||||
return res.json();
|
const data = await res.json();
|
||||||
|
setAuthCookies(data);
|
||||||
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
const request = async <T>(
|
const request = async <T>(
|
||||||
@@ -72,6 +71,9 @@ const request = async <T>(
|
|||||||
await new Promise<void>((resolve) => refreshQueue.push(resolve));
|
await new Promise<void>((resolve) => refreshQueue.push(resolve));
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
|
isRefreshing = false;
|
||||||
|
refreshQueue.forEach((cb) => cb());
|
||||||
|
refreshQueue = [];
|
||||||
hardLogout();
|
hardLogout();
|
||||||
throw new Error("Session expired");
|
throw new Error("Session expired");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user