@redzone/taunt-logins 0.0.16 → 0.0.18
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 +26 -3
- package/dist/cjs/index.d.cts +4 -1
- package/dist/es/index.d.ts +4 -1
- package/dist/es/index.js +26 -3
- package/package.json +1 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -347,7 +347,9 @@ function ErrorFromResponse(response) {
|
|
|
347
347
|
}
|
|
348
348
|
}
|
|
349
349
|
|
|
350
|
+
const REFRESH_PATH = `/v1/auth/token/refresh`;
|
|
350
351
|
const REFRESH_KEY = "taunt_refresh_token";
|
|
352
|
+
const ACCESS_TOKEN_KEY = "taunt_access_token";
|
|
351
353
|
class TauntApi {
|
|
352
354
|
get refreshToken() {
|
|
353
355
|
return localStorage.getItem(REFRESH_KEY);
|
|
@@ -359,7 +361,20 @@ class TauntApi {
|
|
|
359
361
|
localStorage.removeItem(REFRESH_KEY);
|
|
360
362
|
}
|
|
361
363
|
}
|
|
364
|
+
get accessToken() {
|
|
365
|
+
return localStorage.getItem(ACCESS_TOKEN_KEY);
|
|
366
|
+
}
|
|
367
|
+
set accessToken(token) {
|
|
368
|
+
if (token) {
|
|
369
|
+
localStorage.setItem(ACCESS_TOKEN_KEY, token);
|
|
370
|
+
} else {
|
|
371
|
+
localStorage.removeItem(ACCESS_TOKEN_KEY);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
362
374
|
async _api(method, url, body, headers = {}) {
|
|
375
|
+
if (url !== REFRESH_PATH && !this.accessToken && this.refreshToken) {
|
|
376
|
+
await this.refresh();
|
|
377
|
+
}
|
|
363
378
|
headers = {
|
|
364
379
|
"Content-Type": "application/json",
|
|
365
380
|
Accept: "application/json",
|
|
@@ -376,6 +391,13 @@ class TauntApi {
|
|
|
376
391
|
if (data?.refreshToken) {
|
|
377
392
|
this.refreshToken = data.refreshToken;
|
|
378
393
|
}
|
|
394
|
+
if (data?.accessToken) {
|
|
395
|
+
this.accessToken = data.accessToken;
|
|
396
|
+
this.setAccessDetails?.({
|
|
397
|
+
accessToken: data.accessToken,
|
|
398
|
+
refreshToken: data.refreshToken ?? "_ignore_"
|
|
399
|
+
});
|
|
400
|
+
}
|
|
379
401
|
return data;
|
|
380
402
|
}
|
|
381
403
|
async post(url, body = {}, headers = {}) {
|
|
@@ -518,7 +540,8 @@ class TauntApi {
|
|
|
518
540
|
});
|
|
519
541
|
return Promise.resolve();
|
|
520
542
|
}
|
|
521
|
-
constructor(endpoint){
|
|
543
|
+
constructor(endpoint, setAccessDetails){
|
|
544
|
+
this.setAccessDetails = setAccessDetails;
|
|
522
545
|
this.claimrToken = null;
|
|
523
546
|
this.onError = async (error)=>{
|
|
524
547
|
const originalRequest = error.config;
|
|
@@ -531,6 +554,7 @@ class TauntApi {
|
|
|
531
554
|
} catch (error) {
|
|
532
555
|
console.log("Unable to refresh access token, please log in");
|
|
533
556
|
this.refreshToken = null;
|
|
557
|
+
this.accessToken = null;
|
|
534
558
|
// window.location.href = "/"
|
|
535
559
|
return Promise.reject(error);
|
|
536
560
|
}
|
|
@@ -566,7 +590,7 @@ class TauntApi {
|
|
|
566
590
|
if (!token) {
|
|
567
591
|
return Promise.reject(new Error("No refresh token available"));
|
|
568
592
|
}
|
|
569
|
-
return this.post(
|
|
593
|
+
return this.post(REFRESH_PATH, {
|
|
570
594
|
token
|
|
571
595
|
});
|
|
572
596
|
};
|
|
@@ -600,7 +624,6 @@ class TauntApi {
|
|
|
600
624
|
baseURL: endpoint,
|
|
601
625
|
withCredentials: true
|
|
602
626
|
});
|
|
603
|
-
// Response interceptor for API calls
|
|
604
627
|
this._axios.interceptors.response.use((response)=>response, (error)=>this.onError(error));
|
|
605
628
|
}
|
|
606
629
|
}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -249,11 +249,14 @@ interface AxiosErrorResponse {
|
|
|
249
249
|
}>;
|
|
250
250
|
}
|
|
251
251
|
declare class TauntApi {
|
|
252
|
+
private setAccessDetails?;
|
|
252
253
|
private _axios;
|
|
254
|
+
constructor(endpoint: string, setAccessDetails?: ((props: TauntAccessDetails) => void) | undefined);
|
|
253
255
|
get refreshToken(): string | null;
|
|
254
256
|
set refreshToken(token: string | null);
|
|
257
|
+
get accessToken(): string | null;
|
|
258
|
+
set accessToken(token: string | null);
|
|
255
259
|
private claimrToken;
|
|
256
|
-
constructor(endpoint: string);
|
|
257
260
|
onError: (error: AxiosErrorResponse) => Promise<AxiosResponse<any, any, {}>>;
|
|
258
261
|
private _api;
|
|
259
262
|
setLoginDetails: (props: TauntAccessDetails, checkGet?: boolean) => Promise<{
|
package/dist/es/index.d.ts
CHANGED
|
@@ -249,11 +249,14 @@ interface AxiosErrorResponse {
|
|
|
249
249
|
}>;
|
|
250
250
|
}
|
|
251
251
|
declare class TauntApi {
|
|
252
|
+
private setAccessDetails?;
|
|
252
253
|
private _axios;
|
|
254
|
+
constructor(endpoint: string, setAccessDetails?: ((props: TauntAccessDetails) => void) | undefined);
|
|
253
255
|
get refreshToken(): string | null;
|
|
254
256
|
set refreshToken(token: string | null);
|
|
257
|
+
get accessToken(): string | null;
|
|
258
|
+
set accessToken(token: string | null);
|
|
255
259
|
private claimrToken;
|
|
256
|
-
constructor(endpoint: string);
|
|
257
260
|
onError: (error: AxiosErrorResponse) => Promise<AxiosResponse<any, any, {}>>;
|
|
258
261
|
private _api;
|
|
259
262
|
setLoginDetails: (props: TauntAccessDetails, checkGet?: boolean) => Promise<{
|
package/dist/es/index.js
CHANGED
|
@@ -341,7 +341,9 @@ function ErrorFromResponse(response) {
|
|
|
341
341
|
}
|
|
342
342
|
}
|
|
343
343
|
|
|
344
|
+
const REFRESH_PATH = `/v1/auth/token/refresh`;
|
|
344
345
|
const REFRESH_KEY = "taunt_refresh_token";
|
|
346
|
+
const ACCESS_TOKEN_KEY = "taunt_access_token";
|
|
345
347
|
class TauntApi {
|
|
346
348
|
get refreshToken() {
|
|
347
349
|
return localStorage.getItem(REFRESH_KEY);
|
|
@@ -353,7 +355,20 @@ class TauntApi {
|
|
|
353
355
|
localStorage.removeItem(REFRESH_KEY);
|
|
354
356
|
}
|
|
355
357
|
}
|
|
358
|
+
get accessToken() {
|
|
359
|
+
return localStorage.getItem(ACCESS_TOKEN_KEY);
|
|
360
|
+
}
|
|
361
|
+
set accessToken(token) {
|
|
362
|
+
if (token) {
|
|
363
|
+
localStorage.setItem(ACCESS_TOKEN_KEY, token);
|
|
364
|
+
} else {
|
|
365
|
+
localStorage.removeItem(ACCESS_TOKEN_KEY);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
356
368
|
async _api(method, url, body, headers = {}) {
|
|
369
|
+
if (url !== REFRESH_PATH && !this.accessToken && this.refreshToken) {
|
|
370
|
+
await this.refresh();
|
|
371
|
+
}
|
|
357
372
|
headers = {
|
|
358
373
|
"Content-Type": "application/json",
|
|
359
374
|
Accept: "application/json",
|
|
@@ -370,6 +385,13 @@ class TauntApi {
|
|
|
370
385
|
if (data?.refreshToken) {
|
|
371
386
|
this.refreshToken = data.refreshToken;
|
|
372
387
|
}
|
|
388
|
+
if (data?.accessToken) {
|
|
389
|
+
this.accessToken = data.accessToken;
|
|
390
|
+
this.setAccessDetails?.({
|
|
391
|
+
accessToken: data.accessToken,
|
|
392
|
+
refreshToken: data.refreshToken ?? "_ignore_"
|
|
393
|
+
});
|
|
394
|
+
}
|
|
373
395
|
return data;
|
|
374
396
|
}
|
|
375
397
|
async post(url, body = {}, headers = {}) {
|
|
@@ -512,7 +534,8 @@ class TauntApi {
|
|
|
512
534
|
});
|
|
513
535
|
return Promise.resolve();
|
|
514
536
|
}
|
|
515
|
-
constructor(endpoint){
|
|
537
|
+
constructor(endpoint, setAccessDetails){
|
|
538
|
+
this.setAccessDetails = setAccessDetails;
|
|
516
539
|
this.claimrToken = null;
|
|
517
540
|
this.onError = async (error)=>{
|
|
518
541
|
const originalRequest = error.config;
|
|
@@ -525,6 +548,7 @@ class TauntApi {
|
|
|
525
548
|
} catch (error) {
|
|
526
549
|
console.log("Unable to refresh access token, please log in");
|
|
527
550
|
this.refreshToken = null;
|
|
551
|
+
this.accessToken = null;
|
|
528
552
|
// window.location.href = "/"
|
|
529
553
|
return Promise.reject(error);
|
|
530
554
|
}
|
|
@@ -560,7 +584,7 @@ class TauntApi {
|
|
|
560
584
|
if (!token) {
|
|
561
585
|
return Promise.reject(new Error("No refresh token available"));
|
|
562
586
|
}
|
|
563
|
-
return this.post(
|
|
587
|
+
return this.post(REFRESH_PATH, {
|
|
564
588
|
token
|
|
565
589
|
});
|
|
566
590
|
};
|
|
@@ -594,7 +618,6 @@ class TauntApi {
|
|
|
594
618
|
baseURL: endpoint,
|
|
595
619
|
withCredentials: true
|
|
596
620
|
});
|
|
597
|
-
// Response interceptor for API calls
|
|
598
621
|
this._axios.interceptors.response.use((response)=>response, (error)=>this.onError(error));
|
|
599
622
|
}
|
|
600
623
|
}
|