zavadil-ts-common 1.2.36 → 1.2.37

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) {
48
+ console.log(`Redirecting to ${url}`);
49
+ this.redirecting = url;
50
+ document.location.href = url;
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);
@@ -60,9 +73,7 @@ export class RestClientWithOAuth extends RestClient {
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
+ this.redirectTo(location);
66
77
  }
67
78
  ).catch((err) => {
68
79
  console.error('Redirection failed: OAuth info not fetched:', err);
@@ -80,7 +91,6 @@ export class RestClientWithOAuth extends RestClient {
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
  }