@authorizerdev/authorizer-js 1.1.0 → 1.1.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/src/index.ts CHANGED
@@ -52,69 +52,6 @@ export class Authorizer {
52
52
  this.config.clientID = config.clientID.trim();
53
53
  }
54
54
 
55
- revokeToken = async (data: { refresh_token: string }) => {
56
- if (!data.refresh_token && !data.refresh_token.trim()) {
57
- throw new Error(`Invalid refresh_token`);
58
- }
59
-
60
- const fetcher = getFetcher();
61
- const res = await fetcher(this.config.authorizerURL + '/oauth/revoke', {
62
- method: 'POST',
63
- headers: {
64
- ...this.config.extraHeaders,
65
- },
66
- body: JSON.stringify({
67
- refresh_token: data.refresh_token,
68
- client_id: this.config.clientID,
69
- }),
70
- });
71
-
72
- return await res.json();
73
- };
74
-
75
- getToken = async (
76
- data: Types.GetTokenInput,
77
- ): Promise<Types.GetTokenResponse> => {
78
- if (!data.grant_type) {
79
- data.grant_type = 'authorization_code';
80
- }
81
-
82
- if (data.grant_type === 'refresh_token' && !data.refresh_token) {
83
- throw new Error(`Invalid refresh_token`);
84
- }
85
- if (data.grant_type === 'authorization_code' && !this.codeVerifier) {
86
- throw new Error(`Invalid code verifier`);
87
- }
88
-
89
- const requestData = {
90
- client_id: this.config.clientID,
91
- code: data.code || '',
92
- code_verifier: this.codeVerifier || '',
93
- grant_type: data.grant_type || '',
94
- refresh_token: data.refresh_token || '',
95
- };
96
-
97
- try {
98
- const fetcher = getFetcher();
99
- const res = await fetcher(`${this.config.authorizerURL}/oauth/token`, {
100
- method: 'POST',
101
- body: JSON.stringify(requestData),
102
- headers: {
103
- ...this.config.extraHeaders,
104
- },
105
- credentials: 'include',
106
- });
107
-
108
- const json = await res.json();
109
- if (res.status >= 400) {
110
- throw new Error(json);
111
- }
112
- return json;
113
- } catch (err) {
114
- throw err;
115
- }
116
- };
117
-
118
55
  authorize = async (data: Types.AuthorizeInput) => {
119
56
  if (!hasWindow()) {
120
57
  throw new Error(`this feature is only supported in browser`);
@@ -174,31 +111,45 @@ export class Authorizer {
174
111
  }
175
112
  };
176
113
 
177
- // helper to execute graphql queries
178
- // takes in any query or mutation string as input
179
- graphqlQuery = async (data: Types.GraphqlQueryInput) => {
180
- const fetcher = getFetcher();
181
- const res = await fetcher(this.config.authorizerURL + '/graphql', {
182
- method: 'POST',
183
- body: JSON.stringify({
184
- query: data.query,
185
- variables: data.variables || {},
186
- }),
187
- headers: {
188
- ...this.config.extraHeaders,
189
- ...(data.headers || {}),
190
- },
191
- credentials: 'include',
192
- });
114
+ browserLogin = async (): Promise<Types.AuthToken | void> => {
115
+ try {
116
+ const token = await this.getSession();
117
+ return token;
118
+ } catch (err) {
119
+ if (!hasWindow()) {
120
+ throw new Error(`browserLogin is only supported for browsers`);
121
+ }
122
+ window.location.replace(
123
+ `${this.config.authorizerURL}/app?state=${encode(
124
+ JSON.stringify(this.config),
125
+ )}&redirect_uri=${this.config.redirectURL}`,
126
+ );
127
+ }
128
+ };
193
129
 
194
- const json = await res.json();
130
+ forgotPassword = async (
131
+ data: Types.ForgotPasswordInput,
132
+ ): Promise<Types.Response | void> => {
133
+ if (!data.state) {
134
+ data.state = encode(createRandomString());
135
+ }
195
136
 
196
- if (json.errors && json.errors.length) {
197
- console.error(json.errors);
198
- throw new Error(json.errors[0].message);
137
+ if (!data.redirect_uri) {
138
+ data.redirect_uri = this.config.redirectURL;
199
139
  }
200
140
 
201
- return json.data;
141
+ try {
142
+ const forgotPasswordRes = await this.graphqlQuery({
143
+ query: `mutation forgotPassword($data: ForgotPasswordInput!) { forgot_password(params: $data) { message } }`,
144
+ variables: {
145
+ data,
146
+ },
147
+ });
148
+
149
+ return forgotPasswordRes.forgot_password;
150
+ } catch (error) {
151
+ throw error;
152
+ }
202
153
  };
203
154
 
204
155
  getMetaData = async (): Promise<Types.MetaData | void> => {
@@ -213,6 +164,19 @@ export class Authorizer {
213
164
  }
214
165
  };
215
166
 
167
+ getProfile = async (headers?: Types.Headers): Promise<Types.User | void> => {
168
+ try {
169
+ const profileRes = await this.graphqlQuery({
170
+ query: `query { profile { ${userFragment} } }`,
171
+ headers,
172
+ });
173
+
174
+ return profileRes.profile;
175
+ } catch (error) {
176
+ throw error;
177
+ }
178
+ };
179
+
216
180
  // this is used to verify / get session using cookie by default. If using nodejs pass authorization header
217
181
  getSession = async (
218
182
  headers?: Types.Headers,
@@ -232,61 +196,74 @@ export class Authorizer {
232
196
  }
233
197
  };
234
198
 
235
- magicLinkLogin = async (
236
- data: Types.MagicLinkLoginInput,
237
- ): Promise<Types.Response> => {
238
- try {
239
- if (!data.state) {
240
- data.state = encode(createRandomString());
241
- }
242
-
243
- if (!data.redirect_uri) {
244
- data.redirect_uri = this.config.redirectURL;
245
- }
246
-
247
- const res = await this.graphqlQuery({
248
- query: `
249
- mutation magicLinkLogin($data: MagicLinkLoginInput!) { magic_link_login(params: $data) { message }}
250
- `,
251
- variables: { data },
252
- });
199
+ getToken = async (
200
+ data: Types.GetTokenInput,
201
+ ): Promise<Types.GetTokenResponse> => {
202
+ if (!data.grant_type) {
203
+ data.grant_type = 'authorization_code';
204
+ }
253
205
 
254
- return res.magic_link_login;
255
- } catch (err) {
256
- throw err;
206
+ if (data.grant_type === 'refresh_token' && !data.refresh_token) {
207
+ throw new Error(`Invalid refresh_token`);
208
+ }
209
+ if (data.grant_type === 'authorization_code' && !this.codeVerifier) {
210
+ throw new Error(`Invalid code verifier`);
257
211
  }
258
- };
259
212
 
260
- signup = async (data: Types.SignupInput): Promise<Types.AuthToken | void> => {
213
+ const requestData = {
214
+ client_id: this.config.clientID,
215
+ code: data.code || '',
216
+ code_verifier: this.codeVerifier || '',
217
+ grant_type: data.grant_type || '',
218
+ refresh_token: data.refresh_token || '',
219
+ };
220
+
261
221
  try {
262
- const res = await this.graphqlQuery({
263
- query: `
264
- mutation signup($data: SignUpInput!) { signup(params: $data) { ${authTokenFragment}}}
265
- `,
266
- variables: { data },
222
+ const fetcher = getFetcher();
223
+ const res = await fetcher(`${this.config.authorizerURL}/oauth/token`, {
224
+ method: 'POST',
225
+ body: JSON.stringify(requestData),
226
+ headers: {
227
+ ...this.config.extraHeaders,
228
+ },
229
+ credentials: 'include',
267
230
  });
268
231
 
269
- return res.signup;
232
+ const json = await res.json();
233
+ if (res.status >= 400) {
234
+ throw new Error(json);
235
+ }
236
+ return json;
270
237
  } catch (err) {
271
238
  throw err;
272
239
  }
273
240
  };
274
241
 
275
- verifyEmail = async (
276
- data: Types.VerifyEmailInput,
277
- ): Promise<Types.AuthToken | void> => {
278
- try {
279
- const res = await this.graphqlQuery({
280
- query: `
281
- mutation verifyEmail($data: VerifyEmailInput!) { verify_email(params: $data) { ${authTokenFragment}}}
282
- `,
283
- variables: { data },
284
- });
242
+ // helper to execute graphql queries
243
+ // takes in any query or mutation string as input
244
+ graphqlQuery = async (data: Types.GraphqlQueryInput) => {
245
+ const fetcher = getFetcher();
246
+ const res = await fetcher(this.config.authorizerURL + '/graphql', {
247
+ method: 'POST',
248
+ body: JSON.stringify({
249
+ query: data.query,
250
+ variables: data.variables || {},
251
+ }),
252
+ headers: {
253
+ ...this.config.extraHeaders,
254
+ ...(data.headers || {}),
255
+ },
256
+ credentials: 'include',
257
+ });
285
258
 
286
- return res.verify_email;
287
- } catch (err) {
288
- throw err;
259
+ const json = await res.json();
260
+
261
+ if (json.errors && json.errors.length) {
262
+ console.error(json.errors);
263
+ throw new Error(json.errors[0].message);
289
264
  }
265
+
266
+ return json.data;
290
267
  };
291
268
 
292
269
  login = async (data: Types.LoginInput): Promise<Types.AuthToken | void> => {
@@ -304,92 +281,40 @@ export class Authorizer {
304
281
  }
305
282
  };
306
283
 
307
- getProfile = async (headers?: Types.Headers): Promise<Types.User | void> => {
308
- try {
309
- const profileRes = await this.graphqlQuery({
310
- query: `query { profile { ${userFragment} } }`,
311
- headers,
312
- });
313
-
314
- return profileRes.profile;
315
- } catch (error) {
316
- throw error;
317
- }
318
- };
319
-
320
- updateProfile = async (
321
- data: Types.UpdateProfileInput,
322
- headers?: Types.Headers,
323
- ): Promise<Types.Response | void> => {
284
+ logout = async (headers?: Types.Headers): Promise<Types.Response | void> => {
324
285
  try {
325
- const updateProfileRes = await this.graphqlQuery({
326
- query: `mutation updateProfile($data: UpdateProfileInput!) { update_profile(params: $data) { message } }`,
286
+ const res = await this.graphqlQuery({
287
+ query: ` mutation { logout { message } } `,
327
288
  headers,
328
- variables: {
329
- data,
330
- },
331
289
  });
332
-
333
- return updateProfileRes.update_profile;
334
- } catch (error) {
335
- throw error;
290
+ return res.logout;
291
+ } catch (err) {
292
+ console.error(err);
336
293
  }
337
294
  };
338
295
 
339
- forgotPassword = async (
340
- data: Types.ForgotPasswordInput,
341
- ): Promise<Types.Response | void> => {
342
- if (!data.state) {
343
- data.state = encode(createRandomString());
344
- }
345
-
346
- if (!data.redirect_uri) {
347
- data.redirect_uri = this.config.redirectURL;
348
- }
349
-
296
+ magicLinkLogin = async (
297
+ data: Types.MagicLinkLoginInput,
298
+ ): Promise<Types.Response> => {
350
299
  try {
351
- const forgotPasswordRes = await this.graphqlQuery({
352
- query: `mutation forgotPassword($data: ForgotPasswordInput!) { forgot_password(params: $data) { message } }`,
353
- variables: {
354
- data,
355
- },
356
- });
300
+ if (!data.state) {
301
+ data.state = encode(createRandomString());
302
+ }
357
303
 
358
- return forgotPasswordRes.forgot_password;
359
- } catch (error) {
360
- throw error;
361
- }
362
- };
304
+ if (!data.redirect_uri) {
305
+ data.redirect_uri = this.config.redirectURL;
306
+ }
363
307
 
364
- resetPassword = async (
365
- data: Types.ResetPasswordInput,
366
- ): Promise<Types.Response | void> => {
367
- try {
368
- const resetPasswordRes = await this.graphqlQuery({
369
- query: `mutation resetPassword($data: ResetPasswordInput!) { reset_password(params: $data) { message } }`,
370
- variables: {
371
- data,
372
- },
308
+ const res = await this.graphqlQuery({
309
+ query: `
310
+ mutation magicLinkLogin($data: MagicLinkLoginInput!) { magic_link_login(params: $data) { message }}
311
+ `,
312
+ variables: { data },
373
313
  });
374
- return resetPasswordRes.reset_password;
375
- } catch (error) {
376
- throw error;
377
- }
378
- };
379
314
 
380
- browserLogin = async (): Promise<Types.AuthToken | void> => {
381
- try {
382
- const token = await this.getSession();
383
- return token;
315
+ return res.magic_link_login;
384
316
  } catch (err) {
385
- if (!hasWindow()) {
386
- throw new Error(`browserLogin is only supported for browsers`);
387
- }
388
- window.location.replace(
389
- `${this.config.authorizerURL}/app?state=${encode(
390
- JSON.stringify(this.config),
391
- )}&redirect_uri=${this.config.redirectURL}`,
392
- );
317
+ throw err;
393
318
  }
394
319
  };
395
320
 
@@ -423,15 +348,90 @@ export class Authorizer {
423
348
  );
424
349
  };
425
350
 
426
- logout = async (headers?: Types.Headers): Promise<Types.Response | void> => {
351
+ resendOtp = async (
352
+ data: Types.ResendOtpInput,
353
+ ): Promise<Types.Response | void> => {
427
354
  try {
428
355
  const res = await this.graphqlQuery({
429
- query: ` mutation { logout { message } } `,
430
- headers,
356
+ query: `
357
+ mutation resendOtp($data: ResendOTPRequest!) { resend_otp(params: $data) { message }}
358
+ `,
359
+ variables: { data },
431
360
  });
432
- return res.logout;
361
+
362
+ return res.resend_otp;
433
363
  } catch (err) {
434
- console.error(err);
364
+ throw err;
365
+ }
366
+ };
367
+
368
+ resetPassword = async (
369
+ data: Types.ResetPasswordInput,
370
+ ): Promise<Types.Response | void> => {
371
+ try {
372
+ const resetPasswordRes = await this.graphqlQuery({
373
+ query: `mutation resetPassword($data: ResetPasswordInput!) { reset_password(params: $data) { message } }`,
374
+ variables: {
375
+ data,
376
+ },
377
+ });
378
+ return resetPasswordRes.reset_password;
379
+ } catch (error) {
380
+ throw error;
381
+ }
382
+ };
383
+
384
+ revokeToken = async (data: { refresh_token: string }) => {
385
+ if (!data.refresh_token && !data.refresh_token.trim()) {
386
+ throw new Error(`Invalid refresh_token`);
387
+ }
388
+
389
+ const fetcher = getFetcher();
390
+ const res = await fetcher(this.config.authorizerURL + '/oauth/revoke', {
391
+ method: 'POST',
392
+ headers: {
393
+ ...this.config.extraHeaders,
394
+ },
395
+ body: JSON.stringify({
396
+ refresh_token: data.refresh_token,
397
+ client_id: this.config.clientID,
398
+ }),
399
+ });
400
+
401
+ return await res.json();
402
+ };
403
+
404
+ signup = async (data: Types.SignupInput): Promise<Types.AuthToken | void> => {
405
+ try {
406
+ const res = await this.graphqlQuery({
407
+ query: `
408
+ mutation signup($data: SignUpInput!) { signup(params: $data) { ${authTokenFragment}}}
409
+ `,
410
+ variables: { data },
411
+ });
412
+
413
+ return res.signup;
414
+ } catch (err) {
415
+ throw err;
416
+ }
417
+ };
418
+
419
+ updateProfile = async (
420
+ data: Types.UpdateProfileInput,
421
+ headers?: Types.Headers,
422
+ ): Promise<Types.Response | void> => {
423
+ try {
424
+ const updateProfileRes = await this.graphqlQuery({
425
+ query: `mutation updateProfile($data: UpdateProfileInput!) { update_profile(params: $data) { message } }`,
426
+ headers,
427
+ variables: {
428
+ data,
429
+ },
430
+ });
431
+
432
+ return updateProfileRes.update_profile;
433
+ } catch (error) {
434
+ throw error;
435
435
  }
436
436
  };
437
437
 
@@ -440,7 +440,7 @@ export class Authorizer {
440
440
  ): Promise<Types.ValidateJWTTokenResponse> => {
441
441
  try {
442
442
  const res = await this.graphqlQuery({
443
- query: `query validateJWTToken($params: ValidateJWTTokenInput!){validate_jwt_token(params: $params) { is_valid } }`,
443
+ query: `query validateJWTToken($params: ValidateJWTTokenInput!){validate_jwt_token(params: $params) { is_valid claims } }`,
444
444
  variables: {
445
445
  params,
446
446
  },
@@ -452,35 +452,35 @@ export class Authorizer {
452
452
  }
453
453
  };
454
454
 
455
- verifyOtp = async (
456
- data: Types.VerifyOtpInput,
455
+ verifyEmail = async (
456
+ data: Types.VerifyEmailInput,
457
457
  ): Promise<Types.AuthToken | void> => {
458
458
  try {
459
459
  const res = await this.graphqlQuery({
460
460
  query: `
461
- mutation verifyOtp($data: VerifyOTPRequest!) { verify_otp(params: $data) { ${authTokenFragment}}}
461
+ mutation verifyEmail($data: VerifyEmailInput!) { verify_email(params: $data) { ${authTokenFragment}}}
462
462
  `,
463
463
  variables: { data },
464
464
  });
465
465
 
466
- return res.verify_otp;
466
+ return res.verify_email;
467
467
  } catch (err) {
468
468
  throw err;
469
469
  }
470
470
  };
471
471
 
472
- resendOtp = async (
473
- data: Types.ResendOtpInput,
474
- ): Promise<Types.Response | void> => {
472
+ verifyOtp = async (
473
+ data: Types.VerifyOtpInput,
474
+ ): Promise<Types.AuthToken | void> => {
475
475
  try {
476
476
  const res = await this.graphqlQuery({
477
477
  query: `
478
- mutation resendOtp($data: ResendOTPRequest!) { resend_otp(params: $data) { message }}
478
+ mutation verifyOtp($data: VerifyOTPRequest!) { verify_otp(params: $data) { ${authTokenFragment}}}
479
479
  `,
480
480
  variables: { data },
481
481
  });
482
482
 
483
- return res.resend_otp;
483
+ return res.verify_otp;
484
484
  } catch (err) {
485
485
  throw err;
486
486
  }