zavadil-ts-common 1.2.36 → 1.2.38

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.
@@ -23,7 +23,7 @@ export class RestClientWithOAuth extends RestClient {
23
23
 
24
24
  private defaultPrivilege: string;
25
25
 
26
- private redirecting: boolean = false;
26
+ private redirecting?: string;
27
27
 
28
28
  constructor(url: string, defaultPrivilege: string = '*') {
29
29
  super(url);
@@ -36,15 +36,28 @@ export class RestClientWithOAuth extends RestClient {
36
36
  this.tokenManager = new LazyAsync<OAuthTokenManager>(() => this.getTokenManagerInternal());
37
37
  }
38
38
 
39
+ isRedirecting(): boolean {
40
+ return StringUtil.isEmpty(this.redirecting);
41
+ }
42
+
43
+ redirectingTo(): string {
44
+ return StringUtil.getNonEmpty(this.redirecting);
45
+ }
46
+
47
+ redirectTo(url: string): Promise<any> {
48
+ this.redirecting = url;
49
+ document.location.href = url;
50
+ return Promise.reject(`Redirecting to ${location}`);
51
+ }
52
+
39
53
  initializeIdToken(): Promise<any> {
40
54
  const urlToken = this.getIdTokenFromUrl();
41
55
  if (urlToken !== null) {
42
- return this.setIdTokenRaw(urlToken)
43
- .then(() => {
44
- console.log("Redirecting to url without id token");
45
- this.redirecting = true;
46
- document.location.href = this.deleteIdTokenFromUrl();
47
- });
56
+ return this
57
+ .setIdTokenRaw(urlToken)
58
+ .then(
59
+ () => this.redirectTo(this.deleteIdTokenFromUrl())
60
+ );
48
61
  } else {
49
62
  const storageToken = this.getIdTokenFromLocalStorage();
50
63
  if (storageToken) return this.setIdToken(storageToken);
@@ -56,13 +69,11 @@ export class RestClientWithOAuth extends RestClient {
56
69
  * Attempt to get ID token from URL or storage, redirect to login page when not successful
57
70
  */
58
71
  redirectToLogin(): Promise<any> {
59
- if (this.redirecting) return Promise.reject("Already redirecting!");
72
+ if (this.isRedirecting()) return Promise.reject("Already redirecting!");
60
73
  return this.getServerInfo().then(
61
74
  (si) => {
62
75
  const location = `${si.oauthServerUrl}/login?app_name=${si.targetAudience}&redirect_url=${this.deleteIdTokenFromUrl()}`;
63
- console.log('Redirecting to login page:', location);
64
- this.redirecting = true;
65
- document.location.href = location;
76
+ return this.redirectTo(location);
66
77
  }
67
78
  ).catch((err) => {
68
79
  console.error('Redirection failed: OAuth info not fetched:', err);
@@ -76,11 +87,10 @@ export class RestClientWithOAuth extends RestClient {
76
87
  initialize(): Promise<any> {
77
88
  return this.initializeIdToken()
78
89
  .then(() => {
79
- if (!this.redirecting) return this.checkAccessToken();
90
+ if (!this.isRedirecting()) return this.checkAccessToken();
80
91
  })
81
92
  .catch(
82
93
  (reason) => {
83
- if (this.redirecting) return Promise.reject("Already redirecting!");
84
94
  console.log('OAuth initialization failed:', reason);
85
95
  return this.redirectToLogin();
86
96
  }