@gandalan/weblibs 1.1.55 → 1.1.58

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/api/fluentApi.js CHANGED
@@ -197,6 +197,8 @@ export function createApi() {
197
197
  this.token = globalThis.idasTokens.token;
198
198
  // eslint-disable-next-line no-undef
199
199
  this.refreshToken = globalThis.idasTokens.refreshToken;
200
+ // eslint-disable-next-line no-undef
201
+ this.appToken = globalThis.idasTokens.appToken;
200
202
  return this;
201
203
  },
202
204
 
@@ -7,12 +7,14 @@ import { isTokenValid, getRefreshToken } from "./fluentApi";
7
7
  * @property {string} appToken - The application token.
8
8
  * @property {string} token - The JWT token.
9
9
  * @property {string} refreshToken - The refresh token.
10
- * @property {function(string) : FluentApi} useAppToken - Sets the application token and returns the FluentApi object.
11
- * @property {function(string) : FluentApi} useBaseUrl - Sets the base URL for authentication and returns the FluentApi object.
12
- * @property {function(string) : FluentApi} useToken - Sets the JWT token and returns the FluentApi object.
13
- * @property {function(string) : FluentApi} useRefreshToken - Sets the refresh token and returns the FluentApi object.
10
+ * @property {function(string) : FluentAuth} useAppToken - Sets the application token and returns the FluentApi object.
11
+ * @property {function(string) : FluentAuth} useBaseUrl - Sets the base URL for authentication and returns the FluentApi object.
12
+ * @property {function(string|null) : FluentAuth} useToken - Sets the JWT token and returns the FluentApi object.
13
+ * @property {function(string|null) : FluentAuth} useRefreshToken - Sets the refresh token and returns the FluentApi object.
14
14
  * @property {Function} authenticate - Authenticates the user with username and password, or refreshes the token.
15
15
  * @property {Function} tryRefreshToken - Attempts to refresh the authentication token using the refresh token.
16
+ * @property {Function} redirectToLogin - Redirects to the login page.
17
+ * @property {Function} init - Initializes the authentication object.
16
18
  */
17
19
 
18
20
  /**
@@ -75,7 +77,7 @@ export function authBuilder() {
75
77
  * @returns
76
78
  */
77
79
  async authenticate(username = "", password = "") {
78
- console.log("authenticating:", this.token ? `token set, exp: ${jwtDecode(this.token).exp - (Date.now() / 1000)}` : "no token,", this.refreshToken);
80
+ console.log("authenticating:", this.token ? `token set, exp: ${jwtDecode(this.token).exp - (Date.now() / 1000)}` : "no token,", this.refreshToken, this.appToken);
79
81
 
80
82
  if (this.token && isTokenValid(this.token))
81
83
  return this.token;
@@ -129,5 +131,52 @@ export function authBuilder() {
129
131
  });
130
132
  return res.ok ? await res.json() : null;
131
133
  },
134
+
135
+ async init()
136
+ {
137
+ if (!this.token && this.refreshToken)
138
+ {
139
+ this.token = await this.tryRefreshToken(this.refreshToken);
140
+ }
141
+
142
+ if (this.token && isTokenValid(this.token))
143
+ {
144
+ const decoded = jwtDecode(this.token);
145
+ this.refreshToken = ("refreshToken" in decoded) ? decoded["refreshToken"] : null;
146
+ localStorage.setItem("idas-refresh-token", this.refreshToken);
147
+ }
148
+
149
+ if (!isTokenValid(this.token))
150
+ {
151
+ this.redirectToLogin();
152
+ }
153
+
154
+ // eslint-disable-next-line no-undef
155
+ globalThis.idasTokens = { token: this.token, refreshToken: this.refreshToken, appToken: this.appToken };
156
+ //await idasApi(appToken).get("/Version"); // Warm up authentication
157
+ },
158
+
159
+ /**
160
+ * Redirect to the login page
161
+ *
162
+ * @param {string} [authPath=""]
163
+ * @private
164
+ */
165
+ redirectToLogin(authPath = "") {
166
+ if (!window) {
167
+ return;
168
+ }
169
+
170
+ const authEndpoint = (new URL(window.location.href).origin) + authPath;
171
+ let authUrlCallback = `${authEndpoint}?r=%target%&j=%jwt%&m=%mandant%`;
172
+ authUrlCallback = authUrlCallback.replace("%target%", encodeURIComponent(window.location.href));
173
+
174
+ const url = new URL(this.authUrl);
175
+ url.pathname = "/Session";
176
+ url.search = `?a=${this.appToken}&r=${encodeURIComponent(authUrlCallback)}`;
177
+ let loginUrl = url.toString();
178
+
179
+ window.location.href = loginUrl;
180
+ }
132
181
  };
133
182
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "1.1.55",
3
+ "version": "1.1.58",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"