@gandalan/weblibs 1.1.58 → 1.1.59

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.
@@ -11,10 +11,11 @@ import { isTokenValid, getRefreshToken } from "./fluentApi";
11
11
  * @property {function(string) : FluentAuth} useBaseUrl - Sets the base URL for authentication and returns the FluentApi object.
12
12
  * @property {function(string|null) : FluentAuth} useToken - Sets the JWT token and returns the FluentApi object.
13
13
  * @property {function(string|null) : FluentAuth} useRefreshToken - Sets the refresh token and returns the FluentApi object.
14
- * @property {Function} authenticate - Authenticates the user with username and password, or refreshes the token.
14
+ * @property {function() : string} 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
16
  * @property {Function} redirectToLogin - Redirects to the login page.
17
17
  * @property {Function} init - Initializes the authentication object.
18
+ * @property {function(string,string) : string} login - Logs in with the provided credentials.
18
19
  */
19
20
 
20
21
  /**
@@ -70,13 +71,11 @@ export function authBuilder() {
70
71
  },
71
72
 
72
73
  /**
73
- * Authenticates the user with username and password, or refreshes the token.
74
- *
75
- * @param {string} username
76
- * @param {string} password
77
- * @returns
74
+ * Authenticates the user with the JWT token or refreshes the token with
75
+ * the refreshToken set before
76
+ * @return {string} the JWT token
78
77
  */
79
- async authenticate(username = "", password = "") {
78
+ async authenticate() {
80
79
  console.log("authenticating:", this.token ? `token set, exp: ${jwtDecode(this.token).exp - (Date.now() / 1000)}` : "no token,", this.refreshToken, this.appToken);
81
80
 
82
81
  if (this.token && isTokenValid(this.token))
@@ -99,6 +98,17 @@ export function authBuilder() {
99
98
  return this.token;
100
99
  }
101
100
 
101
+ throw new Error("not authenticated");
102
+ },
103
+
104
+ /**
105
+ * Login with credentials and return the JWT token
106
+ * @param {string} username
107
+ * @param {string} password
108
+ * @return {string} the JWT token
109
+ */
110
+ async login(username = "", password = "")
111
+ {
102
112
  if (username && password) {
103
113
  const payload = { "Email": username, "Password": password, "AppToken": this.appToken };
104
114
  const res = await fetch(`${this.authUrl}/LoginJwt`,
@@ -106,16 +116,15 @@ export function authBuilder() {
106
116
  const temptoken = await res.json();
107
117
  if (temptoken) {
108
118
  this.token = temptoken;
119
+ this.refreshToken = getRefreshToken(temptoken);
109
120
  return this.token;
110
121
  }
111
122
  }
112
-
113
123
  throw new Error("not authenticated");
114
124
  },
115
125
 
116
126
  /**
117
- * try to refresh the token using the refresh token
118
- *
127
+ * try to refresh the JWT token by using the refreshToken
119
128
  * @async
120
129
  * @private
121
130
  * @param {string} [refreshToken=""]
@@ -132,6 +141,12 @@ export function authBuilder() {
132
141
  return res.ok ? await res.json() : null;
133
142
  },
134
143
 
144
+ /**
145
+ * Initializes the authentication object. Before calling, set the token and refresh token if available.
146
+ * If the token is not set, the refresh token will be used to try to refresh the token.
147
+ * If the token is not valid, the user will be redirected to the login page.
148
+ * If tokens are valid, they will be stored in the global variable idasTokens.
149
+ */
135
150
  async init()
136
151
  {
137
152
  if (!this.token && this.refreshToken)
@@ -141,8 +156,7 @@ export function authBuilder() {
141
156
 
142
157
  if (this.token && isTokenValid(this.token))
143
158
  {
144
- const decoded = jwtDecode(this.token);
145
- this.refreshToken = ("refreshToken" in decoded) ? decoded["refreshToken"] : null;
159
+ this.refreshToken = getRefreshToken(this.token);
146
160
  localStorage.setItem("idas-refresh-token", this.refreshToken);
147
161
  }
148
162
 
@@ -153,27 +167,24 @@ export function authBuilder() {
153
167
 
154
168
  // eslint-disable-next-line no-undef
155
169
  globalThis.idasTokens = { token: this.token, refreshToken: this.refreshToken, appToken: this.appToken };
156
- //await idasApi(appToken).get("/Version"); // Warm up authentication
157
170
  },
158
171
 
159
172
  /**
160
173
  * Redirect to the login page
161
- *
162
- * @param {string} [authPath=""]
163
174
  * @private
164
175
  */
165
- redirectToLogin(authPath = "") {
176
+ redirectToLogin() {
166
177
  if (!window) {
167
178
  return;
168
179
  }
169
180
 
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));
181
+ const redirectAfterAuth = new URL(window.location.href).origin;
182
+ let redirectUrl = `${redirectAfterAuth}?r=%target%&j=%jwt%&m=%mandant%`;
183
+ redirectUrl = redirectUrl.replace("%target%", encodeURIComponent(window.location.href));
173
184
 
174
185
  const url = new URL(this.authUrl);
175
186
  url.pathname = "/Session";
176
- url.search = `?a=${this.appToken}&r=${encodeURIComponent(authUrlCallback)}`;
187
+ url.search = `?a=${this.appToken}&r=${encodeURIComponent(redirectUrl)}`;
177
188
  let loginUrl = url.toString();
178
189
 
179
190
  window.location.href = loginUrl;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "1.1.58",
3
+ "version": "1.1.59",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"