@mspkapps/auth-client 0.1.21 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/AuthClient.js +81 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mspkapps/auth-client",
3
- "version": "0.1.21",
3
+ "version": "0.1.22",
4
4
  "description": "Lightweight client for Your Auth Service",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/AuthClient.js CHANGED
@@ -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