@mspkapps/auth-client 0.1.2 → 0.1.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/package.json +1 -1
- package/src/AuthClient.js +16 -30
package/package.json
CHANGED
package/src/AuthClient.js
CHANGED
|
@@ -6,37 +6,31 @@ export class AuthClient {
|
|
|
6
6
|
apiKey,
|
|
7
7
|
apiSecret,
|
|
8
8
|
baseUrl = 'https://cpanel.backend.mspkapps.in/api/v1',
|
|
9
|
-
keyInPath = true,
|
|
10
9
|
storage,
|
|
11
|
-
fetch: fetchFn
|
|
10
|
+
fetch: fetchFn,
|
|
11
|
+
keyInPath = false // default: use headers, not key in URL
|
|
12
12
|
} = {}) {
|
|
13
13
|
if (!apiKey) throw new Error('apiKey is required');
|
|
14
|
-
if (!apiSecret) throw new Error('apiSecret is required'); //
|
|
14
|
+
if (!apiSecret) throw new Error('apiSecret is required'); // do not expose in browsers for prod
|
|
15
15
|
this.apiKey = apiKey;
|
|
16
16
|
this.apiSecret = apiSecret;
|
|
17
17
|
this.baseUrl = baseUrl.replace(/\/$/, '');
|
|
18
18
|
this.keyInPath = !!keyInPath;
|
|
19
|
-
this.fetch = fetchFn || (typeof fetch !== 'undefined' ? fetch : null);
|
|
20
|
-
if (!this.fetch) throw new Error('No fetch available. Pass { fetch } or run on Node 18+/browsers.');
|
|
21
19
|
|
|
22
|
-
|
|
20
|
+
const f = fetchFn || (typeof window !== 'undefined' ? window.fetch : (typeof fetch !== 'undefined' ? fetch : null));
|
|
21
|
+
if (!f) throw new Error('No fetch available. Pass { fetch } or run on Node 18+/browsers.');
|
|
22
|
+
// Bind to avoid “Illegal invocation”
|
|
23
|
+
this.fetch = (...args) => f(...args);
|
|
24
|
+
|
|
25
|
+
this.storage = storage ?? (typeof window !== 'undefined' ? window.localStorage : null);
|
|
23
26
|
this.tokenKey = 'auth_user_token';
|
|
24
27
|
this.token = this._load(this.tokenKey);
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
// ---------- storage helpers ----------
|
|
28
|
-
_load(key) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
_save(key, val) {
|
|
33
|
-
if (!this.storage) return;
|
|
34
|
-
try { this.storage.setItem(key, val); } catch { /* ignore */ }
|
|
35
|
-
}
|
|
36
|
-
_clear(key) {
|
|
37
|
-
if (!this.storage) return;
|
|
38
|
-
try { this.storage.removeItem(key); } catch { /* ignore */ }
|
|
39
|
-
}
|
|
31
|
+
_load(key) { if (!this.storage) return null; try { return this.storage.getItem(key); } catch { return null; } }
|
|
32
|
+
_save(key, val) { if (!this.storage) return; try { this.storage.setItem(key, val); } catch {} }
|
|
33
|
+
_clear(key) { if (!this.storage) return; try { this.storage.removeItem(key); } catch {} }
|
|
40
34
|
|
|
41
35
|
// ---------- internal builders ----------
|
|
42
36
|
_buildUrl(path) {
|
|
@@ -62,13 +56,8 @@ export class AuthClient {
|
|
|
62
56
|
else this._clear(this.tokenKey);
|
|
63
57
|
}
|
|
64
58
|
|
|
65
|
-
getAuthHeader() {
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
logout() {
|
|
70
|
-
this.setToken(null);
|
|
71
|
-
}
|
|
59
|
+
getAuthHeader() { return this.token ? { Authorization: `UserToken ${this.token}` } : {}; }
|
|
60
|
+
logout() { this.setToken(null); }
|
|
72
61
|
|
|
73
62
|
// ---------- public API methods ----------
|
|
74
63
|
async register({ email, username, password, name }) {
|
|
@@ -122,16 +111,13 @@ export class AuthClient {
|
|
|
122
111
|
}
|
|
123
112
|
|
|
124
113
|
// ---------- helpers ----------
|
|
125
|
-
async function safeJson(resp) {
|
|
126
|
-
try { return await resp.json(); } catch { return null; }
|
|
127
|
-
}
|
|
114
|
+
async function safeJson(resp) { try { return await resp.json(); } catch { return null; } }
|
|
128
115
|
|
|
129
116
|
function toError(resp, json, fallback) {
|
|
130
|
-
|
|
117
|
+
return new AuthError(
|
|
131
118
|
json?.message || fallback || 'Request failed',
|
|
132
119
|
resp.status,
|
|
133
120
|
json?.code || json?.error || 'REQUEST_FAILED',
|
|
134
121
|
json
|
|
135
122
|
);
|
|
136
|
-
return err;
|
|
137
123
|
}
|