@redzone/taunt-logins 0.0.8 → 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,36 +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
  };
646
664
  this.getClaimrData = async ()=>{
647
- const { data } = await this.api.get("/v1/beamable/inventory/skulls");
665
+ const data = await this.get("/v1/beamable/inventory/skulls");
648
666
  return data;
649
667
  };
650
668
  // Use the cookie stored in the browser to get the user and save user model in state and local storage
651
669
  // This assumes that the user is logged to backend in and has a cookie jwt
652
- this.getLoggedInUser = ()=>this.api.get("/v1/auth/me");
653
- this.api = axios__default.default.create({
654
- baseURL: `${endpoint}`,
655
- withCredentials: true
670
+ this.getLoggedInUser = ()=>this.get("/v1/auth/me");
671
+ this._axios = axios__default.default.create({
672
+ baseURL: `${endpoint}`
656
673
  });
657
674
  // Response interceptor for API calls
658
- this.api.interceptors.response.use((response)=>response, (error)=>this.onError(error));
675
+ this._axios.interceptors.response.use((response)=>response, (error)=>this.onError(error));
659
676
  }
660
677
  }
661
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';
@@ -313,8 +313,9 @@ type ClaimrCampaignData = {
313
313
  questComplete: boolean;
314
314
  };
315
315
  declare class TauntApi {
316
- private api;
316
+ private _axios;
317
317
  private refreshToken;
318
+ private accessToken;
318
319
  private claimrToken;
319
320
  constructor(endpoint: string);
320
321
  onError: (error: {
@@ -323,7 +324,7 @@ declare class TauntApi {
323
324
  };
324
325
  response: AxiosResponse;
325
326
  }) => Promise<AxiosResponse<any, any, {}>>;
326
- stdPGET<T = TauntRespType>(method: Method, url: string, body?: unknown, headers?: {}): Promise<T>;
327
+ private _api;
327
328
  post<T = TauntRespType>(url: string, body?: {}, headers?: {}): Promise<T>;
328
329
  get<T = TauntRespType>(url: string, headers?: {}): Promise<T>;
329
330
  nonceLogin: (walletAddress: string, clientNonce: string) => Promise<string>;
@@ -332,9 +333,9 @@ declare class TauntApi {
332
333
  loginExtWithWeb3WalletSignature: (props: TauntExtSigProps) => Promise<TauntRespType>;
333
334
  refresh: (refreshToken?: string) => Promise<TauntRespType>;
334
335
  logout: () => Promise<void>;
335
- getClaimrToken: () => Promise<string | null>;
336
+ getClaimrToken: () => Promise<string>;
336
337
  getClaimrData: () => Promise<ClaimrCampaignData>;
337
- getLoggedInUser: () => Promise<AxiosResponse<TauntUser, any, {}>>;
338
+ getLoggedInUser: () => Promise<TauntUser>;
338
339
  writePlayerEvent(eventName: string, eventData?: unknown, retryIfLoginNeeded?: boolean): Promise<void>;
339
340
  }
340
341
 
@@ -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';
@@ -313,8 +313,9 @@ type ClaimrCampaignData = {
313
313
  questComplete: boolean;
314
314
  };
315
315
  declare class TauntApi {
316
- private api;
316
+ private _axios;
317
317
  private refreshToken;
318
+ private accessToken;
318
319
  private claimrToken;
319
320
  constructor(endpoint: string);
320
321
  onError: (error: {
@@ -323,7 +324,7 @@ declare class TauntApi {
323
324
  };
324
325
  response: AxiosResponse;
325
326
  }) => Promise<AxiosResponse<any, any, {}>>;
326
- stdPGET<T = TauntRespType>(method: Method, url: string, body?: unknown, headers?: {}): Promise<T>;
327
+ private _api;
327
328
  post<T = TauntRespType>(url: string, body?: {}, headers?: {}): Promise<T>;
328
329
  get<T = TauntRespType>(url: string, headers?: {}): Promise<T>;
329
330
  nonceLogin: (walletAddress: string, clientNonce: string) => Promise<string>;
@@ -332,9 +333,9 @@ declare class TauntApi {
332
333
  loginExtWithWeb3WalletSignature: (props: TauntExtSigProps) => Promise<TauntRespType>;
333
334
  refresh: (refreshToken?: string) => Promise<TauntRespType>;
334
335
  logout: () => Promise<void>;
335
- getClaimrToken: () => Promise<string | null>;
336
+ getClaimrToken: () => Promise<string>;
336
337
  getClaimrData: () => Promise<ClaimrCampaignData>;
337
- getLoggedInUser: () => Promise<AxiosResponse<TauntUser, any, {}>>;
338
+ getLoggedInUser: () => Promise<TauntUser>;
338
339
  writePlayerEvent(eventName: string, eventData?: unknown, retryIfLoginNeeded?: boolean): Promise<void>;
339
340
  }
340
341
 
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,36 +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
  };
640
658
  this.getClaimrData = async ()=>{
641
- const { data } = await this.api.get("/v1/beamable/inventory/skulls");
659
+ const data = await this.get("/v1/beamable/inventory/skulls");
642
660
  return data;
643
661
  };
644
662
  // Use the cookie stored in the browser to get the user and save user model in state and local storage
645
663
  // This assumes that the user is logged to backend in and has a cookie jwt
646
- this.getLoggedInUser = ()=>this.api.get("/v1/auth/me");
647
- this.api = axios.create({
648
- baseURL: `${endpoint}`,
649
- withCredentials: true
664
+ this.getLoggedInUser = ()=>this.get("/v1/auth/me");
665
+ this._axios = axios.create({
666
+ baseURL: `${endpoint}`
650
667
  });
651
668
  // Response interceptor for API calls
652
- this.api.interceptors.response.use((response)=>response, (error)=>this.onError(error));
669
+ this._axios.interceptors.response.use((response)=>response, (error)=>this.onError(error));
653
670
  }
654
671
  }
655
672
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redzone/taunt-logins",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "files": [