@auditauth/web 0.2.0-beta.2 → 0.2.0-beta.3
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/README.md +11 -0
- package/dist/index.cjs +22 -0
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/sdk.cjs +189 -0
- package/dist/sdk.d.cts +23 -0
- package/dist/sdk.d.ts +4 -2
- package/dist/sdk.js +155 -154
- package/dist/types.cjs +16 -0
- package/dist/types.d.cts +7 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.js +0 -1
- package/package.json +11 -8
package/README.md
CHANGED
|
@@ -12,6 +12,17 @@ Install the package in your app.
|
|
|
12
12
|
npm install @auditauth/web
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
## TypeScript import compatibility
|
|
16
|
+
|
|
17
|
+
`@auditauth/web` ships dual module output (ESM + CJS) with declaration files.
|
|
18
|
+
You can import it in TypeScript projects with standard syntax:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { AuditAuthWeb } from '@auditauth/web'
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
You do not need to append `.js` in consumer imports.
|
|
25
|
+
|
|
15
26
|
## Create an SDK instance
|
|
16
27
|
|
|
17
28
|
Create one `AuditAuthWeb` instance with your AuditAuth config and a storage
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var index_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(index_exports);
|
|
18
|
+
__reExport(index_exports, require("./sdk.js"), module.exports);
|
|
19
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
20
|
+
0 && (module.exports = {
|
|
21
|
+
...require("./sdk.js")
|
|
22
|
+
});
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { AuditAuthWeb } from './sdk.js';
|
|
2
|
+
export { StorageAdapter } from './types.js';
|
|
3
|
+
import '@auditauth/core';
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from "./sdk.js";
|
package/dist/sdk.cjs
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var sdk_exports = {};
|
|
20
|
+
__export(sdk_exports, {
|
|
21
|
+
AuditAuthWeb: () => AuditAuthWeb
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(sdk_exports);
|
|
24
|
+
var import_core = require("@auditauth/core");
|
|
25
|
+
class AuditAuthWeb {
|
|
26
|
+
constructor(config, storage) {
|
|
27
|
+
this.config = config;
|
|
28
|
+
this.storage = storage;
|
|
29
|
+
}
|
|
30
|
+
getWithExpiry(key) {
|
|
31
|
+
const itemStr = this.storage.get(key);
|
|
32
|
+
if (!itemStr) return null;
|
|
33
|
+
const item = JSON.parse(itemStr);
|
|
34
|
+
if (Date.now() > item.expiresAt) {
|
|
35
|
+
this.storage.remove(key);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return item.value;
|
|
39
|
+
}
|
|
40
|
+
setWithExpiry(key, value, ttlSeconds) {
|
|
41
|
+
const now = Date.now();
|
|
42
|
+
const item = {
|
|
43
|
+
value,
|
|
44
|
+
expiresAt: now + ttlSeconds * 1e3
|
|
45
|
+
};
|
|
46
|
+
this.storage.set(key, JSON.stringify(item));
|
|
47
|
+
}
|
|
48
|
+
getSessionId() {
|
|
49
|
+
let session_id = this.getWithExpiry(import_core.CORE_SETTINGS.storage_keys.session_id);
|
|
50
|
+
if (!session_id) {
|
|
51
|
+
session_id = crypto.randomUUID();
|
|
52
|
+
this.setWithExpiry(import_core.CORE_SETTINGS.storage_keys.session_id, session_id, 60 * 5);
|
|
53
|
+
}
|
|
54
|
+
return session_id;
|
|
55
|
+
}
|
|
56
|
+
pushMetric(metric) {
|
|
57
|
+
const session_id = this.getSessionId();
|
|
58
|
+
const body = JSON.stringify({
|
|
59
|
+
appId: this.config.appId,
|
|
60
|
+
apiKey: this.config.apiKey,
|
|
61
|
+
session_id,
|
|
62
|
+
...metric
|
|
63
|
+
});
|
|
64
|
+
const url = `${import_core.CORE_SETTINGS.domains.api}/metrics`;
|
|
65
|
+
if (navigator.sendBeacon) {
|
|
66
|
+
const blob = new Blob([body], { type: "application/json" });
|
|
67
|
+
navigator.sendBeacon(url, blob);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
fetch(url, {
|
|
71
|
+
method: "POST",
|
|
72
|
+
headers: { "Content-Type": "application/json" },
|
|
73
|
+
body,
|
|
74
|
+
keepalive: true
|
|
75
|
+
}).catch(() => {
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
trackNavigationPath(path) {
|
|
79
|
+
this.pushMetric({
|
|
80
|
+
event_type: "navigation",
|
|
81
|
+
runtime: "browser",
|
|
82
|
+
target: {
|
|
83
|
+
type: "page",
|
|
84
|
+
path
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
initNavigationTracking() {
|
|
89
|
+
const track = () => {
|
|
90
|
+
this.pushMetric({
|
|
91
|
+
event_type: "navigation",
|
|
92
|
+
runtime: "browser",
|
|
93
|
+
target: {
|
|
94
|
+
type: "page",
|
|
95
|
+
path: window.location.pathname
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
window.addEventListener("popstate", track);
|
|
100
|
+
const originalPushState = history.pushState;
|
|
101
|
+
history.pushState = (...args) => {
|
|
102
|
+
originalPushState.apply(history, args);
|
|
103
|
+
track();
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
async fetch(input, init = {}) {
|
|
107
|
+
const start = performance.now();
|
|
108
|
+
let access = this.getWithExpiry(import_core.CORE_SETTINGS.storage_keys.access);
|
|
109
|
+
const doFetch = (token) => fetch(input, {
|
|
110
|
+
...init,
|
|
111
|
+
credentials: "include",
|
|
112
|
+
headers: {
|
|
113
|
+
...init.headers,
|
|
114
|
+
...token ? { Authorization: `Bearer ${token}` } : {}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
let res = await doFetch(access || void 0);
|
|
118
|
+
if (res.status === 401) {
|
|
119
|
+
const refreshData = await (0, import_core.refreshTokens)({ client_type: "browser" });
|
|
120
|
+
if (!refreshData) {
|
|
121
|
+
await this.logout();
|
|
122
|
+
return res;
|
|
123
|
+
}
|
|
124
|
+
this.setWithExpiry(
|
|
125
|
+
import_core.CORE_SETTINGS.storage_keys.access,
|
|
126
|
+
refreshData.access_token,
|
|
127
|
+
refreshData.access_expires_seconds
|
|
128
|
+
);
|
|
129
|
+
res = await doFetch(refreshData.access_token);
|
|
130
|
+
}
|
|
131
|
+
this.pushMetric({
|
|
132
|
+
event_type: "request",
|
|
133
|
+
runtime: "browser",
|
|
134
|
+
target: {
|
|
135
|
+
type: "api",
|
|
136
|
+
method: init.method || "GET",
|
|
137
|
+
path: typeof input === "string" ? input : input instanceof Request ? input.url : "",
|
|
138
|
+
status: res.status,
|
|
139
|
+
duration_ms: Math.round(performance.now() - start)
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
return res;
|
|
143
|
+
}
|
|
144
|
+
isAuthenticated() {
|
|
145
|
+
return !!this.getWithExpiry(import_core.CORE_SETTINGS.storage_keys.session);
|
|
146
|
+
}
|
|
147
|
+
getSessionUser() {
|
|
148
|
+
const value = this.getWithExpiry(import_core.CORE_SETTINGS.storage_keys.session);
|
|
149
|
+
return value ? value?.user : null;
|
|
150
|
+
}
|
|
151
|
+
async goToPortal() {
|
|
152
|
+
const access_token = this.getWithExpiry(import_core.CORE_SETTINGS.storage_keys.access);
|
|
153
|
+
if (!access_token) return this.login();
|
|
154
|
+
const url = await (0, import_core.buildPortalUrl)({ access_token, redirectUrl: this.config.redirectUrl });
|
|
155
|
+
window.location.href = url.href;
|
|
156
|
+
}
|
|
157
|
+
async logout() {
|
|
158
|
+
const access_token = this.getWithExpiry(import_core.CORE_SETTINGS.storage_keys.access);
|
|
159
|
+
await (0, import_core.revokeSession)({ access_token }).catch(() => {
|
|
160
|
+
});
|
|
161
|
+
this.storage.remove(import_core.CORE_SETTINGS.storage_keys.access);
|
|
162
|
+
this.storage.remove(import_core.CORE_SETTINGS.storage_keys.session);
|
|
163
|
+
window.location.reload();
|
|
164
|
+
}
|
|
165
|
+
async login() {
|
|
166
|
+
const url = await (0, import_core.buildAuthUrl)({
|
|
167
|
+
apiKey: this.config.apiKey,
|
|
168
|
+
redirectUrl: this.config.redirectUrl
|
|
169
|
+
});
|
|
170
|
+
window.location.href = url.href;
|
|
171
|
+
}
|
|
172
|
+
async handleRedirect() {
|
|
173
|
+
const url = new URL(window.location.href);
|
|
174
|
+
const code = url.searchParams.get("code");
|
|
175
|
+
try {
|
|
176
|
+
const { data } = await (0, import_core.authorizeCode)({ code, client_type: "browser" });
|
|
177
|
+
this.setWithExpiry(import_core.CORE_SETTINGS.storage_keys.access, data.access_token, data.access_expires_seconds);
|
|
178
|
+
this.setWithExpiry(import_core.CORE_SETTINGS.storage_keys.session, { user: data.user }, data.access_expires_seconds);
|
|
179
|
+
url.searchParams.delete("code");
|
|
180
|
+
window.history.replaceState({}, document.title, url.pathname + url.search);
|
|
181
|
+
} catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
187
|
+
0 && (module.exports = {
|
|
188
|
+
AuditAuthWeb
|
|
189
|
+
});
|
package/dist/sdk.d.cts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AuditAuthConfig } from '@auditauth/core';
|
|
2
|
+
import { StorageAdapter } from './types.cjs';
|
|
3
|
+
|
|
4
|
+
declare class AuditAuthWeb {
|
|
5
|
+
private config;
|
|
6
|
+
private storage;
|
|
7
|
+
constructor(config: AuditAuthConfig, storage: StorageAdapter);
|
|
8
|
+
private getWithExpiry;
|
|
9
|
+
private setWithExpiry;
|
|
10
|
+
private getSessionId;
|
|
11
|
+
private pushMetric;
|
|
12
|
+
trackNavigationPath(path: string): void;
|
|
13
|
+
initNavigationTracking(): void;
|
|
14
|
+
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
|
|
15
|
+
isAuthenticated(): boolean;
|
|
16
|
+
getSessionUser(): any;
|
|
17
|
+
goToPortal(): Promise<void>;
|
|
18
|
+
logout(): Promise<void>;
|
|
19
|
+
login(): Promise<void>;
|
|
20
|
+
handleRedirect(): Promise<null | undefined>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { AuditAuthWeb };
|
package/dist/sdk.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { AuditAuthConfig } from
|
|
2
|
-
import
|
|
1
|
+
import { AuditAuthConfig } from '@auditauth/core';
|
|
2
|
+
import { StorageAdapter } from './types.js';
|
|
3
|
+
|
|
3
4
|
declare class AuditAuthWeb {
|
|
4
5
|
private config;
|
|
5
6
|
private storage;
|
|
@@ -18,4 +19,5 @@ declare class AuditAuthWeb {
|
|
|
18
19
|
login(): Promise<void>;
|
|
19
20
|
handleRedirect(): Promise<null | undefined>;
|
|
20
21
|
}
|
|
22
|
+
|
|
21
23
|
export { AuditAuthWeb };
|
package/dist/sdk.js
CHANGED
|
@@ -1,164 +1,165 @@
|
|
|
1
1
|
import { authorizeCode, buildAuthUrl, buildPortalUrl, CORE_SETTINGS, refreshTokens, revokeSession } from "@auditauth/core";
|
|
2
2
|
class AuditAuthWeb {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
constructor(config, storage) {
|
|
4
|
+
this.config = config;
|
|
5
|
+
this.storage = storage;
|
|
6
|
+
}
|
|
7
|
+
getWithExpiry(key) {
|
|
8
|
+
const itemStr = this.storage.get(key);
|
|
9
|
+
if (!itemStr) return null;
|
|
10
|
+
const item = JSON.parse(itemStr);
|
|
11
|
+
if (Date.now() > item.expiresAt) {
|
|
12
|
+
this.storage.remove(key);
|
|
13
|
+
return null;
|
|
6
14
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
expiresAt: now + ttlSeconds * 1000,
|
|
23
|
-
};
|
|
24
|
-
this.storage.set(key, JSON.stringify(item));
|
|
25
|
-
}
|
|
26
|
-
getSessionId() {
|
|
27
|
-
let session_id = this.getWithExpiry(CORE_SETTINGS.storage_keys.session_id);
|
|
28
|
-
if (!session_id) {
|
|
29
|
-
session_id = crypto.randomUUID();
|
|
30
|
-
this.setWithExpiry(CORE_SETTINGS.storage_keys.session_id, session_id, 60 * 5);
|
|
31
|
-
}
|
|
32
|
-
return session_id;
|
|
33
|
-
}
|
|
34
|
-
pushMetric(metric) {
|
|
35
|
-
const session_id = this.getSessionId();
|
|
36
|
-
const body = JSON.stringify({
|
|
37
|
-
appId: this.config.appId,
|
|
38
|
-
apiKey: this.config.apiKey,
|
|
39
|
-
session_id,
|
|
40
|
-
...metric,
|
|
41
|
-
});
|
|
42
|
-
const url = `${CORE_SETTINGS.domains.api}/metrics`;
|
|
43
|
-
if (navigator.sendBeacon) {
|
|
44
|
-
const blob = new Blob([body], { type: 'application/json' });
|
|
45
|
-
navigator.sendBeacon(url, blob);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
fetch(url, {
|
|
49
|
-
method: 'POST',
|
|
50
|
-
headers: { 'Content-Type': 'application/json' },
|
|
51
|
-
body,
|
|
52
|
-
keepalive: true,
|
|
53
|
-
}).catch(() => { });
|
|
54
|
-
}
|
|
55
|
-
trackNavigationPath(path) {
|
|
56
|
-
this.pushMetric({
|
|
57
|
-
event_type: 'navigation',
|
|
58
|
-
runtime: 'browser',
|
|
59
|
-
target: {
|
|
60
|
-
type: 'page',
|
|
61
|
-
path: path,
|
|
62
|
-
},
|
|
63
|
-
});
|
|
15
|
+
return item.value;
|
|
16
|
+
}
|
|
17
|
+
setWithExpiry(key, value, ttlSeconds) {
|
|
18
|
+
const now = Date.now();
|
|
19
|
+
const item = {
|
|
20
|
+
value,
|
|
21
|
+
expiresAt: now + ttlSeconds * 1e3
|
|
22
|
+
};
|
|
23
|
+
this.storage.set(key, JSON.stringify(item));
|
|
24
|
+
}
|
|
25
|
+
getSessionId() {
|
|
26
|
+
let session_id = this.getWithExpiry(CORE_SETTINGS.storage_keys.session_id);
|
|
27
|
+
if (!session_id) {
|
|
28
|
+
session_id = crypto.randomUUID();
|
|
29
|
+
this.setWithExpiry(CORE_SETTINGS.storage_keys.session_id, session_id, 60 * 5);
|
|
64
30
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
track();
|
|
81
|
-
};
|
|
31
|
+
return session_id;
|
|
32
|
+
}
|
|
33
|
+
pushMetric(metric) {
|
|
34
|
+
const session_id = this.getSessionId();
|
|
35
|
+
const body = JSON.stringify({
|
|
36
|
+
appId: this.config.appId,
|
|
37
|
+
apiKey: this.config.apiKey,
|
|
38
|
+
session_id,
|
|
39
|
+
...metric
|
|
40
|
+
});
|
|
41
|
+
const url = `${CORE_SETTINGS.domains.api}/metrics`;
|
|
42
|
+
if (navigator.sendBeacon) {
|
|
43
|
+
const blob = new Blob([body], { type: "application/json" });
|
|
44
|
+
navigator.sendBeacon(url, blob);
|
|
45
|
+
return;
|
|
82
46
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
47
|
+
fetch(url, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: { "Content-Type": "application/json" },
|
|
50
|
+
body,
|
|
51
|
+
keepalive: true
|
|
52
|
+
}).catch(() => {
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
trackNavigationPath(path) {
|
|
56
|
+
this.pushMetric({
|
|
57
|
+
event_type: "navigation",
|
|
58
|
+
runtime: "browser",
|
|
59
|
+
target: {
|
|
60
|
+
type: "page",
|
|
61
|
+
path
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
initNavigationTracking() {
|
|
66
|
+
const track = () => {
|
|
67
|
+
this.pushMetric({
|
|
68
|
+
event_type: "navigation",
|
|
69
|
+
runtime: "browser",
|
|
70
|
+
target: {
|
|
71
|
+
type: "page",
|
|
72
|
+
path: window.location.pathname
|
|
103
73
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
window.addEventListener("popstate", track);
|
|
77
|
+
const originalPushState = history.pushState;
|
|
78
|
+
history.pushState = (...args) => {
|
|
79
|
+
originalPushState.apply(history, args);
|
|
80
|
+
track();
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
async fetch(input, init = {}) {
|
|
84
|
+
const start = performance.now();
|
|
85
|
+
let access = this.getWithExpiry(CORE_SETTINGS.storage_keys.access);
|
|
86
|
+
const doFetch = (token) => fetch(input, {
|
|
87
|
+
...init,
|
|
88
|
+
credentials: "include",
|
|
89
|
+
headers: {
|
|
90
|
+
...init.headers,
|
|
91
|
+
...token ? { Authorization: `Bearer ${token}` } : {}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
let res = await doFetch(access || void 0);
|
|
95
|
+
if (res.status === 401) {
|
|
96
|
+
const refreshData = await refreshTokens({ client_type: "browser" });
|
|
97
|
+
if (!refreshData) {
|
|
98
|
+
await this.logout();
|
|
119
99
|
return res;
|
|
100
|
+
}
|
|
101
|
+
this.setWithExpiry(
|
|
102
|
+
CORE_SETTINGS.storage_keys.access,
|
|
103
|
+
refreshData.access_token,
|
|
104
|
+
refreshData.access_expires_seconds
|
|
105
|
+
);
|
|
106
|
+
res = await doFetch(refreshData.access_token);
|
|
120
107
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
108
|
+
this.pushMetric({
|
|
109
|
+
event_type: "request",
|
|
110
|
+
runtime: "browser",
|
|
111
|
+
target: {
|
|
112
|
+
type: "api",
|
|
113
|
+
method: init.method || "GET",
|
|
114
|
+
path: typeof input === "string" ? input : input instanceof Request ? input.url : "",
|
|
115
|
+
status: res.status,
|
|
116
|
+
duration_ms: Math.round(performance.now() - start)
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
return res;
|
|
120
|
+
}
|
|
121
|
+
isAuthenticated() {
|
|
122
|
+
return !!this.getWithExpiry(CORE_SETTINGS.storage_keys.session);
|
|
123
|
+
}
|
|
124
|
+
getSessionUser() {
|
|
125
|
+
const value = this.getWithExpiry(CORE_SETTINGS.storage_keys.session);
|
|
126
|
+
return value ? value?.user : null;
|
|
127
|
+
}
|
|
128
|
+
async goToPortal() {
|
|
129
|
+
const access_token = this.getWithExpiry(CORE_SETTINGS.storage_keys.access);
|
|
130
|
+
if (!access_token) return this.login();
|
|
131
|
+
const url = await buildPortalUrl({ access_token, redirectUrl: this.config.redirectUrl });
|
|
132
|
+
window.location.href = url.href;
|
|
133
|
+
}
|
|
134
|
+
async logout() {
|
|
135
|
+
const access_token = this.getWithExpiry(CORE_SETTINGS.storage_keys.access);
|
|
136
|
+
await revokeSession({ access_token }).catch(() => {
|
|
137
|
+
});
|
|
138
|
+
this.storage.remove(CORE_SETTINGS.storage_keys.access);
|
|
139
|
+
this.storage.remove(CORE_SETTINGS.storage_keys.session);
|
|
140
|
+
window.location.reload();
|
|
141
|
+
}
|
|
142
|
+
async login() {
|
|
143
|
+
const url = await buildAuthUrl({
|
|
144
|
+
apiKey: this.config.apiKey,
|
|
145
|
+
redirectUrl: this.config.redirectUrl
|
|
146
|
+
});
|
|
147
|
+
window.location.href = url.href;
|
|
148
|
+
}
|
|
149
|
+
async handleRedirect() {
|
|
150
|
+
const url = new URL(window.location.href);
|
|
151
|
+
const code = url.searchParams.get("code");
|
|
152
|
+
try {
|
|
153
|
+
const { data } = await authorizeCode({ code, client_type: "browser" });
|
|
154
|
+
this.setWithExpiry(CORE_SETTINGS.storage_keys.access, data.access_token, data.access_expires_seconds);
|
|
155
|
+
this.setWithExpiry(CORE_SETTINGS.storage_keys.session, { user: data.user }, data.access_expires_seconds);
|
|
156
|
+
url.searchParams.delete("code");
|
|
157
|
+
window.history.replaceState({}, document.title, url.pathname + url.search);
|
|
158
|
+
} catch {
|
|
159
|
+
return null;
|
|
162
160
|
}
|
|
161
|
+
}
|
|
163
162
|
}
|
|
164
|
-
export {
|
|
163
|
+
export {
|
|
164
|
+
AuditAuthWeb
|
|
165
|
+
};
|
package/dist/types.cjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var types_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(types_exports);
|
package/dist/types.d.cts
ADDED
package/dist/types.d.ts
CHANGED
package/dist/types.js
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@auditauth/web",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.3",
|
|
4
4
|
"description": "AuditAuth Web SDK (framework-agnostic)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Nimibyte",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
"security",
|
|
25
25
|
"auditauth"
|
|
26
26
|
],
|
|
27
|
-
"module": "dist/index.js",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
28
|
"type": "module",
|
|
29
|
-
"main": "dist/index.
|
|
30
|
-
"types": "dist/index.d.ts",
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
31
|
"files": [
|
|
32
32
|
"dist"
|
|
33
33
|
],
|
|
@@ -35,17 +35,20 @@
|
|
|
35
35
|
"exports": {
|
|
36
36
|
".": {
|
|
37
37
|
"types": "./dist/index.d.ts",
|
|
38
|
+
"import": "./dist/index.js",
|
|
39
|
+
"require": "./dist/index.cjs",
|
|
38
40
|
"default": "./dist/index.js"
|
|
39
41
|
},
|
|
40
42
|
"./package.json": "./package.json"
|
|
41
43
|
},
|
|
42
44
|
"scripts": {
|
|
43
|
-
"build": "
|
|
44
|
-
"dev": "
|
|
45
|
-
"clean": "rm -rf dist"
|
|
45
|
+
"build": "tsup",
|
|
46
|
+
"dev": "tsup --watch",
|
|
47
|
+
"clean": "rm -rf dist",
|
|
48
|
+
"prepack": "npm run build"
|
|
46
49
|
},
|
|
47
50
|
"dependencies": {
|
|
48
|
-
"@auditauth/core": "^0.2.0-beta.
|
|
51
|
+
"@auditauth/core": "^0.2.0-beta.3"
|
|
49
52
|
},
|
|
50
53
|
"devDependencies": {
|
|
51
54
|
"typescript": "^5.3.3"
|