@ethlete/query 0.1.0-next.17 → 0.1.0-next.19
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/esm2020/lib/auth/auth-provider.types.mjs +1 -1
- package/esm2020/lib/auth/bearer-auth-provider.mjs +63 -22
- package/esm2020/lib/directives/infinity-query.directive.mjs +2 -2
- package/fesm2015/ethlete-query.mjs +71 -28
- package/fesm2015/ethlete-query.mjs.map +1 -1
- package/fesm2020/ethlete-query.mjs +64 -23
- package/fesm2020/ethlete-query.mjs.map +1 -1
- package/lib/auth/auth-provider.types.d.ts +13 -4
- package/lib/auth/bearer-auth-provider.d.ts +6 -3
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getCookie,
|
|
1
|
+
import { getCookie, setCookie, deleteCookie, DestroyService } from '@ethlete/core';
|
|
2
2
|
import { filter, takeWhile, switchMap, of, BehaviorSubject, Subject, interval, takeUntil, tap, timer, fromEvent, startWith, concat, take, debounceTime, combineLatest, map, distinctUntilChanged } from 'rxjs';
|
|
3
3
|
import * as i0 from '@angular/core';
|
|
4
4
|
import { Directive, Input, InjectionToken, inject, ChangeDetectorRef, ViewContainerRef, TemplateRef, ErrorHandler, ElementRef } from '@angular/core';
|
|
@@ -599,7 +599,13 @@ const decryptBearer = (token) => {
|
|
|
599
599
|
|
|
600
600
|
class BearerAuthProvider {
|
|
601
601
|
get header() {
|
|
602
|
-
return { Authorization: `Bearer ${this.
|
|
602
|
+
return { Authorization: `Bearer ${this._tokens$.getValue().token}` };
|
|
603
|
+
}
|
|
604
|
+
get tokens$() {
|
|
605
|
+
return this._tokens$.asObservable();
|
|
606
|
+
}
|
|
607
|
+
get tokens() {
|
|
608
|
+
return this._tokens$.getValue();
|
|
603
609
|
}
|
|
604
610
|
get currentRefreshQuery$() {
|
|
605
611
|
return this._currentRefreshQuery$.asObservable();
|
|
@@ -611,18 +617,31 @@ class BearerAuthProvider {
|
|
|
611
617
|
this._config = _config;
|
|
612
618
|
this._destroy$ = new Subject();
|
|
613
619
|
this._currentRefreshQuery$ = new BehaviorSubject(null);
|
|
614
|
-
this.
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
+
this._tokens$ = new BehaviorSubject({
|
|
621
|
+
token: null,
|
|
622
|
+
refreshToken: null,
|
|
623
|
+
});
|
|
624
|
+
const cookieEnabled = _config.refreshConfig?.cookieEnabled ?? true;
|
|
625
|
+
const cookieToken = _config.refreshConfig?.cookieName && cookieEnabled ? getCookie(_config.refreshConfig.cookieName) ?? null : null;
|
|
626
|
+
this._tokens$.next({
|
|
627
|
+
token: _config.token || null,
|
|
628
|
+
refreshToken: _config.refreshConfig?.token || cookieToken || null,
|
|
629
|
+
});
|
|
630
|
+
if (!this.tokens.token && !this.tokens.refreshToken) {
|
|
620
631
|
if (!_config.refreshConfig?.cookieName) {
|
|
621
632
|
console.error('A BearerAuthProvider was created without token or refresh token. You should provide at least a cookieName where the refresh token might be stored.');
|
|
622
633
|
}
|
|
623
634
|
return;
|
|
624
635
|
}
|
|
625
|
-
this.
|
|
636
|
+
if (this.tokens.token) {
|
|
637
|
+
this._prepareForRefresh();
|
|
638
|
+
if (this.tokens.refreshToken && this._config.refreshConfig?.cookieName) {
|
|
639
|
+
setCookie(this._config.refreshConfig.cookieName, this.tokens.refreshToken);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
else if (this.tokens.refreshToken) {
|
|
643
|
+
this._refreshQuery();
|
|
644
|
+
}
|
|
626
645
|
}
|
|
627
646
|
cleanUp() {
|
|
628
647
|
this._destroy$.next(true);
|
|
@@ -632,11 +651,28 @@ class BearerAuthProvider {
|
|
|
632
651
|
deleteCookie(this._config.refreshConfig.cookieName, '/');
|
|
633
652
|
}
|
|
634
653
|
}
|
|
654
|
+
enableCookie() {
|
|
655
|
+
if (!this._config.refreshConfig?.cookieName) {
|
|
656
|
+
throw new Error('No cookie name was provided');
|
|
657
|
+
}
|
|
658
|
+
if (!this.tokens.refreshToken) {
|
|
659
|
+
throw new Error('No refresh token was provided');
|
|
660
|
+
}
|
|
661
|
+
this._config.refreshConfig.cookieEnabled = true;
|
|
662
|
+
setCookie(this._config.refreshConfig.cookieName, this.tokens.refreshToken);
|
|
663
|
+
}
|
|
664
|
+
disableCookie() {
|
|
665
|
+
if (!this._config.refreshConfig?.cookieName) {
|
|
666
|
+
throw new Error('No cookie name was provided');
|
|
667
|
+
}
|
|
668
|
+
this._config.refreshConfig.cookieEnabled = false;
|
|
669
|
+
deleteCookie(this._config.refreshConfig.cookieName, '/');
|
|
670
|
+
}
|
|
635
671
|
_prepareForRefresh() {
|
|
636
|
-
if (!this.
|
|
672
|
+
if (!this.tokens.token || !this._config.refreshConfig) {
|
|
637
673
|
return;
|
|
638
674
|
}
|
|
639
|
-
const bearer = decryptBearer(this.
|
|
675
|
+
const bearer = decryptBearer(this.tokens.token);
|
|
640
676
|
if (!bearer) {
|
|
641
677
|
return;
|
|
642
678
|
}
|
|
@@ -658,11 +694,15 @@ class BearerAuthProvider {
|
|
|
658
694
|
}
|
|
659
695
|
}
|
|
660
696
|
_refreshQuery() {
|
|
661
|
-
|
|
697
|
+
const currentRefreshToken = this.tokens.refreshToken;
|
|
698
|
+
if (!currentRefreshToken || !this._config.refreshConfig) {
|
|
662
699
|
return;
|
|
663
700
|
}
|
|
664
|
-
const args = this._config.refreshConfig.requestArgsAdapter?.(
|
|
665
|
-
|
|
701
|
+
const args = this._config.refreshConfig.requestArgsAdapter?.({
|
|
702
|
+
token: this.tokens.token,
|
|
703
|
+
refreshToken: currentRefreshToken,
|
|
704
|
+
}) ?? {
|
|
705
|
+
body: { refreshToken: currentRefreshToken },
|
|
666
706
|
};
|
|
667
707
|
const query = this._config.refreshConfig.queryCreator.prepare(args).execute({ skipCache: true });
|
|
668
708
|
this._currentRefreshQuery$.next(query);
|
|
@@ -671,21 +711,22 @@ class BearerAuthProvider {
|
|
|
671
711
|
if (isQueryStateSuccess(state)) {
|
|
672
712
|
if (this._config.refreshConfig?.responseAdapter) {
|
|
673
713
|
const tokens = this._config.refreshConfig.responseAdapter(state.response);
|
|
674
|
-
this.
|
|
675
|
-
this._refreshToken = tokens.refreshToken || null;
|
|
714
|
+
this._tokens$.next(tokens);
|
|
676
715
|
}
|
|
677
716
|
else {
|
|
678
|
-
this.
|
|
679
|
-
|
|
717
|
+
this._tokens$.next({
|
|
718
|
+
token: state.response['token'],
|
|
719
|
+
refreshToken: state.response['refreshToken'],
|
|
720
|
+
});
|
|
680
721
|
}
|
|
681
|
-
|
|
682
|
-
|
|
722
|
+
const cookieEnabled = this._config.refreshConfig?.cookieEnabled ?? true;
|
|
723
|
+
if (this._config.refreshConfig?.cookieName && this.tokens.refreshToken && cookieEnabled) {
|
|
724
|
+
setCookie(this._config.refreshConfig.cookieName, this.tokens.refreshToken);
|
|
683
725
|
}
|
|
684
726
|
this._prepareForRefresh();
|
|
685
727
|
}
|
|
686
728
|
else if (isQueryStateFailure(state)) {
|
|
687
|
-
this.
|
|
688
|
-
this._refreshToken = null;
|
|
729
|
+
this._tokens$.next({ token: null, refreshToken: null });
|
|
689
730
|
if (this._config.refreshConfig?.cookieName) {
|
|
690
731
|
deleteCookie(this._config.refreshConfig.cookieName, '/');
|
|
691
732
|
}
|
|
@@ -973,7 +1014,7 @@ class InfinityQueryDirective {
|
|
|
973
1014
|
itemsPerPage: null,
|
|
974
1015
|
};
|
|
975
1016
|
this._infinityQueryInstance = null;
|
|
976
|
-
this._destroy$ = inject(DestroyService).destroy$;
|
|
1017
|
+
this._destroy$ = inject(DestroyService, { self: true }).destroy$;
|
|
977
1018
|
this._cdr = inject(ChangeDetectorRef);
|
|
978
1019
|
this._viewContainerRef = inject(ViewContainerRef);
|
|
979
1020
|
this._mainTemplateRef = inject((TemplateRef));
|