@ethlete/query 0.1.0-next.17 → 0.1.0-next.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/esm2020/lib/auth/auth-provider.types.mjs +1 -1
- package/esm2020/lib/auth/bearer-auth-provider.mjs +39 -21
- package/fesm2015/ethlete-query.mjs +41 -23
- package/fesm2015/ethlete-query.mjs.map +1 -1
- package/fesm2020/ethlete-query.mjs +39 -21
- package/fesm2020/ethlete-query.mjs.map +1 -1
- package/lib/auth/auth-provider.types.d.ts +4 -4
- package/lib/auth/bearer-auth-provider.d.ts +4 -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,30 @@ class BearerAuthProvider {
|
|
|
611
617
|
this._config = _config;
|
|
612
618
|
this._destroy$ = new Subject();
|
|
613
619
|
this._currentRefreshQuery$ = new BehaviorSubject(null);
|
|
614
|
-
this.
|
|
615
|
-
|
|
620
|
+
this._tokens$ = new BehaviorSubject({
|
|
621
|
+
token: null,
|
|
622
|
+
refreshToken: null,
|
|
623
|
+
});
|
|
616
624
|
const cookieToken = _config.refreshConfig?.cookieName ? getCookie(_config.refreshConfig.cookieName) ?? null : null;
|
|
617
|
-
this.
|
|
618
|
-
|
|
619
|
-
|
|
625
|
+
this._tokens$.next({
|
|
626
|
+
token: _config.token || null,
|
|
627
|
+
refreshToken: _config.refreshConfig?.token || cookieToken || null,
|
|
628
|
+
});
|
|
629
|
+
if (!this.tokens.token && !this.tokens.refreshToken) {
|
|
620
630
|
if (!_config.refreshConfig?.cookieName) {
|
|
621
631
|
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
632
|
}
|
|
623
633
|
return;
|
|
624
634
|
}
|
|
625
|
-
this.
|
|
635
|
+
if (this.tokens.token) {
|
|
636
|
+
this._prepareForRefresh();
|
|
637
|
+
if (this.tokens.refreshToken && this._config.refreshConfig?.cookieName) {
|
|
638
|
+
setCookie(this._config.refreshConfig.cookieName, this.tokens.refreshToken);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
else if (this.tokens.refreshToken) {
|
|
642
|
+
this._refreshQuery();
|
|
643
|
+
}
|
|
626
644
|
}
|
|
627
645
|
cleanUp() {
|
|
628
646
|
this._destroy$.next(true);
|
|
@@ -633,10 +651,10 @@ class BearerAuthProvider {
|
|
|
633
651
|
}
|
|
634
652
|
}
|
|
635
653
|
_prepareForRefresh() {
|
|
636
|
-
if (!this.
|
|
654
|
+
if (!this.tokens.token || !this._config.refreshConfig) {
|
|
637
655
|
return;
|
|
638
656
|
}
|
|
639
|
-
const bearer = decryptBearer(this.
|
|
657
|
+
const bearer = decryptBearer(this.tokens.token);
|
|
640
658
|
if (!bearer) {
|
|
641
659
|
return;
|
|
642
660
|
}
|
|
@@ -658,11 +676,11 @@ class BearerAuthProvider {
|
|
|
658
676
|
}
|
|
659
677
|
}
|
|
660
678
|
_refreshQuery() {
|
|
661
|
-
if (!this.
|
|
679
|
+
if (!this.tokens.refreshToken || !this._config.refreshConfig) {
|
|
662
680
|
return;
|
|
663
681
|
}
|
|
664
|
-
const args = this._config.refreshConfig.requestArgsAdapter?.(this.
|
|
665
|
-
body: { refreshToken: this.
|
|
682
|
+
const args = this._config.refreshConfig.requestArgsAdapter?.(this.tokens) ?? {
|
|
683
|
+
body: { refreshToken: this.tokens.refreshToken },
|
|
666
684
|
};
|
|
667
685
|
const query = this._config.refreshConfig.queryCreator.prepare(args).execute({ skipCache: true });
|
|
668
686
|
this._currentRefreshQuery$.next(query);
|
|
@@ -671,21 +689,21 @@ class BearerAuthProvider {
|
|
|
671
689
|
if (isQueryStateSuccess(state)) {
|
|
672
690
|
if (this._config.refreshConfig?.responseAdapter) {
|
|
673
691
|
const tokens = this._config.refreshConfig.responseAdapter(state.response);
|
|
674
|
-
this.
|
|
675
|
-
this._refreshToken = tokens.refreshToken || null;
|
|
692
|
+
this._tokens$.next(tokens);
|
|
676
693
|
}
|
|
677
694
|
else {
|
|
678
|
-
this.
|
|
679
|
-
|
|
695
|
+
this._tokens$.next({
|
|
696
|
+
token: state.response['token'],
|
|
697
|
+
refreshToken: state.response['refreshToken'],
|
|
698
|
+
});
|
|
680
699
|
}
|
|
681
|
-
if (this._config.refreshConfig?.cookieName && this.
|
|
682
|
-
setCookie(this._config.refreshConfig.cookieName, this.
|
|
700
|
+
if (this._config.refreshConfig?.cookieName && this.tokens.refreshToken) {
|
|
701
|
+
setCookie(this._config.refreshConfig.cookieName, this.tokens.refreshToken);
|
|
683
702
|
}
|
|
684
703
|
this._prepareForRefresh();
|
|
685
704
|
}
|
|
686
705
|
else if (isQueryStateFailure(state)) {
|
|
687
|
-
this.
|
|
688
|
-
this._refreshToken = null;
|
|
706
|
+
this._tokens$.next({ token: null, refreshToken: null });
|
|
689
707
|
if (this._config.refreshConfig?.cookieName) {
|
|
690
708
|
deleteCookie(this._config.refreshConfig.cookieName, '/');
|
|
691
709
|
}
|