@block-auth.io/core 0.4.2 → 0.5.1
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.cjs +217 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +69 -6
- package/dist/index.d.ts +69 -6
- package/dist/index.js +217 -51
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -13,8 +13,70 @@ var __export = (target, all) => {
|
|
|
13
13
|
for (var name in all)
|
|
14
14
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
15
|
};
|
|
16
|
+
|
|
17
|
+
// src/utils/apiBridge.ts
|
|
18
|
+
var DEFAULT_API_URL = "https://api.block-auth.io";
|
|
19
|
+
function buildApiUrl(path, apiUrl) {
|
|
20
|
+
const base = (apiUrl || DEFAULT_API_URL).replace(/\/+$/, "");
|
|
21
|
+
return `${base}${path}`;
|
|
22
|
+
}
|
|
23
|
+
async function bridgeSessionCookie(token, apiUrl) {
|
|
24
|
+
if (!token || typeof fetch === "undefined") return false;
|
|
25
|
+
try {
|
|
26
|
+
const response = await fetch(buildApiUrl("/auth/session", apiUrl), {
|
|
27
|
+
method: "POST",
|
|
28
|
+
credentials: "include",
|
|
29
|
+
headers: { "Content-Type": "application/json" },
|
|
30
|
+
body: JSON.stringify({ token })
|
|
31
|
+
});
|
|
32
|
+
return response.ok;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.warn("[block-auth] failed to bridge session cookie", error);
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async function clearSessionCookie(apiUrl) {
|
|
39
|
+
if (typeof fetch === "undefined") return false;
|
|
40
|
+
try {
|
|
41
|
+
const response = await fetch(buildApiUrl("/auth/logout", apiUrl), {
|
|
42
|
+
method: "POST",
|
|
43
|
+
credentials: "include"
|
|
44
|
+
});
|
|
45
|
+
return response.ok;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.warn("[block-auth] failed to clear session cookie", error);
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function fetchWsTicket(apiUrl) {
|
|
52
|
+
if (typeof fetch === "undefined") return null;
|
|
53
|
+
try {
|
|
54
|
+
const response = await fetch(buildApiUrl("/auth/ws-ticket", apiUrl), {
|
|
55
|
+
method: "GET",
|
|
56
|
+
credentials: "include"
|
|
57
|
+
});
|
|
58
|
+
if (!response.ok) return null;
|
|
59
|
+
const data = await response.json();
|
|
60
|
+
return data?.ticket || null;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.warn("[block-auth] failed to fetch ws ticket", error);
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/protocol/WsClient.ts
|
|
16
68
|
var WsClient = class {
|
|
17
|
-
|
|
69
|
+
/**
|
|
70
|
+
* @param url - the wss:// endpoint to connect to.
|
|
71
|
+
* @param reconnect - allow socket.io to auto-reconnect.
|
|
72
|
+
* @param apiUrl - backend base url used to fetch the short-lived WS
|
|
73
|
+
* connect ticket (GET /auth/ws-ticket) right before opening the socket.
|
|
74
|
+
* Defaults to https://api.block-auth.io. The cookie set by
|
|
75
|
+
* POST /auth/session is host-only to the API origin, so it can't reach
|
|
76
|
+
* this (different-host) WS endpoint directly — the ticket is what
|
|
77
|
+
* actually authenticates the connection now.
|
|
78
|
+
*/
|
|
79
|
+
constructor(url = "", reconnect = false, apiUrl) {
|
|
18
80
|
this._uiConfig = {};
|
|
19
81
|
this._accessToken = "";
|
|
20
82
|
this._refreshToken = "";
|
|
@@ -24,8 +86,13 @@ var WsClient = class {
|
|
|
24
86
|
this._logs = [];
|
|
25
87
|
this._kycIssuerByAddress = {};
|
|
26
88
|
this._wsUrl = url || "wss://wss.block-auth.io/";
|
|
89
|
+
this._apiUrl = apiUrl;
|
|
27
90
|
this._wsConfig = {
|
|
28
91
|
transports: ["websocket"],
|
|
92
|
+
// No longer load-bearing for auth (the session cookie can't reach
|
|
93
|
+
// this host), kept as a harmless default; see the ticket-based `auth`
|
|
94
|
+
// option added in connect() below for the auth that actually matters.
|
|
95
|
+
withCredentials: true,
|
|
29
96
|
reconnection: reconnect,
|
|
30
97
|
reconnectionDelay: 100,
|
|
31
98
|
reconnectionDelayMax: 3e3,
|
|
@@ -50,6 +117,9 @@ var WsClient = class {
|
|
|
50
117
|
*/
|
|
51
118
|
async connect(url = "", waitMs = 5e3, retries = 3) {
|
|
52
119
|
const _url = url || this._wsUrl;
|
|
120
|
+
this._wsConfig.auth = (done) => {
|
|
121
|
+
fetchWsTicket(this._apiUrl).then((ticket) => done(ticket ? { ticket } : {})).catch(() => done({}));
|
|
122
|
+
};
|
|
53
123
|
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
54
124
|
if (this._ws) {
|
|
55
125
|
this._ws.disconnect();
|
|
@@ -103,6 +173,24 @@ var WsClient = class {
|
|
|
103
173
|
this.on("sdk:roles", this.onSdkRoles);
|
|
104
174
|
this.on("sdk:logs", this.onSdkLogs);
|
|
105
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* Re-authenticates the CURRENT connection in place: fetches a fresh
|
|
178
|
+
* ws-ticket and sends it over the already-open socket, so the server
|
|
179
|
+
* updates its view of who this connection belongs to without a
|
|
180
|
+
* disconnect/reconnect. A full reconnect would replace `this._ws` with a
|
|
181
|
+
* brand new socket.io instance, silently dropping every listener
|
|
182
|
+
* authRuntime registered on the old one (e.g. the response side of
|
|
183
|
+
* requestSignin -> user:txns:create -> sign) - this avoids that.
|
|
184
|
+
* No-op if there is no open connection yet (connect() will pick up a
|
|
185
|
+
* ticket on its own once one exists).
|
|
186
|
+
* @returns Promise<void>
|
|
187
|
+
*/
|
|
188
|
+
async refreshAuth() {
|
|
189
|
+
if (!this.isConnected()) return;
|
|
190
|
+
const ticket = await fetchWsTicket(this._apiUrl);
|
|
191
|
+
if (!ticket) return;
|
|
192
|
+
this._ws.emit("auth:refresh", ticket);
|
|
193
|
+
}
|
|
106
194
|
/**
|
|
107
195
|
* Allow you to subscribe to an event, and
|
|
108
196
|
* verify if the event is already subscribed
|
|
@@ -511,9 +599,9 @@ var hasAnyRoleBySlug = (user, roleSlugs) => {
|
|
|
511
599
|
|
|
512
600
|
// src/protocol/BlockAuthProvider.ts
|
|
513
601
|
var BlockAuthProvider = class {
|
|
514
|
-
constructor(apiKey = "", apiSecret = "", wsUrl = "wss://wss.block-auth.io") {
|
|
602
|
+
constructor(apiKey = "", apiSecret = "", wsUrl = "wss://wss.block-auth.io", options = {}) {
|
|
515
603
|
this._rpc = new RpcClient();
|
|
516
|
-
this._wsc = new WsClient(wsUrl, true);
|
|
604
|
+
this._wsc = new WsClient(wsUrl, true, options.apiUrl);
|
|
517
605
|
this._apiKey = apiKey;
|
|
518
606
|
this._apiSecret = apiSecret;
|
|
519
607
|
}
|
|
@@ -954,9 +1042,7 @@ var UI_CONFIG = {
|
|
|
954
1042
|
isLocked: false
|
|
955
1043
|
};
|
|
956
1044
|
var SESSION_CONFIG = {
|
|
957
|
-
did: ""
|
|
958
|
-
token: ""
|
|
959
|
-
};
|
|
1045
|
+
did: ""};
|
|
960
1046
|
var EXTERNAL_SERVICE_CONFIG = {
|
|
961
1047
|
connected: false,
|
|
962
1048
|
service: "blockauth",
|
|
@@ -974,6 +1060,11 @@ function getStorage() {
|
|
|
974
1060
|
return null;
|
|
975
1061
|
}
|
|
976
1062
|
var BrowserBase = class {
|
|
1063
|
+
constructor() {
|
|
1064
|
+
// In-memory only - see the SECURITY NOTE above. Never persisted.
|
|
1065
|
+
this._sessionToken = "";
|
|
1066
|
+
this._externalToken = "";
|
|
1067
|
+
}
|
|
977
1068
|
/**
|
|
978
1069
|
* Wipe every SDK-owned localStorage key and reseed the defaults. Unlike a
|
|
979
1070
|
* per-key reset it also clears the `blockauth-logged-out` /
|
|
@@ -992,6 +1083,8 @@ var BrowserBase = class {
|
|
|
992
1083
|
if (keepExtensionInstalled) {
|
|
993
1084
|
storage.setItem(UI, JSON.stringify({ ...UI_CONFIG, installed: true }));
|
|
994
1085
|
}
|
|
1086
|
+
this._sessionToken = "";
|
|
1087
|
+
this._externalToken = "";
|
|
995
1088
|
this.getUI();
|
|
996
1089
|
this.getSession();
|
|
997
1090
|
this.getExternalService();
|
|
@@ -1008,10 +1101,12 @@ var BrowserBase = class {
|
|
|
1008
1101
|
}
|
|
1009
1102
|
if (target === "session") {
|
|
1010
1103
|
storage?.removeItem(SESSION);
|
|
1104
|
+
this._sessionToken = "";
|
|
1011
1105
|
this.getSession();
|
|
1012
1106
|
}
|
|
1013
1107
|
if (target === "external") {
|
|
1014
1108
|
storage?.removeItem(EXTERNAL_SERVICE);
|
|
1109
|
+
this._externalToken = "";
|
|
1015
1110
|
this.getExternalService();
|
|
1016
1111
|
}
|
|
1017
1112
|
}
|
|
@@ -1071,33 +1166,55 @@ var BrowserBase = class {
|
|
|
1071
1166
|
})
|
|
1072
1167
|
);
|
|
1073
1168
|
}
|
|
1169
|
+
/**
|
|
1170
|
+
* `did` is read from localStorage; `token` is never persisted there (see
|
|
1171
|
+
* the SECURITY NOTE above) and comes from the in-memory field instead, so
|
|
1172
|
+
* it reads back as '' after a page reload even though `did` survives.
|
|
1173
|
+
*/
|
|
1074
1174
|
getSession() {
|
|
1075
1175
|
const storage = getStorage();
|
|
1076
|
-
if (!storage) return {
|
|
1176
|
+
if (!storage) return { did: "", token: this._sessionToken };
|
|
1077
1177
|
const currentConfig = storage.getItem(SESSION);
|
|
1078
|
-
if (currentConfig)
|
|
1079
|
-
|
|
1080
|
-
|
|
1178
|
+
if (currentConfig) {
|
|
1179
|
+
const { did } = JSON.parse(currentConfig);
|
|
1180
|
+
return { did: did || "", token: this._sessionToken };
|
|
1181
|
+
}
|
|
1182
|
+
storage.setItem(SESSION, JSON.stringify({ did: SESSION_CONFIG.did }));
|
|
1183
|
+
return { did: "", token: this._sessionToken };
|
|
1081
1184
|
}
|
|
1082
1185
|
/**
|
|
1083
|
-
* Obtains the configuration about the address and the service
|
|
1084
|
-
*
|
|
1186
|
+
* Obtains the configuration about the address and the service where the
|
|
1187
|
+
* user is connected. As with `getSession()`, `token` is never persisted
|
|
1188
|
+
* to localStorage - it's overlaid here from the in-memory field.
|
|
1085
1189
|
*/
|
|
1086
1190
|
getExternalService() {
|
|
1087
1191
|
const storage = getStorage();
|
|
1088
|
-
if (!storage) return { ...EXTERNAL_SERVICE_CONFIG };
|
|
1192
|
+
if (!storage) return { ...EXTERNAL_SERVICE_CONFIG, token: this._externalToken };
|
|
1089
1193
|
const currentConfig = storage.getItem(EXTERNAL_SERVICE);
|
|
1090
|
-
if (currentConfig)
|
|
1091
|
-
|
|
1194
|
+
if (currentConfig) {
|
|
1195
|
+
const parsed = JSON.parse(currentConfig);
|
|
1196
|
+
return { ...parsed, token: this._externalToken };
|
|
1197
|
+
}
|
|
1092
1198
|
storage.setItem(EXTERNAL_SERVICE, JSON.stringify(EXTERNAL_SERVICE_CONFIG));
|
|
1093
|
-
return
|
|
1094
|
-
storage.getItem(EXTERNAL_SERVICE)
|
|
1095
|
-
);
|
|
1199
|
+
return { ...EXTERNAL_SERVICE_CONFIG, token: this._externalToken };
|
|
1096
1200
|
}
|
|
1097
1201
|
/**
|
|
1098
|
-
* Auxiliary method to update the SESSION configuration using websocket
|
|
1202
|
+
* Auxiliary method to update the SESSION configuration using websocket
|
|
1203
|
+
* events.
|
|
1204
|
+
*
|
|
1205
|
+
* IMPORTANT: the wallet JWT (`token`) is never persisted to localStorage
|
|
1206
|
+
* anymore (see the SECURITY NOTE at the top of this file). It's kept in
|
|
1207
|
+
* the `_sessionToken`/`_externalToken` in-memory fields instead, and -
|
|
1208
|
+
* when it's a fresh/changed `blockauth` token - bridged into an httpOnly
|
|
1209
|
+
* `blockauth-session` cookie via the `/auth/session` backend endpoint.
|
|
1210
|
+
* Only the (non-sensitive) `did` is persisted, for UI/reload purposes.
|
|
1211
|
+
*
|
|
1212
|
+
* Returns a Promise once the (best-effort) session-bridge call has
|
|
1213
|
+
* settled; callers that don't need to wait for it can keep calling this
|
|
1214
|
+
* without awaiting, same as before - every state mutation below happens
|
|
1215
|
+
* synchronously, before the `await`.
|
|
1099
1216
|
*/
|
|
1100
|
-
setSession({
|
|
1217
|
+
async setSession({
|
|
1101
1218
|
installed = void 0,
|
|
1102
1219
|
isUser = void 0,
|
|
1103
1220
|
isUserActive = void 0,
|
|
@@ -1105,7 +1222,8 @@ var BrowserBase = class {
|
|
|
1105
1222
|
did = void 0,
|
|
1106
1223
|
token = void 0,
|
|
1107
1224
|
externalService = void 0,
|
|
1108
|
-
serviceName = "blockauth"
|
|
1225
|
+
serviceName = "blockauth",
|
|
1226
|
+
apiUrl = void 0
|
|
1109
1227
|
} = {}) {
|
|
1110
1228
|
const storage = getStorage();
|
|
1111
1229
|
if (!storage) return;
|
|
@@ -1122,16 +1240,20 @@ var BrowserBase = class {
|
|
|
1122
1240
|
isLocked: isLockedValue
|
|
1123
1241
|
};
|
|
1124
1242
|
storage.setItem(UI, JSON.stringify(newCfgUI));
|
|
1243
|
+
let tokenToBridge;
|
|
1125
1244
|
if (serviceName === "blockauth") {
|
|
1126
|
-
const
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1245
|
+
const didValue = did !== void 0 ? did : cfgSession.did || "";
|
|
1246
|
+
const tokenValue = token !== void 0 ? token : cfgSession.token || "";
|
|
1247
|
+
storage.setItem(SESSION, JSON.stringify({ did: didValue }));
|
|
1248
|
+
if (tokenValue && tokenValue !== this._sessionToken) {
|
|
1249
|
+
tokenToBridge = tokenValue;
|
|
1250
|
+
}
|
|
1251
|
+
this._sessionToken = tokenValue;
|
|
1131
1252
|
}
|
|
1132
1253
|
if (externalService) {
|
|
1133
1254
|
const cfgExternalService = this.getExternalService();
|
|
1134
1255
|
const isBlockauth = serviceName === "blockauth";
|
|
1256
|
+
const prevExternalToken = this._externalToken;
|
|
1135
1257
|
const connected = externalService.connected;
|
|
1136
1258
|
const service = externalService.service;
|
|
1137
1259
|
const didEs = externalService.did;
|
|
@@ -1153,20 +1275,29 @@ var BrowserBase = class {
|
|
|
1153
1275
|
account: !isBlockauth ? accountValue : "",
|
|
1154
1276
|
networkId: !isBlockauth ? networkIdValue : ""
|
|
1155
1277
|
};
|
|
1156
|
-
storage.setItem(
|
|
1278
|
+
storage.setItem(
|
|
1279
|
+
EXTERNAL_SERVICE,
|
|
1280
|
+
JSON.stringify({ ...newCfgExternalService, token: "" })
|
|
1281
|
+
);
|
|
1282
|
+
this._externalToken = newCfgExternalService.token;
|
|
1157
1283
|
if (!isBlockauth) {
|
|
1158
1284
|
storage.setItem(
|
|
1159
1285
|
SESSION,
|
|
1160
|
-
JSON.stringify({
|
|
1161
|
-
did: newCfgExternalService.did,
|
|
1162
|
-
token: newCfgExternalService.token
|
|
1163
|
-
})
|
|
1286
|
+
JSON.stringify({ did: newCfgExternalService.did, token: "" })
|
|
1164
1287
|
);
|
|
1288
|
+
this._sessionToken = newCfgExternalService.token;
|
|
1289
|
+
if (newCfgExternalService.token && newCfgExternalService.token !== prevExternalToken) {
|
|
1290
|
+
tokenToBridge = newCfgExternalService.token;
|
|
1291
|
+
}
|
|
1165
1292
|
}
|
|
1166
1293
|
}
|
|
1294
|
+
if (tokenToBridge) {
|
|
1295
|
+
await bridgeSessionCookie(tokenToBridge, apiUrl);
|
|
1296
|
+
}
|
|
1167
1297
|
}
|
|
1168
1298
|
externalServiceDisconnect() {
|
|
1169
1299
|
const storage = getStorage();
|
|
1300
|
+
this._externalToken = "";
|
|
1170
1301
|
if (!storage) return;
|
|
1171
1302
|
storage.setItem(
|
|
1172
1303
|
EXTERNAL_SERVICE,
|
|
@@ -1784,12 +1915,16 @@ function startExtensionSync() {
|
|
|
1784
1915
|
}
|
|
1785
1916
|
|
|
1786
1917
|
// src/auth/connectBlockAuth.ts
|
|
1787
|
-
function connectBlockAuth() {
|
|
1918
|
+
function connectBlockAuth(apiUrl) {
|
|
1788
1919
|
setServiceName("blockauth");
|
|
1789
|
-
|
|
1920
|
+
if (typeof localStorage !== "undefined") {
|
|
1921
|
+
localStorage.removeItem("blockauth-logged-out");
|
|
1922
|
+
}
|
|
1923
|
+
void browserBase.setSession({
|
|
1790
1924
|
...browserBase.getSession(),
|
|
1791
1925
|
externalService: { service: "blockauth", connected: true },
|
|
1792
|
-
serviceName: "blockauth"
|
|
1926
|
+
serviceName: "blockauth",
|
|
1927
|
+
apiUrl
|
|
1793
1928
|
});
|
|
1794
1929
|
setIsServiceConnected(true);
|
|
1795
1930
|
setWaitingForSuccessLogInSince(0);
|
|
@@ -1798,7 +1933,7 @@ function connectBlockAuth() {
|
|
|
1798
1933
|
|
|
1799
1934
|
// src/auth/logout.ts
|
|
1800
1935
|
async function performLogout(args) {
|
|
1801
|
-
const { wsc, token, apiKey, sessionId } = args;
|
|
1936
|
+
const { wsc, token, apiKey, sessionId, apiUrl } = args;
|
|
1802
1937
|
await new Promise((resolve) => {
|
|
1803
1938
|
let done = false;
|
|
1804
1939
|
const finish = () => {
|
|
@@ -1814,6 +1949,7 @@ async function performLogout(args) {
|
|
|
1814
1949
|
if (typeof localStorage !== "undefined") {
|
|
1815
1950
|
localStorage.setItem("blockauth-logged-out", "true");
|
|
1816
1951
|
}
|
|
1952
|
+
await clearSessionCookie(apiUrl);
|
|
1817
1953
|
resetWs();
|
|
1818
1954
|
resetUi();
|
|
1819
1955
|
resetWallet();
|
|
@@ -2049,7 +2185,8 @@ function createBlockAuthClient(config3) {
|
|
|
2049
2185
|
const provider = new BlockAuthProvider(
|
|
2050
2186
|
config3.apiKey,
|
|
2051
2187
|
config3.apiSecret,
|
|
2052
|
-
config3.wsUrl ?? "wss://wss.block-auth.io"
|
|
2188
|
+
config3.wsUrl ?? "wss://wss.block-auth.io",
|
|
2189
|
+
{ apiUrl: config3.apiUrl }
|
|
2053
2190
|
);
|
|
2054
2191
|
const evm = createEvmWallet({
|
|
2055
2192
|
walletConnectProjectId: config3.walletConnectProjectId,
|
|
@@ -2114,7 +2251,7 @@ function createBlockAuthClient(config3) {
|
|
|
2114
2251
|
web3: { signup: web3Signup },
|
|
2115
2252
|
// BlockAuth extension connect (sets blockauth as the active service);
|
|
2116
2253
|
// the txn-signing flow is driven by the caller via requestSignin.
|
|
2117
|
-
connectBlockAuth: () => connectBlockAuth(),
|
|
2254
|
+
connectBlockAuth: () => connectBlockAuth(config3.apiUrl),
|
|
2118
2255
|
// oauth
|
|
2119
2256
|
oauth: {
|
|
2120
2257
|
...oauth_exports,
|
|
@@ -2183,7 +2320,8 @@ function createBlockAuthClient(config3) {
|
|
|
2183
2320
|
wsc: provider.wsc,
|
|
2184
2321
|
token: userToken(),
|
|
2185
2322
|
apiKey: config3.apiKey,
|
|
2186
|
-
sessionId: browserBase.getDeviceSessionId()
|
|
2323
|
+
sessionId: browserBase.getDeviceSessionId(),
|
|
2324
|
+
apiUrl: config3.apiUrl
|
|
2187
2325
|
}),
|
|
2188
2326
|
// modals (stack — supports overlaying signin on top of signup)
|
|
2189
2327
|
openModal,
|
|
@@ -2192,6 +2330,9 @@ function createBlockAuthClient(config3) {
|
|
|
2192
2330
|
// opciones del connect inicial del WS (las consume createAuthRuntime)
|
|
2193
2331
|
wsConnectTimeout: config3.wsConnectTimeout ?? 300,
|
|
2194
2332
|
wsConnectRetries: config3.wsConnectRetries ?? 3,
|
|
2333
|
+
// Backend base url for the session/logout/ws-ticket bridge (consumed by
|
|
2334
|
+
// createAuthRuntime and by browserBase.setSession callers here above).
|
|
2335
|
+
apiUrl: config3.apiUrl,
|
|
2195
2336
|
// acceso crudo al provider si hace falta
|
|
2196
2337
|
provider
|
|
2197
2338
|
};
|
|
@@ -2299,6 +2440,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2299
2440
|
if (typeof localStorage !== "undefined") {
|
|
2300
2441
|
localStorage.setItem("blockauth-logged-out", "true");
|
|
2301
2442
|
}
|
|
2443
|
+
await clearSessionCookie(client.apiUrl);
|
|
2302
2444
|
const serviceName = browserBase.getExternalService().service;
|
|
2303
2445
|
setIsServiceConnected(false);
|
|
2304
2446
|
setWaitingForSuccessLogInSince(0);
|
|
@@ -2323,6 +2465,18 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2323
2465
|
console.error("[sdk] applyLocalLogout failed (storage event):", err);
|
|
2324
2466
|
});
|
|
2325
2467
|
};
|
|
2468
|
+
const onExtensionSessionBridged = (event) => {
|
|
2469
|
+
const did = event.detail?.did;
|
|
2470
|
+
if (did) {
|
|
2471
|
+
void browserBase.setSession({
|
|
2472
|
+
did,
|
|
2473
|
+
serviceName: "blockauth",
|
|
2474
|
+
apiUrl: client.apiUrl
|
|
2475
|
+
});
|
|
2476
|
+
setDid(did);
|
|
2477
|
+
}
|
|
2478
|
+
void wsc().refreshAuth();
|
|
2479
|
+
};
|
|
2326
2480
|
const deletion = createDeletionService({ provider });
|
|
2327
2481
|
const handleUserDeleted = (forAddress = null) => {
|
|
2328
2482
|
const handled = deletion.handleUserDeleted(forAddress);
|
|
@@ -2343,10 +2497,11 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2343
2497
|
if (service === "blockauth" || prevDid && prevDid.toLowerCase() !== String(did).toLowerCase()) {
|
|
2344
2498
|
browserBase.clearBrowserState({ keepExtensionInstalled: keepInstalled });
|
|
2345
2499
|
}
|
|
2346
|
-
browserBase.setSession({
|
|
2500
|
+
void browserBase.setSession({
|
|
2347
2501
|
...result,
|
|
2348
|
-
serviceName: service
|
|
2349
|
-
|
|
2502
|
+
serviceName: service,
|
|
2503
|
+
apiUrl: client.apiUrl
|
|
2504
|
+
}).then(() => wsc().refreshAuth());
|
|
2350
2505
|
setDid(did);
|
|
2351
2506
|
setToken(token);
|
|
2352
2507
|
setServiceIcon(imgUrlByService(service));
|
|
@@ -2413,10 +2568,6 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2413
2568
|
const signinUnsigned = async () => {
|
|
2414
2569
|
setIsSignedFirstCheck(true);
|
|
2415
2570
|
if (!hookState.isSigned) {
|
|
2416
|
-
const ext = browserBase.getExternalService();
|
|
2417
|
-
if (ext.connected && typeof localStorage !== "undefined") {
|
|
2418
|
-
localStorage.setItem("blockauth-logged-out", "true");
|
|
2419
|
-
}
|
|
2420
2571
|
await _onError();
|
|
2421
2572
|
}
|
|
2422
2573
|
};
|
|
@@ -2623,7 +2774,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2623
2774
|
if (!activeDid || activeDid !== changedAddress) return;
|
|
2624
2775
|
await fetchUserProfile(provider, activeDid);
|
|
2625
2776
|
};
|
|
2626
|
-
const onSdkRestore = (data) => {
|
|
2777
|
+
const onSdkRestore = async (data) => {
|
|
2627
2778
|
if (data.success) {
|
|
2628
2779
|
deletion.resetHandlingState();
|
|
2629
2780
|
setIsOnDeletion(false);
|
|
@@ -2634,15 +2785,17 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2634
2785
|
browserBase.clearBrowserState({
|
|
2635
2786
|
keepExtensionInstalled: keepInstalled
|
|
2636
2787
|
});
|
|
2637
|
-
browserBase.setSession({
|
|
2788
|
+
await browserBase.setSession({
|
|
2638
2789
|
did: session.did,
|
|
2639
2790
|
token: session.token,
|
|
2640
|
-
serviceName: "blockauth"
|
|
2791
|
+
serviceName: "blockauth",
|
|
2792
|
+
apiUrl: client.apiUrl
|
|
2641
2793
|
});
|
|
2642
2794
|
if (session.serviceName && session.serviceName !== "blockauth") {
|
|
2643
|
-
browserBase.setSession({
|
|
2795
|
+
await browserBase.setSession({
|
|
2644
2796
|
...session,
|
|
2645
|
-
serviceName: session.serviceName
|
|
2797
|
+
serviceName: session.serviceName,
|
|
2798
|
+
apiUrl: client.apiUrl
|
|
2646
2799
|
});
|
|
2647
2800
|
}
|
|
2648
2801
|
}
|
|
@@ -2669,7 +2822,8 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2669
2822
|
setDid(_did);
|
|
2670
2823
|
setToken(_token);
|
|
2671
2824
|
setApiKey(client.apiKey ?? $ws.get().apiKey);
|
|
2672
|
-
|
|
2825
|
+
const isLoggedOut = typeof localStorage !== "undefined" && localStorage.getItem("blockauth-logged-out") === "true";
|
|
2826
|
+
setIsServiceConnected(isLoggedOut ? false : externalService.connected);
|
|
2673
2827
|
setServiceName(externalService.service);
|
|
2674
2828
|
setServiceIcon(imgUrlByService(externalService.service));
|
|
2675
2829
|
await provider.connect(
|
|
@@ -2713,6 +2867,12 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2713
2867
|
if (typeof window !== "undefined") {
|
|
2714
2868
|
window.addEventListener("storage", onStorageLogout);
|
|
2715
2869
|
}
|
|
2870
|
+
if (typeof document !== "undefined") {
|
|
2871
|
+
document.addEventListener(
|
|
2872
|
+
"blockauth:session-bridged",
|
|
2873
|
+
onExtensionSessionBridged
|
|
2874
|
+
);
|
|
2875
|
+
}
|
|
2716
2876
|
const { domain, signinCallbackUrl, config: config3 } = await provider.app();
|
|
2717
2877
|
setUiConfig({ domain, signinCallbackUrl, config: config3 });
|
|
2718
2878
|
setReady(true);
|
|
@@ -2755,6 +2915,12 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2755
2915
|
if (typeof window !== "undefined") {
|
|
2756
2916
|
window.removeEventListener("storage", onStorageLogout);
|
|
2757
2917
|
}
|
|
2918
|
+
if (typeof document !== "undefined") {
|
|
2919
|
+
document.removeEventListener(
|
|
2920
|
+
"blockauth:session-bridged",
|
|
2921
|
+
onExtensionSessionBridged
|
|
2922
|
+
);
|
|
2923
|
+
}
|
|
2758
2924
|
provider.disconnect();
|
|
2759
2925
|
};
|
|
2760
2926
|
const logout = async () => {
|