@agent-platform/ui 0.0.13 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +52 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -870,12 +870,41 @@ function AgentContainer({ className }) {
|
|
|
870
870
|
}
|
|
871
871
|
);
|
|
872
872
|
}
|
|
873
|
+
var STORAGE_KEY = "agent_auth_token_v1";
|
|
874
|
+
function saveToken(token) {
|
|
875
|
+
if (typeof window === "undefined") return;
|
|
876
|
+
try {
|
|
877
|
+
window.localStorage.setItem(STORAGE_KEY, token);
|
|
878
|
+
} catch {
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
function loadToken() {
|
|
882
|
+
if (typeof window === "undefined") return null;
|
|
883
|
+
try {
|
|
884
|
+
return window.localStorage.getItem(STORAGE_KEY);
|
|
885
|
+
} catch {
|
|
886
|
+
return null;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
function clearToken() {
|
|
890
|
+
if (typeof window === "undefined") return;
|
|
891
|
+
try {
|
|
892
|
+
window.localStorage.removeItem(STORAGE_KEY);
|
|
893
|
+
} catch {
|
|
894
|
+
}
|
|
895
|
+
}
|
|
873
896
|
function createAgentAuthClient(agentServerUrl) {
|
|
874
897
|
const baseURL = agentServerUrl.replace(/\/api\/agent\/?$/, "");
|
|
875
898
|
return createAuthClient({
|
|
876
899
|
baseURL,
|
|
877
900
|
basePath: "/api/auth",
|
|
878
|
-
plugins: [anonymousClient()]
|
|
901
|
+
plugins: [anonymousClient()],
|
|
902
|
+
fetchOptions: {
|
|
903
|
+
auth: {
|
|
904
|
+
type: "Bearer",
|
|
905
|
+
token: () => loadToken() ?? void 0
|
|
906
|
+
}
|
|
907
|
+
}
|
|
879
908
|
});
|
|
880
909
|
}
|
|
881
910
|
|
|
@@ -891,15 +920,31 @@ function useAgentAuth(agentServerUrl) {
|
|
|
891
920
|
let timeoutId;
|
|
892
921
|
async function init() {
|
|
893
922
|
try {
|
|
894
|
-
|
|
923
|
+
let sessionResult;
|
|
924
|
+
try {
|
|
925
|
+
sessionResult = await client.getSession();
|
|
926
|
+
} catch {
|
|
927
|
+
const cachedToken = loadToken();
|
|
928
|
+
if (cachedToken) {
|
|
929
|
+
tokenRef.current = cachedToken;
|
|
930
|
+
setError(null);
|
|
931
|
+
setIsReady(true);
|
|
932
|
+
return;
|
|
933
|
+
}
|
|
934
|
+
sessionResult = { data: null, error: null };
|
|
935
|
+
}
|
|
895
936
|
if (cancelled) return;
|
|
896
937
|
if (sessionResult.data?.session) {
|
|
897
|
-
|
|
938
|
+
const token = sessionResult.data.session.token;
|
|
939
|
+
tokenRef.current = token;
|
|
940
|
+
saveToken(token);
|
|
898
941
|
setUserId(sessionResult.data.user.id);
|
|
899
942
|
setError(null);
|
|
900
943
|
setIsReady(true);
|
|
901
944
|
return;
|
|
902
945
|
}
|
|
946
|
+
clearToken();
|
|
947
|
+
if (cancelled) return;
|
|
903
948
|
const signInResult = await client.signIn.anonymous();
|
|
904
949
|
if (cancelled) return;
|
|
905
950
|
if (signInResult.error) {
|
|
@@ -908,7 +953,9 @@ function useAgentAuth(agentServerUrl) {
|
|
|
908
953
|
return;
|
|
909
954
|
}
|
|
910
955
|
if (signInResult.data) {
|
|
911
|
-
|
|
956
|
+
const token = signInResult.data.token ?? "";
|
|
957
|
+
tokenRef.current = token;
|
|
958
|
+
if (token) saveToken(token);
|
|
912
959
|
setUserId(signInResult.data.user?.id ?? null);
|
|
913
960
|
}
|
|
914
961
|
setError(null);
|
|
@@ -942,6 +989,7 @@ function useAgentAuth(agentServerUrl) {
|
|
|
942
989
|
const sessionResult = await client.getSession();
|
|
943
990
|
if (sessionResult.data?.session?.token) {
|
|
944
991
|
tokenRef.current = sessionResult.data.session.token;
|
|
992
|
+
saveToken(sessionResult.data.session.token);
|
|
945
993
|
}
|
|
946
994
|
} catch {
|
|
947
995
|
}
|