zavadil-ts-common 1.1.49 → 1.1.51

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.
@@ -2,6 +2,7 @@ import {OAuthTokenManager} from "./OAuthTokenManager";
2
2
  import {RestClient, RestClientHeaders} from "../client";
3
3
  import {IdTokenPayload} from "./OAuthRestClient";
4
4
  import {LazyAsync} from "../cache";
5
+ import {StringUtil} from "../util";
5
6
 
6
7
  export type ServerOAuthInfoPayload = {
7
8
  debugMode?: boolean;
@@ -123,4 +124,26 @@ export class RestClientWithOAuth extends RestClient {
123
124
  );
124
125
  }
125
126
 
127
+ /**
128
+ * Try to obtain access token, then return true or reject.
129
+ * This is basically only used when initializing and trying to determine whether we need to redirect user to login page
130
+ */
131
+ checkAccessToken(privilege?: string): Promise<boolean> {
132
+ return this.getTokenManager()
133
+ .then(tm => tm.getAccessToken(StringUtil.getNonEmpty(privilege, this.defaultPrivilege)))
134
+ .then((accessToken) => true);
135
+ }
136
+
137
+ checkAndRedirectToLogin(): Promise<boolean> {
138
+ return this.checkAccessToken()
139
+ .catch(
140
+ () => this.getServerInfo().then(
141
+ (si) => {
142
+ document.location = `${si.oauthServerUrl}/login?app_name=${si.targetAudience}&redirect_url=${document.location}`;
143
+ return Promise.resolve(false);
144
+ }
145
+ )
146
+ );
147
+ }
148
+
126
149
  }