Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: token refreshing

Brad Barbin8a075078c5d9
diff --git a/apps/carbon/app/routes/x+/_layout.tsx b/apps/carbon/app/routes/x+/_layout.tsx
index b0afac0f0d..a85be22ae3 100644
--- a/apps/carbon/app/routes/x+/_layout.tsx
+++ b/apps/carbon/app/routes/x+/_layout.tsx
@@ -30,7 +30,8 @@ export const shouldRevalidate: ShouldRevalidateFunction = ({
}) => {
if (
currentUrl.pathname.startsWith("/x/settings") ||
- currentUrl.pathname.startsWith("/x/users")
+ currentUrl.pathname.startsWith("/x/users") ||
+ currentUrl.pathname.startsWith("/refresh-session")
) {
return true;
}
diff --git a/packages/auth/src/lib/supabase/provider.tsx b/packages/auth/src/lib/supabase/provider.tsx
index 6dfa06342a..b29a44903f 100644
--- a/packages/auth/src/lib/supabase/provider.tsx
+++ b/packages/auth/src/lib/supabase/provider.tsx
@@ -1,5 +1,5 @@
import type { Database } from "@carbon/database";
-import { useInterval } from "@carbon/react";
+import { useInterval, useMount } from "@carbon/react";
import { isBrowser } from "@carbon/utils";
import { useFetcher } from "@remix-run/react";
import type { SupabaseClient } from "@supabase/supabase-js";
@@ -30,16 +30,26 @@ export const CarbonProvider = ({
}>) => {
const { accessToken, refreshToken, expiresAt } = session;
const initialLoad = useRef(true);
- const authenticatedClientLoaded = useRef(false);
+
const [carbon, setCarbon] = useState<SupabaseClient<Database> | undefined>(
- () => {
- // prevents server side initial state
- // init a default anonymous client in browser until we have an auth token
- if (isBrowser) return getCarbon();
- }
+ undefined
);
const refresh = useFetcher<{}>();
+ useEffect(() => {
+ if (!carbon || !accessToken || !refreshToken) return;
+
+ carbon.auth.setSession({
+ access_token: accessToken,
+ refresh_token: refreshToken,
+ });
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [accessToken, refreshToken]);
+
+ useMount(() => {
+ setCarbon(getCarbon(accessToken));
+ });
+
useEffect(() => {
const handleFocus = () => {
refresh.submit(null, {
@@ -64,6 +74,9 @@ export const CarbonProvider = ({
const shouldRefresh = expiresAt - 60 * 10 < Date.now() / 1000;
if (!initialLoad.current && shouldRefresh) {
+ carbon.auth.refreshSession({
+ refresh_token: refreshToken,
+ });
refresh.submit(null, {
method: "post",
action: path.to.refreshSession,
@@ -73,21 +86,6 @@ export const CarbonProvider = ({
initialLoad.current = false;
}, 15000);
- if (isBrowser && accessToken && !authenticatedClientLoaded.current) {
- // recreate a carbon client to force provider's consumer to rerender
- setCarbon(getCarbon(accessToken));
- authenticatedClientLoaded.current = true;
- }
-
- useEffect(() => {
- if (!carbon || !accessToken || !refreshToken) return;
-
- carbon.auth.setSession({
- access_token: accessToken,
- refresh_token: refreshToken,
- });
- }, [accessToken, refreshToken, carbon]);
-
const value = useMemo(() => ({ carbon, accessToken }), [carbon, accessToken]);
return (