@mspkapps/auth-client 0.1.4 → 0.1.6
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 +60 -8
package/package.json
CHANGED
package/src/AuthClient.js
CHANGED
|
@@ -2,13 +2,18 @@
|
|
|
2
2
|
import { AuthError } from './errors.js';
|
|
3
3
|
|
|
4
4
|
export class AuthClient {
|
|
5
|
+
// Convenience: allow users to create with only key/secret
|
|
6
|
+
static create(apiKey, apiSecret, opts = {}) {
|
|
7
|
+
return new AuthClient({ apiKey, apiSecret, ...opts });
|
|
8
|
+
}
|
|
9
|
+
|
|
5
10
|
constructor({
|
|
6
11
|
apiKey,
|
|
7
12
|
apiSecret,
|
|
8
13
|
baseUrl = 'https://cpanel.backend.mspkapps.in/api/v1',
|
|
9
14
|
storage,
|
|
10
15
|
fetch: fetchFn,
|
|
11
|
-
keyInPath =
|
|
16
|
+
keyInPath = true, // default: use headers, not key in URL
|
|
12
17
|
} = {}) {
|
|
13
18
|
if (!apiKey) throw new Error('apiKey is required');
|
|
14
19
|
if (!apiSecret) throw new Error('apiSecret is required'); // do not expose in browsers for prod
|
|
@@ -29,8 +34,8 @@ export class AuthClient {
|
|
|
29
34
|
|
|
30
35
|
// ---------- storage helpers ----------
|
|
31
36
|
_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 {} }
|
|
37
|
+
_save(key, val) { if (!this.storage) return; try { this.storage.setItem(key, val); } catch { } }
|
|
38
|
+
_clear(key) { if (!this.storage) return; try { this.storage.removeItem(key); } catch { } }
|
|
34
39
|
|
|
35
40
|
// ---------- internal builders ----------
|
|
36
41
|
_buildUrl(path) {
|
|
@@ -87,16 +92,63 @@ export class AuthClient {
|
|
|
87
92
|
return json;
|
|
88
93
|
}
|
|
89
94
|
|
|
90
|
-
async
|
|
91
|
-
const resp = await this.fetch(this._buildUrl('auth/
|
|
92
|
-
method: '
|
|
93
|
-
headers: this._headers()
|
|
95
|
+
async requestPasswordReset({ email }) {
|
|
96
|
+
const resp = await this.fetch(this._buildUrl('auth/request-password-reset'), {
|
|
97
|
+
method: 'POST',
|
|
98
|
+
headers: this._headers(),
|
|
99
|
+
body: JSON.stringify({ email })
|
|
100
|
+
});
|
|
101
|
+
const json = await safeJson(resp);
|
|
102
|
+
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Password reset request failed');
|
|
103
|
+
return json;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async changePassword({ currentPassword, newPassword }) {
|
|
107
|
+
const resp = await this.fetch(this._buildUrl('auth/change-password'), {
|
|
108
|
+
method: 'POST',
|
|
109
|
+
headers: this._headers(),
|
|
110
|
+
body: JSON.stringify({ current_password: currentPassword, new_password: newPassword })
|
|
111
|
+
});
|
|
112
|
+
const json = await safeJson(resp);
|
|
113
|
+
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Change password failed');
|
|
114
|
+
return json;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async resendVerificationEmail({ email, purpose }) {
|
|
118
|
+
// purpose: 'New Account' | 'Password change' | 'Profile Edit'
|
|
119
|
+
const resp = await this.fetch(this._buildUrl('auth/resend-verification'), {
|
|
120
|
+
method: 'POST',
|
|
121
|
+
headers: this._headers(),
|
|
122
|
+
body: JSON.stringify({ email, purpose })
|
|
123
|
+
});
|
|
124
|
+
const json = await safeJson(resp);
|
|
125
|
+
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Resend verification failed');
|
|
126
|
+
return json;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async deleteAccount({ email, password }) {
|
|
130
|
+
const resp = await this.fetch(this._buildUrl('auth/delete-account'), {
|
|
131
|
+
method: 'POST',
|
|
132
|
+
headers: this._headers(),
|
|
133
|
+
body: JSON.stringify({ email, password })
|
|
94
134
|
});
|
|
95
135
|
const json = await safeJson(resp);
|
|
96
|
-
if (!resp.ok || json?.success === false) throw toError(resp, json, '
|
|
136
|
+
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Delete account failed');
|
|
97
137
|
return json;
|
|
98
138
|
}
|
|
99
139
|
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
// async getProfile() {
|
|
143
|
+
// const resp = await this.fetch(this._buildUrl('user/profile'), {
|
|
144
|
+
// method: 'GET',
|
|
145
|
+
// headers: this._headers()
|
|
146
|
+
// });
|
|
147
|
+
// const json = await safeJson(resp);
|
|
148
|
+
// if (!resp.ok || json?.success === false) throw toError(resp, json, 'Profile failed');
|
|
149
|
+
// return json;
|
|
150
|
+
// }
|
|
151
|
+
|
|
100
152
|
// Generic authorized call for extra endpoints
|
|
101
153
|
async authed(path, { method = 'GET', body, headers } = {}) {
|
|
102
154
|
const resp = await this.fetch(this._buildUrl(path), {
|