@redzone/taunt-logins 0.0.14 → 0.0.16
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/dist/cjs/index.cjs +19 -19
- package/dist/cjs/index.d.cts +3 -3
- package/dist/es/index.d.ts +3 -3
- package/dist/es/index.js +19 -19
- package/package.json +1 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -347,19 +347,24 @@ function ErrorFromResponse(response) {
|
|
|
347
347
|
}
|
|
348
348
|
}
|
|
349
349
|
|
|
350
|
+
const REFRESH_KEY = "taunt_refresh_token";
|
|
350
351
|
class TauntApi {
|
|
352
|
+
get refreshToken() {
|
|
353
|
+
return localStorage.getItem(REFRESH_KEY);
|
|
354
|
+
}
|
|
355
|
+
set refreshToken(token) {
|
|
356
|
+
if (token) {
|
|
357
|
+
localStorage.setItem(REFRESH_KEY, token);
|
|
358
|
+
} else {
|
|
359
|
+
localStorage.removeItem(REFRESH_KEY);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
351
362
|
async _api(method, url, body, headers = {}) {
|
|
352
363
|
headers = {
|
|
353
364
|
"Content-Type": "application/json",
|
|
354
365
|
Accept: "application/json",
|
|
355
366
|
...headers
|
|
356
367
|
};
|
|
357
|
-
if (this.accessToken) {
|
|
358
|
-
headers = {
|
|
359
|
-
...headers,
|
|
360
|
-
Authorization: `Bearer ${this.accessToken}`
|
|
361
|
-
};
|
|
362
|
-
}
|
|
363
368
|
const response = await this._axios.request({
|
|
364
369
|
url,
|
|
365
370
|
method,
|
|
@@ -371,9 +376,6 @@ class TauntApi {
|
|
|
371
376
|
if (data?.refreshToken) {
|
|
372
377
|
this.refreshToken = data.refreshToken;
|
|
373
378
|
}
|
|
374
|
-
if (data?.accessToken) {
|
|
375
|
-
this.accessToken = data.accessToken;
|
|
376
|
-
}
|
|
377
379
|
return data;
|
|
378
380
|
}
|
|
379
381
|
async post(url, body = {}, headers = {}) {
|
|
@@ -387,8 +389,7 @@ class TauntApi {
|
|
|
387
389
|
if (!walletAddress) {
|
|
388
390
|
throw new Error("No wallet address found");
|
|
389
391
|
}
|
|
390
|
-
const
|
|
391
|
-
const clientNonce = crypto.getRandomValues(cryptoValuesArray).toString();
|
|
392
|
+
const clientNonce = this.randomTokenString();
|
|
392
393
|
const serverNonce = await this.nonceLogin(walletAddress, clientNonce);
|
|
393
394
|
const payload = JSON.stringify({
|
|
394
395
|
clientNonce,
|
|
@@ -399,7 +400,6 @@ class TauntApi {
|
|
|
399
400
|
try {
|
|
400
401
|
signature = await provider.personalSign(message, walletAddress);
|
|
401
402
|
} catch (err) {
|
|
402
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
403
403
|
throw new Error(`User denied message signature, ${err}`);
|
|
404
404
|
}
|
|
405
405
|
if (!signature) {
|
|
@@ -519,8 +519,6 @@ class TauntApi {
|
|
|
519
519
|
return Promise.resolve();
|
|
520
520
|
}
|
|
521
521
|
constructor(endpoint){
|
|
522
|
-
this.refreshToken = null;
|
|
523
|
-
this.accessToken = null;
|
|
524
522
|
this.claimrToken = null;
|
|
525
523
|
this.onError = async (error)=>{
|
|
526
524
|
const originalRequest = error.config;
|
|
@@ -540,18 +538,16 @@ class TauntApi {
|
|
|
540
538
|
return Promise.reject(ErrorFromResponse(error.response));
|
|
541
539
|
};
|
|
542
540
|
this.setLoginDetails = async (props, checkGet = true)=>{
|
|
543
|
-
this.accessToken = props.accessToken;
|
|
544
541
|
this.refreshToken = props.refreshToken;
|
|
545
542
|
if (checkGet) {
|
|
546
543
|
const user = await this.getLoggedInUser();
|
|
547
544
|
if (!user) {
|
|
548
|
-
this.accessToken = null;
|
|
549
545
|
this.refreshToken = null;
|
|
550
546
|
return null;
|
|
551
547
|
}
|
|
552
548
|
}
|
|
553
549
|
return {
|
|
554
|
-
accessToken:
|
|
550
|
+
accessToken: props.accessToken,
|
|
555
551
|
refreshToken: this.refreshToken
|
|
556
552
|
};
|
|
557
553
|
};
|
|
@@ -593,12 +589,16 @@ class TauntApi {
|
|
|
593
589
|
const data = await this.get("v1/claimr/referral-code");
|
|
594
590
|
return data.data?.ref_id;
|
|
595
591
|
};
|
|
596
|
-
this.randomTokenString = ()=>
|
|
592
|
+
this.randomTokenString = (len = 3)=>{
|
|
593
|
+
const cryptoValuesArray = new Uint32Array(len);
|
|
594
|
+
return crypto.getRandomValues(cryptoValuesArray).toString();
|
|
595
|
+
};
|
|
597
596
|
// Use the cookie stored in the browser to get the user and save user model in state and local storage
|
|
598
597
|
// This assumes that the user is logged to backend in and has a cookie jwt
|
|
599
598
|
this.getLoggedInUser = ()=>this.get("/v1/auth/me");
|
|
600
599
|
this._axios = axios__default.default.create({
|
|
601
|
-
baseURL: endpoint
|
|
600
|
+
baseURL: endpoint,
|
|
601
|
+
withCredentials: true
|
|
602
602
|
});
|
|
603
603
|
// Response interceptor for API calls
|
|
604
604
|
this._axios.interceptors.response.use((response)=>response, (error)=>this.onError(error));
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -250,8 +250,8 @@ interface AxiosErrorResponse {
|
|
|
250
250
|
}
|
|
251
251
|
declare class TauntApi {
|
|
252
252
|
private _axios;
|
|
253
|
-
|
|
254
|
-
|
|
253
|
+
get refreshToken(): string | null;
|
|
254
|
+
set refreshToken(token: string | null);
|
|
255
255
|
private claimrToken;
|
|
256
256
|
constructor(endpoint: string);
|
|
257
257
|
onError: (error: AxiosErrorResponse) => Promise<AxiosResponse<any, any, {}>>;
|
|
@@ -270,7 +270,7 @@ declare class TauntApi {
|
|
|
270
270
|
getClaimrToken: () => Promise<string>;
|
|
271
271
|
getClaimrSkulls: () => Promise<ClaimrCampaignData>;
|
|
272
272
|
getClaimrReferralCode: () => Promise<string | undefined>;
|
|
273
|
-
randomTokenString: () => string;
|
|
273
|
+
randomTokenString: (len?: number) => string;
|
|
274
274
|
withProvider(provider: {
|
|
275
275
|
getAddress: () => Promise<string | undefined | null>;
|
|
276
276
|
personalSign: (message: string, address: string) => Promise<string | undefined | null>;
|
package/dist/es/index.d.ts
CHANGED
|
@@ -250,8 +250,8 @@ interface AxiosErrorResponse {
|
|
|
250
250
|
}
|
|
251
251
|
declare class TauntApi {
|
|
252
252
|
private _axios;
|
|
253
|
-
|
|
254
|
-
|
|
253
|
+
get refreshToken(): string | null;
|
|
254
|
+
set refreshToken(token: string | null);
|
|
255
255
|
private claimrToken;
|
|
256
256
|
constructor(endpoint: string);
|
|
257
257
|
onError: (error: AxiosErrorResponse) => Promise<AxiosResponse<any, any, {}>>;
|
|
@@ -270,7 +270,7 @@ declare class TauntApi {
|
|
|
270
270
|
getClaimrToken: () => Promise<string>;
|
|
271
271
|
getClaimrSkulls: () => Promise<ClaimrCampaignData>;
|
|
272
272
|
getClaimrReferralCode: () => Promise<string | undefined>;
|
|
273
|
-
randomTokenString: () => string;
|
|
273
|
+
randomTokenString: (len?: number) => string;
|
|
274
274
|
withProvider(provider: {
|
|
275
275
|
getAddress: () => Promise<string | undefined | null>;
|
|
276
276
|
personalSign: (message: string, address: string) => Promise<string | undefined | null>;
|
package/dist/es/index.js
CHANGED
|
@@ -341,19 +341,24 @@ function ErrorFromResponse(response) {
|
|
|
341
341
|
}
|
|
342
342
|
}
|
|
343
343
|
|
|
344
|
+
const REFRESH_KEY = "taunt_refresh_token";
|
|
344
345
|
class TauntApi {
|
|
346
|
+
get refreshToken() {
|
|
347
|
+
return localStorage.getItem(REFRESH_KEY);
|
|
348
|
+
}
|
|
349
|
+
set refreshToken(token) {
|
|
350
|
+
if (token) {
|
|
351
|
+
localStorage.setItem(REFRESH_KEY, token);
|
|
352
|
+
} else {
|
|
353
|
+
localStorage.removeItem(REFRESH_KEY);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
345
356
|
async _api(method, url, body, headers = {}) {
|
|
346
357
|
headers = {
|
|
347
358
|
"Content-Type": "application/json",
|
|
348
359
|
Accept: "application/json",
|
|
349
360
|
...headers
|
|
350
361
|
};
|
|
351
|
-
if (this.accessToken) {
|
|
352
|
-
headers = {
|
|
353
|
-
...headers,
|
|
354
|
-
Authorization: `Bearer ${this.accessToken}`
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
362
|
const response = await this._axios.request({
|
|
358
363
|
url,
|
|
359
364
|
method,
|
|
@@ -365,9 +370,6 @@ class TauntApi {
|
|
|
365
370
|
if (data?.refreshToken) {
|
|
366
371
|
this.refreshToken = data.refreshToken;
|
|
367
372
|
}
|
|
368
|
-
if (data?.accessToken) {
|
|
369
|
-
this.accessToken = data.accessToken;
|
|
370
|
-
}
|
|
371
373
|
return data;
|
|
372
374
|
}
|
|
373
375
|
async post(url, body = {}, headers = {}) {
|
|
@@ -381,8 +383,7 @@ class TauntApi {
|
|
|
381
383
|
if (!walletAddress) {
|
|
382
384
|
throw new Error("No wallet address found");
|
|
383
385
|
}
|
|
384
|
-
const
|
|
385
|
-
const clientNonce = crypto.getRandomValues(cryptoValuesArray).toString();
|
|
386
|
+
const clientNonce = this.randomTokenString();
|
|
386
387
|
const serverNonce = await this.nonceLogin(walletAddress, clientNonce);
|
|
387
388
|
const payload = JSON.stringify({
|
|
388
389
|
clientNonce,
|
|
@@ -393,7 +394,6 @@ class TauntApi {
|
|
|
393
394
|
try {
|
|
394
395
|
signature = await provider.personalSign(message, walletAddress);
|
|
395
396
|
} catch (err) {
|
|
396
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
397
397
|
throw new Error(`User denied message signature, ${err}`);
|
|
398
398
|
}
|
|
399
399
|
if (!signature) {
|
|
@@ -513,8 +513,6 @@ class TauntApi {
|
|
|
513
513
|
return Promise.resolve();
|
|
514
514
|
}
|
|
515
515
|
constructor(endpoint){
|
|
516
|
-
this.refreshToken = null;
|
|
517
|
-
this.accessToken = null;
|
|
518
516
|
this.claimrToken = null;
|
|
519
517
|
this.onError = async (error)=>{
|
|
520
518
|
const originalRequest = error.config;
|
|
@@ -534,18 +532,16 @@ class TauntApi {
|
|
|
534
532
|
return Promise.reject(ErrorFromResponse(error.response));
|
|
535
533
|
};
|
|
536
534
|
this.setLoginDetails = async (props, checkGet = true)=>{
|
|
537
|
-
this.accessToken = props.accessToken;
|
|
538
535
|
this.refreshToken = props.refreshToken;
|
|
539
536
|
if (checkGet) {
|
|
540
537
|
const user = await this.getLoggedInUser();
|
|
541
538
|
if (!user) {
|
|
542
|
-
this.accessToken = null;
|
|
543
539
|
this.refreshToken = null;
|
|
544
540
|
return null;
|
|
545
541
|
}
|
|
546
542
|
}
|
|
547
543
|
return {
|
|
548
|
-
accessToken:
|
|
544
|
+
accessToken: props.accessToken,
|
|
549
545
|
refreshToken: this.refreshToken
|
|
550
546
|
};
|
|
551
547
|
};
|
|
@@ -587,12 +583,16 @@ class TauntApi {
|
|
|
587
583
|
const data = await this.get("v1/claimr/referral-code");
|
|
588
584
|
return data.data?.ref_id;
|
|
589
585
|
};
|
|
590
|
-
this.randomTokenString = ()=>
|
|
586
|
+
this.randomTokenString = (len = 3)=>{
|
|
587
|
+
const cryptoValuesArray = new Uint32Array(len);
|
|
588
|
+
return crypto.getRandomValues(cryptoValuesArray).toString();
|
|
589
|
+
};
|
|
591
590
|
// Use the cookie stored in the browser to get the user and save user model in state and local storage
|
|
592
591
|
// This assumes that the user is logged to backend in and has a cookie jwt
|
|
593
592
|
this.getLoggedInUser = ()=>this.get("/v1/auth/me");
|
|
594
593
|
this._axios = axios.create({
|
|
595
|
-
baseURL: endpoint
|
|
594
|
+
baseURL: endpoint,
|
|
595
|
+
withCredentials: true
|
|
596
596
|
});
|
|
597
597
|
// Response interceptor for API calls
|
|
598
598
|
this._axios.interceptors.response.use((response)=>response, (error)=>this.onError(error));
|