@redzone/taunt-logins 0.0.7 → 0.0.9

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.
@@ -468,28 +468,39 @@ function ErrorFromResponse(response) {
468
468
  }
469
469
 
470
470
  class TauntApi {
471
- async stdPGET(method, url, body, headers = {}) {
472
- const response = await this.api.request({
471
+ async _api(method, url, body, headers = {}) {
472
+ headers = {
473
+ "Content-Type": "application/json",
474
+ Accept: "application/json",
475
+ ...headers
476
+ };
477
+ if (this.accessToken) {
478
+ headers = {
479
+ ...headers,
480
+ Authorization: `Bearer ${this.accessToken}`
481
+ };
482
+ }
483
+ const response = await this._axios.request({
473
484
  url,
474
485
  method,
475
- headers: {
476
- "Content-Type": "application/json",
477
- Accept: "application/json",
478
- ...headers
479
- },
480
- data: body ? JSON.stringify(body) : null
486
+ headers,
487
+ data: body ? JSON.stringify(body) : null,
488
+ withCredentials: true
481
489
  });
482
490
  const data = response.data;
483
- if (data.refreshToken) {
491
+ if (data?.refreshToken) {
484
492
  this.refreshToken = data.refreshToken;
485
493
  }
494
+ if (data?.accessToken) {
495
+ this.accessToken = data.accessToken;
496
+ }
486
497
  return data;
487
498
  }
488
499
  async post(url, body = {}, headers = {}) {
489
- return this.stdPGET("POST", url, body, headers);
500
+ return this._api("POST", url, body, headers);
490
501
  }
491
502
  async get(url, headers = {}) {
492
- return this.stdPGET("GET", url, undefined, headers);
503
+ return this._api("GET", url, undefined, headers);
493
504
  }
494
505
  // this.user = data
495
506
  // localStorage.setItem("user", JSON.stringify(data))
@@ -599,15 +610,16 @@ class TauntApi {
599
610
  }
600
611
  constructor(endpoint){
601
612
  this.refreshToken = null;
613
+ this.accessToken = null;
602
614
  this.claimrToken = null;
603
615
  this.onError = async (error)=>{
604
616
  const originalRequest = error.config;
605
- if (error.response.status === 401 && !originalRequest._retry) {
617
+ if (error.response.status === 401 && !originalRequest._retry && this.refreshToken) {
606
618
  originalRequest._retry = true;
607
619
  console.log("401 - refreshing token");
608
620
  try {
609
621
  const data = await this.refresh();
610
- return this.api(originalRequest);
622
+ return this._axios(originalRequest);
611
623
  } catch (error) {
612
624
  console.log("Unable to refresh access token, please log in");
613
625
  this.refreshToken = null;
@@ -626,32 +638,41 @@ class TauntApi {
626
638
  });
627
639
  this.loginWithWeb3WalletSignature = (props)=>this.post("/v1/auth/login/signature", props);
628
640
  this.loginExtWithWeb3WalletSignature = (props)=>this.post("/v1/auth/login/ext-signature", props);
629
- this.refresh = (refreshToken)=>this.post(`/v1/auth/token/refresh`, {
630
- token: refreshToken || this.refreshToken
641
+ this.refresh = (refreshToken)=>{
642
+ const token = refreshToken || this.refreshToken;
643
+ if (!token) {
644
+ return Promise.reject(new Error("No refresh token available"));
645
+ }
646
+ return this.post(`/v1/auth/token/refresh`, {
647
+ token
631
648
  });
649
+ };
632
650
  // Logout of the backend with access token. Assumes user is currently logged in and has a cookie with the access token
633
651
  this.logout = async ()=>{
634
652
  // If logging out fails on the backend we'll still remove the user info
635
- await this.api.post("/v1/auth/logout");
653
+ await this.post("/v1/auth/logout");
636
654
  await this.writePlayerEvent("player_logged_out");
637
655
  };
638
656
  this.getClaimrToken = async ()=>{
639
657
  if (!this.claimrToken) {
640
658
  const url = "v1/claimr/token";
641
- const { data } = await this.api.get(`${url}`);
659
+ const data = await this.get(`${url}`);
642
660
  this.claimrToken = data.data.token;
643
661
  }
644
662
  return this.claimrToken;
645
663
  };
664
+ this.getClaimrData = async ()=>{
665
+ const data = await this.get("/v1/beamable/inventory/skulls");
666
+ return data;
667
+ };
646
668
  // Use the cookie stored in the browser to get the user and save user model in state and local storage
647
669
  // This assumes that the user is logged to backend in and has a cookie jwt
648
- this.getLoggedInUser = ()=>this.api.get("/v1/auth/me");
649
- this.api = axios__default.default.create({
650
- baseURL: `${endpoint}`,
651
- withCredentials: true
670
+ this.getLoggedInUser = ()=>this.get("/v1/auth/me");
671
+ this._axios = axios__default.default.create({
672
+ baseURL: `${endpoint}`
652
673
  });
653
674
  // Response interceptor for API calls
654
- this.api.interceptors.response.use((response)=>response, (error)=>this.onError(error));
675
+ this._axios.interceptors.response.use((response)=>response, (error)=>this.onError(error));
655
676
  }
656
677
  }
657
678
 
@@ -1,4 +1,4 @@
1
- import { AxiosResponse, AxiosRequestConfig, Method } from 'axios';
1
+ import { AxiosResponse, AxiosRequestConfig } from 'axios';
2
2
  import * as magic_sdk from 'magic-sdk';
3
3
  import { OAuthRedirectResult, OAuthRedirectError } from '@magic-ext/oauth2';
4
4
  import { BaseProvider } from '@metamask/providers';
@@ -307,9 +307,15 @@ type TauntUser = {
307
307
  refreshToken: string;
308
308
  accessToken: string;
309
309
  };
310
+ type ClaimrCampaignData = {
311
+ redSkulls: number;
312
+ numActionsComplete: number;
313
+ questComplete: boolean;
314
+ };
310
315
  declare class TauntApi {
311
- private api;
316
+ private _axios;
312
317
  private refreshToken;
318
+ private accessToken;
313
319
  private claimrToken;
314
320
  constructor(endpoint: string);
315
321
  onError: (error: {
@@ -318,7 +324,7 @@ declare class TauntApi {
318
324
  };
319
325
  response: AxiosResponse;
320
326
  }) => Promise<AxiosResponse<any, any, {}>>;
321
- stdPGET<T = TauntRespType>(method: Method, url: string, body?: unknown, headers?: {}): Promise<T>;
327
+ private _api;
322
328
  post<T = TauntRespType>(url: string, body?: {}, headers?: {}): Promise<T>;
323
329
  get<T = TauntRespType>(url: string, headers?: {}): Promise<T>;
324
330
  nonceLogin: (walletAddress: string, clientNonce: string) => Promise<string>;
@@ -327,8 +333,9 @@ declare class TauntApi {
327
333
  loginExtWithWeb3WalletSignature: (props: TauntExtSigProps) => Promise<TauntRespType>;
328
334
  refresh: (refreshToken?: string) => Promise<TauntRespType>;
329
335
  logout: () => Promise<void>;
330
- getClaimrToken: () => Promise<string | null>;
331
- getLoggedInUser: () => Promise<AxiosResponse<TauntUser, any, {}>>;
336
+ getClaimrToken: () => Promise<string>;
337
+ getClaimrData: () => Promise<ClaimrCampaignData>;
338
+ getLoggedInUser: () => Promise<TauntUser>;
332
339
  writePlayerEvent(eventName: string, eventData?: unknown, retryIfLoginNeeded?: boolean): Promise<void>;
333
340
  }
334
341
 
@@ -400,4 +407,4 @@ declare function tauntMetamaskLogin(tauntServiceEndpoint: string, providerParam?
400
407
  }>;
401
408
 
402
409
  export { BadGatewayError, BadRequestError, BandwidthLimitExceededError, ConflictError, ErrorFromResponse, ErrorResponse, ExpectationFailedError, FailedDependencyError, ForbiddenError, GatewayTimeoutError, GoneError, HTTPVersionNotSupportedError, ImATeapotError, InsufficientStorageError, InternalServerError, LengthRequiredError, LockedError, LoopDetectedError, MethodNotAllowedError, NetworkAuthenticationRequiredError, NotAcceptableError, NotExtendedError, NotFoundError, NotImplementedError, PaymentRequiredError, PreconditionFailedError, PreconditionRequiredError, ProxyAuthenticationRequiredError, RequestEntityTooLargeError, RequestHeaderFieldsTooLargeError, RequestTimeoutError, RequestUriTooLongError, RequestedRangeNotSatisfiableError, ServiceUnavailableError, TauntApi, TooManyRequestsError, UnauthorizedError, UnknownError, UnprocessableEntityError, UnsupportedMediaTypeError, UpgradeRequiredError, VariantAlsoNegotiatesError, emailOTPWithMagic, tauntMagicDidLogin, tauntMagicEmailOTPLogin, tauntMagicTelegramLogin, tauntMetamaskLogin, tauntSignWithMetamask, telegramWithMagic };
403
- export type { TauntExtSigProps, TauntSigProps, TauntUser };
410
+ export type { ClaimrCampaignData, TauntExtSigProps, TauntSigProps, TauntUser };
@@ -1,4 +1,4 @@
1
- import { AxiosResponse, AxiosRequestConfig, Method } from 'axios';
1
+ import { AxiosResponse, AxiosRequestConfig } from 'axios';
2
2
  import * as magic_sdk from 'magic-sdk';
3
3
  import { OAuthRedirectResult, OAuthRedirectError } from '@magic-ext/oauth2';
4
4
  import { BaseProvider } from '@metamask/providers';
@@ -307,9 +307,15 @@ type TauntUser = {
307
307
  refreshToken: string;
308
308
  accessToken: string;
309
309
  };
310
+ type ClaimrCampaignData = {
311
+ redSkulls: number;
312
+ numActionsComplete: number;
313
+ questComplete: boolean;
314
+ };
310
315
  declare class TauntApi {
311
- private api;
316
+ private _axios;
312
317
  private refreshToken;
318
+ private accessToken;
313
319
  private claimrToken;
314
320
  constructor(endpoint: string);
315
321
  onError: (error: {
@@ -318,7 +324,7 @@ declare class TauntApi {
318
324
  };
319
325
  response: AxiosResponse;
320
326
  }) => Promise<AxiosResponse<any, any, {}>>;
321
- stdPGET<T = TauntRespType>(method: Method, url: string, body?: unknown, headers?: {}): Promise<T>;
327
+ private _api;
322
328
  post<T = TauntRespType>(url: string, body?: {}, headers?: {}): Promise<T>;
323
329
  get<T = TauntRespType>(url: string, headers?: {}): Promise<T>;
324
330
  nonceLogin: (walletAddress: string, clientNonce: string) => Promise<string>;
@@ -327,8 +333,9 @@ declare class TauntApi {
327
333
  loginExtWithWeb3WalletSignature: (props: TauntExtSigProps) => Promise<TauntRespType>;
328
334
  refresh: (refreshToken?: string) => Promise<TauntRespType>;
329
335
  logout: () => Promise<void>;
330
- getClaimrToken: () => Promise<string | null>;
331
- getLoggedInUser: () => Promise<AxiosResponse<TauntUser, any, {}>>;
336
+ getClaimrToken: () => Promise<string>;
337
+ getClaimrData: () => Promise<ClaimrCampaignData>;
338
+ getLoggedInUser: () => Promise<TauntUser>;
332
339
  writePlayerEvent(eventName: string, eventData?: unknown, retryIfLoginNeeded?: boolean): Promise<void>;
333
340
  }
334
341
 
@@ -400,4 +407,4 @@ declare function tauntMetamaskLogin(tauntServiceEndpoint: string, providerParam?
400
407
  }>;
401
408
 
402
409
  export { BadGatewayError, BadRequestError, BandwidthLimitExceededError, ConflictError, ErrorFromResponse, ErrorResponse, ExpectationFailedError, FailedDependencyError, ForbiddenError, GatewayTimeoutError, GoneError, HTTPVersionNotSupportedError, ImATeapotError, InsufficientStorageError, InternalServerError, LengthRequiredError, LockedError, LoopDetectedError, MethodNotAllowedError, NetworkAuthenticationRequiredError, NotAcceptableError, NotExtendedError, NotFoundError, NotImplementedError, PaymentRequiredError, PreconditionFailedError, PreconditionRequiredError, ProxyAuthenticationRequiredError, RequestEntityTooLargeError, RequestHeaderFieldsTooLargeError, RequestTimeoutError, RequestUriTooLongError, RequestedRangeNotSatisfiableError, ServiceUnavailableError, TauntApi, TooManyRequestsError, UnauthorizedError, UnknownError, UnprocessableEntityError, UnsupportedMediaTypeError, UpgradeRequiredError, VariantAlsoNegotiatesError, emailOTPWithMagic, tauntMagicDidLogin, tauntMagicEmailOTPLogin, tauntMagicTelegramLogin, tauntMetamaskLogin, tauntSignWithMetamask, telegramWithMagic };
403
- export type { TauntExtSigProps, TauntSigProps, TauntUser };
410
+ export type { ClaimrCampaignData, TauntExtSigProps, TauntSigProps, TauntUser };
package/dist/es/index.js CHANGED
@@ -462,28 +462,39 @@ function ErrorFromResponse(response) {
462
462
  }
463
463
 
464
464
  class TauntApi {
465
- async stdPGET(method, url, body, headers = {}) {
466
- const response = await this.api.request({
465
+ async _api(method, url, body, headers = {}) {
466
+ headers = {
467
+ "Content-Type": "application/json",
468
+ Accept: "application/json",
469
+ ...headers
470
+ };
471
+ if (this.accessToken) {
472
+ headers = {
473
+ ...headers,
474
+ Authorization: `Bearer ${this.accessToken}`
475
+ };
476
+ }
477
+ const response = await this._axios.request({
467
478
  url,
468
479
  method,
469
- headers: {
470
- "Content-Type": "application/json",
471
- Accept: "application/json",
472
- ...headers
473
- },
474
- data: body ? JSON.stringify(body) : null
480
+ headers,
481
+ data: body ? JSON.stringify(body) : null,
482
+ withCredentials: true
475
483
  });
476
484
  const data = response.data;
477
- if (data.refreshToken) {
485
+ if (data?.refreshToken) {
478
486
  this.refreshToken = data.refreshToken;
479
487
  }
488
+ if (data?.accessToken) {
489
+ this.accessToken = data.accessToken;
490
+ }
480
491
  return data;
481
492
  }
482
493
  async post(url, body = {}, headers = {}) {
483
- return this.stdPGET("POST", url, body, headers);
494
+ return this._api("POST", url, body, headers);
484
495
  }
485
496
  async get(url, headers = {}) {
486
- return this.stdPGET("GET", url, undefined, headers);
497
+ return this._api("GET", url, undefined, headers);
487
498
  }
488
499
  // this.user = data
489
500
  // localStorage.setItem("user", JSON.stringify(data))
@@ -593,15 +604,16 @@ class TauntApi {
593
604
  }
594
605
  constructor(endpoint){
595
606
  this.refreshToken = null;
607
+ this.accessToken = null;
596
608
  this.claimrToken = null;
597
609
  this.onError = async (error)=>{
598
610
  const originalRequest = error.config;
599
- if (error.response.status === 401 && !originalRequest._retry) {
611
+ if (error.response.status === 401 && !originalRequest._retry && this.refreshToken) {
600
612
  originalRequest._retry = true;
601
613
  console.log("401 - refreshing token");
602
614
  try {
603
615
  const data = await this.refresh();
604
- return this.api(originalRequest);
616
+ return this._axios(originalRequest);
605
617
  } catch (error) {
606
618
  console.log("Unable to refresh access token, please log in");
607
619
  this.refreshToken = null;
@@ -620,32 +632,41 @@ class TauntApi {
620
632
  });
621
633
  this.loginWithWeb3WalletSignature = (props)=>this.post("/v1/auth/login/signature", props);
622
634
  this.loginExtWithWeb3WalletSignature = (props)=>this.post("/v1/auth/login/ext-signature", props);
623
- this.refresh = (refreshToken)=>this.post(`/v1/auth/token/refresh`, {
624
- token: refreshToken || this.refreshToken
635
+ this.refresh = (refreshToken)=>{
636
+ const token = refreshToken || this.refreshToken;
637
+ if (!token) {
638
+ return Promise.reject(new Error("No refresh token available"));
639
+ }
640
+ return this.post(`/v1/auth/token/refresh`, {
641
+ token
625
642
  });
643
+ };
626
644
  // Logout of the backend with access token. Assumes user is currently logged in and has a cookie with the access token
627
645
  this.logout = async ()=>{
628
646
  // If logging out fails on the backend we'll still remove the user info
629
- await this.api.post("/v1/auth/logout");
647
+ await this.post("/v1/auth/logout");
630
648
  await this.writePlayerEvent("player_logged_out");
631
649
  };
632
650
  this.getClaimrToken = async ()=>{
633
651
  if (!this.claimrToken) {
634
652
  const url = "v1/claimr/token";
635
- const { data } = await this.api.get(`${url}`);
653
+ const data = await this.get(`${url}`);
636
654
  this.claimrToken = data.data.token;
637
655
  }
638
656
  return this.claimrToken;
639
657
  };
658
+ this.getClaimrData = async ()=>{
659
+ const data = await this.get("/v1/beamable/inventory/skulls");
660
+ return data;
661
+ };
640
662
  // Use the cookie stored in the browser to get the user and save user model in state and local storage
641
663
  // This assumes that the user is logged to backend in and has a cookie jwt
642
- this.getLoggedInUser = ()=>this.api.get("/v1/auth/me");
643
- this.api = axios.create({
644
- baseURL: `${endpoint}`,
645
- withCredentials: true
664
+ this.getLoggedInUser = ()=>this.get("/v1/auth/me");
665
+ this._axios = axios.create({
666
+ baseURL: `${endpoint}`
646
667
  });
647
668
  // Response interceptor for API calls
648
- this.api.interceptors.response.use((response)=>response, (error)=>this.onError(error));
669
+ this._axios.interceptors.response.use((response)=>response, (error)=>this.onError(error));
649
670
  }
650
671
  }
651
672
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redzone/taunt-logins",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "files": [
@@ -25,7 +25,7 @@
25
25
  "build": "bunchee",
26
26
  "dev": "bunchee --watch",
27
27
  "prepublish": "npm run build",
28
- "publish": "npm publish --access public"
28
+ "npm:publish": "npm publish --access public"
29
29
  },
30
30
  "dependencies": {
31
31
  "@magic-ext/oauth2": "^12.0.0",