@mspkapps/auth-client 0.1.20 → 0.1.22
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 +83 -3
package/package.json
CHANGED
package/src/AuthClient.js
CHANGED
|
@@ -67,11 +67,11 @@ export class AuthClient {
|
|
|
67
67
|
logout() { this.setToken(null); }
|
|
68
68
|
|
|
69
69
|
// ---------- public API methods ----------
|
|
70
|
-
async register({ email, username, password, name }) {
|
|
70
|
+
async register({ email, username, password, name, extra = {} }) {
|
|
71
71
|
const resp = await this.fetch(this._buildUrl('auth/register'), {
|
|
72
72
|
method: 'POST',
|
|
73
73
|
headers: this._headers(),
|
|
74
|
-
body: JSON.stringify({ email, username, password, name })
|
|
74
|
+
body: JSON.stringify({ email, username, password, name, ...extra })
|
|
75
75
|
});
|
|
76
76
|
const json = await safeJson(resp);
|
|
77
77
|
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Register failed');
|
|
@@ -231,4 +231,84 @@ function toError(resp, json, fallback) {
|
|
|
231
231
|
json?.code || json?.error || 'REQUEST_FAILED',
|
|
232
232
|
json
|
|
233
233
|
);
|
|
234
|
-
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
// ---- Singleton-style convenience API ----
|
|
238
|
+
|
|
239
|
+
// Internal holder
|
|
240
|
+
const _singleton = { client: null };
|
|
241
|
+
|
|
242
|
+
function ensureClient() {
|
|
243
|
+
if (!_singleton.client) {
|
|
244
|
+
throw new Error(
|
|
245
|
+
'AuthClient not initialized. Call authclient.init({ apiKey, apiSecret, ... }) first.'
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
return _singleton.client;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Initialize once in your backend (typically at startup)
|
|
252
|
+
function init({
|
|
253
|
+
apiKey = process.env.MSPK_AUTH_API_KEY,
|
|
254
|
+
apiSecret = process.env.MSPK_AUTH_API_SECRET,
|
|
255
|
+
googleClientId = process.env.GOOGLE_CLIENT_ID,
|
|
256
|
+
baseUrl, // optional override
|
|
257
|
+
storage, // usually omit on backend
|
|
258
|
+
fetch: fetchFn,
|
|
259
|
+
keyInPath,
|
|
260
|
+
} = {}) {
|
|
261
|
+
_singleton.client = new AuthClient({
|
|
262
|
+
apiKey,
|
|
263
|
+
apiSecret,
|
|
264
|
+
googleClientId,
|
|
265
|
+
baseUrl,
|
|
266
|
+
storage,
|
|
267
|
+
fetch: fetchFn,
|
|
268
|
+
keyInPath,
|
|
269
|
+
});
|
|
270
|
+
return _singleton.client;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Thin wrappers delegating to the singleton
|
|
274
|
+
const authclient = {
|
|
275
|
+
init,
|
|
276
|
+
get client() {
|
|
277
|
+
return ensureClient();
|
|
278
|
+
},
|
|
279
|
+
|
|
280
|
+
// auth shortcuts
|
|
281
|
+
login(creds) {
|
|
282
|
+
return ensureClient().login(creds);
|
|
283
|
+
},
|
|
284
|
+
register(data) {
|
|
285
|
+
return ensureClient().register(data);
|
|
286
|
+
},
|
|
287
|
+
googleAuth(tokens) {
|
|
288
|
+
return ensureClient().googleAuth(tokens);
|
|
289
|
+
},
|
|
290
|
+
|
|
291
|
+
// profile helpers
|
|
292
|
+
getProfile() {
|
|
293
|
+
return ensureClient().getProfile();
|
|
294
|
+
},
|
|
295
|
+
updateProfile(updates) {
|
|
296
|
+
return ensureClient().updateProfile(updates);
|
|
297
|
+
},
|
|
298
|
+
|
|
299
|
+
// generic authed call
|
|
300
|
+
authed(path, opts) {
|
|
301
|
+
return ensureClient().authed(path, opts);
|
|
302
|
+
},
|
|
303
|
+
|
|
304
|
+
// token helpers
|
|
305
|
+
setToken(token) {
|
|
306
|
+
return ensureClient().setToken(token);
|
|
307
|
+
},
|
|
308
|
+
logout() {
|
|
309
|
+
return ensureClient().logout();
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
export { authclient, init }; // named exports if someone prefers them
|
|
314
|
+
export default authclient; // default export for your desired DX
|