@authorizerdev/authorizer-js 3.0.0-rc.1 → 3.0.1

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/lib/index.mjs CHANGED
@@ -9,8 +9,7 @@ var DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS = 60;
9
9
  var CLEANUP_IFRAME_TIMEOUT_IN_SECONDS = 2;
10
10
 
11
11
  // src/types.ts
12
- var OAuthProviders;
13
- (function(OAuthProviders2) {
12
+ var OAuthProviders = /* @__PURE__ */ (function(OAuthProviders2) {
14
13
  OAuthProviders2["Apple"] = "apple";
15
14
  OAuthProviders2["Github"] = "github";
16
15
  OAuthProviders2["Google"] = "google";
@@ -21,20 +20,20 @@ var OAuthProviders;
21
20
  OAuthProviders2["Twitch"] = "twitch";
22
21
  OAuthProviders2["Roblox"] = "roblox";
23
22
  OAuthProviders2["Discord"] = "discord";
24
- })(OAuthProviders || (OAuthProviders = {}));
25
- var ResponseTypes;
26
- (function(ResponseTypes2) {
23
+ return OAuthProviders2;
24
+ })({});
25
+ var ResponseTypes = /* @__PURE__ */ (function(ResponseTypes2) {
27
26
  ResponseTypes2["Code"] = "code";
28
27
  ResponseTypes2["Token"] = "token";
29
- })(ResponseTypes || (ResponseTypes = {}));
28
+ return ResponseTypes2;
29
+ })({});
30
30
 
31
31
  // src/utils.ts
32
32
  var hasWindow = /* @__PURE__ */ __name(() => typeof window !== "undefined", "hasWindow");
33
33
  var trimURL = /* @__PURE__ */ __name((url) => {
34
34
  let trimmedData = url.trim();
35
35
  const lastChar = trimmedData[trimmedData.length - 1];
36
- if (lastChar === "/")
37
- trimmedData = trimmedData.slice(0, -1);
36
+ if (lastChar === "/") trimmedData = trimmedData.slice(0, -1);
38
37
  return trimmedData;
39
38
  }, "trimURL");
40
39
  var getCrypto = /* @__PURE__ */ __name(() => {
@@ -103,16 +102,14 @@ var executeIframe = /* @__PURE__ */ __name((authorizeUrl, eventOrigin, timeoutIn
103
102
  }
104
103
  }, "removeIframe");
105
104
  const timeoutSetTimeoutId = setTimeout(() => {
105
+ reject(new Error("Authorization timeout"));
106
106
  removeIframe();
107
107
  }, timeoutInSeconds * 1e3);
108
108
  const iframeEventHandler = /* @__PURE__ */ __name(function(e) {
109
- if (e.origin !== eventOrigin)
110
- return;
111
- if (!e.data || !e.data.response)
112
- return;
109
+ if (e.origin !== eventOrigin) return;
110
+ if (!e.data || !e.data.response) return;
113
111
  const eventSource = e.source;
114
- if (eventSource)
115
- eventSource.close();
112
+ if (eventSource) eventSource.close();
116
113
  e.data.response.error ? reject(e.data.response) : resolve(e.data.response);
117
114
  clearTimeout(timeoutSetTimeoutId);
118
115
  window.removeEventListener("message", iframeEventHandler, false);
@@ -134,17 +131,12 @@ var _Authorizer = class _Authorizer {
134
131
  codeVerifier;
135
132
  // constructor
136
133
  constructor(config) {
137
- if (!config)
138
- throw new Error("Configuration is required");
134
+ if (!config) throw new Error("Configuration is required");
139
135
  this.config = config;
140
- if (!config.authorizerURL && !config.authorizerURL.trim())
141
- throw new Error("Invalid authorizerURL");
142
- if (config.authorizerURL)
143
- this.config.authorizerURL = trimURL(config.authorizerURL);
144
- if (!config.redirectURL && !config.redirectURL.trim())
145
- throw new Error("Invalid redirectURL");
146
- else
147
- this.config.redirectURL = trimURL(config.redirectURL);
136
+ if (!config.authorizerURL && !config.authorizerURL.trim()) throw new Error("Invalid authorizerURL");
137
+ if (config.authorizerURL) this.config.authorizerURL = trimURL(config.authorizerURL);
138
+ if (!config.redirectURL && !config.redirectURL.trim()) throw new Error("Invalid redirectURL");
139
+ else this.config.redirectURL = trimURL(config.redirectURL);
148
140
  this.config.extraHeaders = {
149
141
  ...config.extraHeaders || {},
150
142
  "x-authorizer-url": this.config.authorizerURL,
@@ -153,19 +145,17 @@ var _Authorizer = class _Authorizer {
153
145
  };
154
146
  this.config.clientID = ((config == null ? void 0 : config.clientID) || "").trim();
155
147
  }
156
- authorize = async (data) => {
148
+ authorize = /* @__PURE__ */ __name(async (data) => {
157
149
  var _a;
158
- if (!hasWindow())
159
- return this.errorResponse([
160
- new Error("this feature is only supported in browser")
161
- ]);
150
+ if (!hasWindow()) return this.errorResponse([
151
+ new Error("this feature is only supported in browser")
152
+ ]);
162
153
  const scopes = [
163
154
  "openid",
164
155
  "profile",
165
156
  "email"
166
157
  ];
167
- if (data.use_refresh_token)
168
- scopes.push("offline_access");
158
+ if (data.use_refresh_token) scopes.push("offline_access");
169
159
  const requestData = {
170
160
  redirect_uri: this.config.redirectURL,
171
161
  response_mode: data.response_mode || "web_message",
@@ -197,12 +187,16 @@ var _Authorizer = class _Authorizer {
197
187
  return this.okResponse(iframeRes);
198
188
  } catch (err) {
199
189
  if (err.error) {
200
- window.location.replace(`${this.config.authorizerURL}/app?state=${encode(JSON.stringify(this.config))}&redirect_uri=${this.config.redirectURL}`);
190
+ window.location.replace(`${this.config.authorizerURL}/app?state=${encode(JSON.stringify({
191
+ clientID: this.config.clientID,
192
+ redirectURL: this.config.redirectURL,
193
+ authorizerURL: this.config.authorizerURL
194
+ }))}&redirect_uri=${encodeURIComponent(this.config.redirectURL || "")}`);
201
195
  }
202
196
  return this.errorResponse(err);
203
197
  }
204
- };
205
- browserLogin = async () => {
198
+ }, "authorize");
199
+ browserLogin = /* @__PURE__ */ __name(async () => {
206
200
  try {
207
201
  const tokenResp = await this.getSession();
208
202
  return tokenResp.errors.length ? this.errorResponse(tokenResp.errors) : this.okResponse(tokenResp.data);
@@ -215,16 +209,18 @@ var _Authorizer = class _Authorizer {
215
209
  ]
216
210
  };
217
211
  }
218
- window.location.replace(`${this.config.authorizerURL}/app?state=${encode(JSON.stringify(this.config))}&redirect_uri=${this.config.redirectURL}`);
212
+ window.location.replace(`${this.config.authorizerURL}/app?state=${encode(JSON.stringify({
213
+ clientID: this.config.clientID,
214
+ redirectURL: this.config.redirectURL,
215
+ authorizerURL: this.config.authorizerURL
216
+ }))}&redirect_uri=${encodeURIComponent(this.config.redirectURL || "")}`);
219
217
  return this.errorResponse(err);
220
218
  }
221
- };
222
- forgotPassword = async (data) => {
219
+ }, "browserLogin");
220
+ forgotPassword = /* @__PURE__ */ __name(async (data) => {
223
221
  var _a;
224
- if (!data.state)
225
- data.state = encode(createRandomString());
226
- if (!data.redirect_uri)
227
- data.redirect_uri = this.config.redirectURL;
222
+ if (!data.state) data.state = encode(createRandomString());
223
+ if (!data.redirect_uri) data.redirect_uri = this.config.redirectURL;
228
224
  try {
229
225
  const forgotPasswordResp = await this.graphqlQuery({
230
226
  query: "mutation forgotPassword($data: ForgotPasswordRequest!) { forgot_password(params: $data) { message should_show_mobile_otp_screen } }",
@@ -238,8 +234,8 @@ var _Authorizer = class _Authorizer {
238
234
  error
239
235
  ]);
240
236
  }
241
- };
242
- getMetaData = async () => {
237
+ }, "forgotPassword");
238
+ getMetaData = /* @__PURE__ */ __name(async () => {
243
239
  var _a;
244
240
  try {
245
241
  const res = await this.graphqlQuery({
@@ -251,8 +247,8 @@ var _Authorizer = class _Authorizer {
251
247
  error
252
248
  ]);
253
249
  }
254
- };
255
- getProfile = async (headers) => {
250
+ }, "getMetaData");
251
+ getProfile = /* @__PURE__ */ __name(async (headers) => {
256
252
  var _a;
257
253
  try {
258
254
  const profileRes = await this.graphqlQuery({
@@ -265,9 +261,9 @@ var _Authorizer = class _Authorizer {
265
261
  error
266
262
  ]);
267
263
  }
268
- };
264
+ }, "getProfile");
269
265
  // this is used to verify / get session using cookie by default. If using node.js pass authorization header
270
- getSession = async (headers, params) => {
266
+ getSession = /* @__PURE__ */ __name(async (headers, params) => {
271
267
  var _a, _b;
272
268
  try {
273
269
  const res = await this.graphqlQuery({
@@ -281,18 +277,15 @@ var _Authorizer = class _Authorizer {
281
277
  } catch (err) {
282
278
  return this.errorResponse(err);
283
279
  }
284
- };
285
- getToken = async (data) => {
286
- if (!data.grant_type)
287
- data.grant_type = "authorization_code";
288
- if (data.grant_type === "refresh_token" && !data.refresh_token)
289
- return this.errorResponse([
290
- new Error("Invalid refresh_token")
291
- ]);
292
- if (data.grant_type === "authorization_code" && !this.codeVerifier)
293
- return this.errorResponse([
294
- new Error("Invalid code verifier")
295
- ]);
280
+ }, "getSession");
281
+ getToken = /* @__PURE__ */ __name(async (data) => {
282
+ if (!data.grant_type) data.grant_type = "authorization_code";
283
+ if (data.grant_type === "refresh_token" && !data.refresh_token) return this.errorResponse([
284
+ new Error("Invalid refresh_token")
285
+ ]);
286
+ if (data.grant_type === "authorization_code" && !this.codeVerifier) return this.errorResponse([
287
+ new Error("Invalid code verifier")
288
+ ]);
296
289
  const requestData = {
297
290
  client_id: this.config.clientID,
298
291
  code: data.code || "",
@@ -311,16 +304,15 @@ var _Authorizer = class _Authorizer {
311
304
  credentials: "include"
312
305
  });
313
306
  const json = await res.json();
314
- if (res.status >= 400)
315
- return this.errorResponse([
316
- new Error(json.error_description || json.error)
317
- ]);
307
+ if (res.status >= 400) return this.errorResponse([
308
+ new Error(json.error_description || json.error)
309
+ ]);
318
310
  return this.okResponse(json);
319
311
  } catch (err) {
320
312
  return this.errorResponse(err);
321
313
  }
322
- };
323
- login = async (data) => {
314
+ }, "getToken");
315
+ login = /* @__PURE__ */ __name(async (data) => {
324
316
  var _a, _b;
325
317
  try {
326
318
  const res = await this.graphqlQuery({
@@ -337,8 +329,8 @@ var _Authorizer = class _Authorizer {
337
329
  new Error(err)
338
330
  ]);
339
331
  }
340
- };
341
- logout = async (headers) => {
332
+ }, "login");
333
+ logout = /* @__PURE__ */ __name(async (headers) => {
342
334
  var _a, _b;
343
335
  try {
344
336
  const res = await this.graphqlQuery({
@@ -351,14 +343,12 @@ var _Authorizer = class _Authorizer {
351
343
  err
352
344
  ]);
353
345
  }
354
- };
355
- magicLinkLogin = async (data) => {
346
+ }, "logout");
347
+ magicLinkLogin = /* @__PURE__ */ __name(async (data) => {
356
348
  var _a, _b;
357
349
  try {
358
- if (!data.state)
359
- data.state = encode(createRandomString());
360
- if (!data.redirect_uri)
361
- data.redirect_uri = this.config.redirectURL;
350
+ if (!data.state) data.state = encode(createRandomString());
351
+ if (!data.redirect_uri) data.redirect_uri = this.config.redirectURL;
362
352
  const res = await this.graphqlQuery({
363
353
  query: `
364
354
  mutation magicLinkLogin($data: MagicLinkLoginRequest!) { magic_link_login(params: $data) { message }}
@@ -373,8 +363,8 @@ var _Authorizer = class _Authorizer {
373
363
  err
374
364
  ]);
375
365
  }
376
- };
377
- oauthLogin = async (oauthProvider, roles, redirect_uri, state) => {
366
+ }, "magicLinkLogin");
367
+ oauthLogin = /* @__PURE__ */ __name(async (oauthProvider, roles, redirect_uri, state) => {
378
368
  let urlState = state;
379
369
  if (!urlState) {
380
370
  urlState = encode(createRandomString());
@@ -382,13 +372,11 @@ var _Authorizer = class _Authorizer {
382
372
  if (!Object.values(OAuthProviders).includes(oauthProvider)) {
383
373
  throw new Error(`only following oauth providers are supported: ${Object.values(oauthProvider).toString()}`);
384
374
  }
385
- if (!hasWindow())
386
- throw new Error("oauthLogin is only supported for browsers");
387
- if (roles && roles.length)
388
- urlState += `&roles=${roles.join(",")}`;
389
- window.location.replace(`${this.config.authorizerURL}/oauth_login/${oauthProvider}?redirect_uri=${redirect_uri || this.config.redirectURL}&state=${urlState}`);
390
- };
391
- resendOtp = async (data) => {
375
+ if (!hasWindow()) throw new Error("oauthLogin is only supported for browsers");
376
+ if (roles && roles.length) urlState += `&roles=${roles.join(",")}`;
377
+ window.location.replace(`${this.config.authorizerURL}/oauth_login/${oauthProvider}?redirect_uri=${encodeURIComponent(redirect_uri || this.config.redirectURL || "")}&state=${encodeURIComponent(urlState)}`);
378
+ }, "oauthLogin");
379
+ resendOtp = /* @__PURE__ */ __name(async (data) => {
392
380
  var _a, _b;
393
381
  try {
394
382
  const res = await this.graphqlQuery({
@@ -405,8 +393,8 @@ var _Authorizer = class _Authorizer {
405
393
  err
406
394
  ]);
407
395
  }
408
- };
409
- resetPassword = async (data) => {
396
+ }, "resendOtp");
397
+ resetPassword = /* @__PURE__ */ __name(async (data) => {
410
398
  var _a, _b;
411
399
  try {
412
400
  const resetPasswordRes = await this.graphqlQuery({
@@ -421,12 +409,11 @@ var _Authorizer = class _Authorizer {
421
409
  error
422
410
  ]);
423
411
  }
424
- };
425
- revokeToken = async (data) => {
426
- if (!data.refresh_token && !data.refresh_token.trim())
427
- return this.errorResponse([
428
- new Error("Invalid refresh_token")
429
- ]);
412
+ }, "resetPassword");
413
+ revokeToken = /* @__PURE__ */ __name(async (data) => {
414
+ if (!data.refresh_token && !data.refresh_token.trim()) return this.errorResponse([
415
+ new Error("Invalid refresh_token")
416
+ ]);
430
417
  const fetcher = getFetcher();
431
418
  const res = await fetcher(`${this.config.authorizerURL}/oauth/revoke`, {
432
419
  method: "POST",
@@ -440,8 +427,8 @@ var _Authorizer = class _Authorizer {
440
427
  });
441
428
  const responseData = await res.json();
442
429
  return this.okResponse(responseData);
443
- };
444
- signup = async (data) => {
430
+ }, "revokeToken");
431
+ signup = /* @__PURE__ */ __name(async (data) => {
445
432
  var _a, _b;
446
433
  try {
447
434
  const res = await this.graphqlQuery({
@@ -458,8 +445,8 @@ var _Authorizer = class _Authorizer {
458
445
  err
459
446
  ]);
460
447
  }
461
- };
462
- updateProfile = async (data, headers) => {
448
+ }, "signup");
449
+ updateProfile = /* @__PURE__ */ __name(async (data, headers) => {
463
450
  var _a, _b;
464
451
  try {
465
452
  const updateProfileRes = await this.graphqlQuery({
@@ -475,8 +462,8 @@ var _Authorizer = class _Authorizer {
475
462
  error
476
463
  ]);
477
464
  }
478
- };
479
- deactivateAccount = async (headers) => {
465
+ }, "updateProfile");
466
+ deactivateAccount = /* @__PURE__ */ __name(async (headers) => {
480
467
  var _a, _b;
481
468
  try {
482
469
  const res = await this.graphqlQuery({
@@ -489,8 +476,8 @@ var _Authorizer = class _Authorizer {
489
476
  error
490
477
  ]);
491
478
  }
492
- };
493
- validateJWTToken = async (params) => {
479
+ }, "deactivateAccount");
480
+ validateJWTToken = /* @__PURE__ */ __name(async (params) => {
494
481
  var _a, _b;
495
482
  try {
496
483
  const res = await this.graphqlQuery({
@@ -505,8 +492,8 @@ var _Authorizer = class _Authorizer {
505
492
  error
506
493
  ]);
507
494
  }
508
- };
509
- validateSession = async (params) => {
495
+ }, "validateJWTToken");
496
+ validateSession = /* @__PURE__ */ __name(async (params) => {
510
497
  var _a, _b;
511
498
  try {
512
499
  const res = await this.graphqlQuery({
@@ -521,8 +508,8 @@ var _Authorizer = class _Authorizer {
521
508
  error
522
509
  ]);
523
510
  }
524
- };
525
- verifyEmail = async (data) => {
511
+ }, "validateSession");
512
+ verifyEmail = /* @__PURE__ */ __name(async (data) => {
526
513
  var _a, _b;
527
514
  try {
528
515
  const res = await this.graphqlQuery({
@@ -539,8 +526,8 @@ var _Authorizer = class _Authorizer {
539
526
  err
540
527
  ]);
541
528
  }
542
- };
543
- resendVerifyEmail = async (data) => {
529
+ }, "verifyEmail");
530
+ resendVerifyEmail = /* @__PURE__ */ __name(async (data) => {
544
531
  var _a, _b;
545
532
  try {
546
533
  const res = await this.graphqlQuery({
@@ -557,8 +544,8 @@ var _Authorizer = class _Authorizer {
557
544
  err
558
545
  ]);
559
546
  }
560
- };
561
- verifyOtp = async (data) => {
547
+ }, "resendVerifyEmail");
548
+ verifyOtp = /* @__PURE__ */ __name(async (data) => {
562
549
  var _a, _b;
563
550
  try {
564
551
  const res = await this.graphqlQuery({
@@ -575,10 +562,10 @@ var _Authorizer = class _Authorizer {
575
562
  err
576
563
  ]);
577
564
  }
578
- };
565
+ }, "verifyOtp");
579
566
  // helper to execute graphql queries
580
567
  // takes in any query or mutation string as value
581
- graphqlQuery = async (data) => {
568
+ graphqlQuery = /* @__PURE__ */ __name(async (data) => {
582
569
  var _a;
583
570
  const fetcher = getFetcher();
584
571
  const res = await fetcher(`${this.config.authorizerURL}/graphql`, {
@@ -604,19 +591,19 @@ var _Authorizer = class _Authorizer {
604
591
  data: json.data,
605
592
  errors: []
606
593
  };
607
- };
608
- errorResponse = (errors) => {
594
+ }, "graphqlQuery");
595
+ errorResponse = /* @__PURE__ */ __name((errors) => {
609
596
  return {
610
597
  data: void 0,
611
598
  errors
612
599
  };
613
- };
614
- okResponse = (data) => {
600
+ }, "errorResponse");
601
+ okResponse = /* @__PURE__ */ __name((data) => {
615
602
  return {
616
603
  data,
617
604
  errors: []
618
605
  };
619
- };
606
+ }, "okResponse");
620
607
  };
621
608
  __name(_Authorizer, "Authorizer");
622
609
  var Authorizer = _Authorizer;