@gandalan/weblibs 1.0.26 → 1.0.27

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/IDAS.js CHANGED
@@ -1,4 +1,4 @@
1
- import { jwtTokenInvalid } from "./authUtils";
1
+ import { isInvalid } from "./authUtils";
2
2
  import { RESTClient } from "./RESTClient";
3
3
  import jwt_decode from 'jwt-decode';
4
4
 
@@ -14,7 +14,7 @@ export function IDASFactory(settings)
14
14
  };
15
15
  settings = { ...defaultSettings, ...settings };
16
16
  let idas = undefined;
17
- if (!jwtTokenInvalid(settings))
17
+ if (!isInvalid(settings))
18
18
  {
19
19
  console.log("init: with JWT token");
20
20
  idas = new IDAS(settings);
package/api/RESTClient.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import axios from "axios";
2
- import { jwtTokenInvalid, jwtTokenRenew } from "./authUtils";
2
+ import { isInvalid, tryRenew } from "./authUtils";
3
3
 
4
4
  export class RESTClient {
5
5
  lastError = "";
@@ -23,8 +23,8 @@ export class RESTClient {
23
23
  }
24
24
 
25
25
  async checkTokenBeforeRequest(config) {
26
- if (this.settings.jwtToken && jwtTokenInvalid(this.settings)) { // ignore custom/different JWT tokens
27
- await jwtTokenRenew(this.settings);
26
+ if (this.settings.jwtToken && isInvalid(this.settings)) { // ignore custom/different JWT tokens
27
+ await tryRenew(this.settings);
28
28
  console.log(`Updating Header with new JWT Token: ${this.settings.jwtToken}`);
29
29
  this.axiosInstance.headers = {
30
30
  "Authorization" : `Bearer ${ this.settings.jwtToken }`
package/api/authUtils.js CHANGED
@@ -1,16 +1,51 @@
1
1
  import { RESTClient } from "./RESTClient";
2
2
  import jwt_decode from "jwt-decode";
3
3
 
4
+ export async function initIDAS(appToken) {
5
+
6
+ let jwtToken = "";
7
+ let mandantGuid = "";
8
+ let apiBaseurl = "https://api.dev.idas-cloudservices.net/api/";
9
+ let jwtRefreshToken = localStorage.getItem("IDAS_AuthJwtRefreshToken");
10
+
11
+ let urlParams = new URLSearchParams(location.search);
12
+ if (urlParams.has("m")) mandantGuid = urlParams.get("m");
13
+ if (urlParams.has("a")) apiBaseurl = urlParams.get("a");
14
+ if (urlParams.has("j")) jwtToken = urlParams.get("j");
15
+ if (urlParams.has("t")) jwtRefreshToken = urlParams.get("t");
16
+
17
+ let settings = {
18
+ appToken,
19
+ mandantGuid,
20
+ apiBaseurl,
21
+ jwtToken: jwtToken,
22
+ jwtRefreshToken,
23
+ //jwtCallbackPath: localStorage.getItem("IDAS_AuthJwtCallbackPath")
24
+ }
25
+
26
+ try {
27
+ await setup(settings);
28
+ if (isInvalid(settings))
29
+ redirectToLogin(settings, "/");
30
+ }
31
+ catch
32
+ {
33
+ redirectToLogin(settings, "/");
34
+ }
35
+ return settings;
36
+ }
37
+
38
+
4
39
  export async function setup(settings)
5
40
  {
6
- console.log("Setup");
41
+ console.log("Setup IDAS");
7
42
  if (!settings.jwtToken && !settings.jwtRefreshToken)
8
43
  throw("Either jwtToken or jwtRefreshToken must be set to authenticate");
9
44
 
10
- if (settings.jwtRefreshToken && jwtTokenInvalid(settings))
45
+ if (settings.jwtRefreshToken && isInvalid(settings))
11
46
  {
12
- await jwtTokenRenew(settings);
13
- if (jwtTokenInvalid(settings))
47
+ await tryRenew(settings);
48
+ if (isInvalid(settings))
14
49
  console.log("Refresh failed, invalid JWT token!");
15
50
 
16
51
  } else {
@@ -27,7 +62,7 @@ export async function setup(settings)
27
62
  console.log("Setup finished", settings);
28
63
  }
29
64
 
30
- export function jwtTokenInvalid(settings)
65
+ export function isInvalid(settings)
31
66
  {
32
67
  if (!settings.jwtToken)
33
68
  return true;
@@ -38,7 +73,7 @@ export function jwtTokenInvalid(settings)
38
73
  return true;
39
74
  }
40
75
 
41
- export async function jwtTokenRenew(settings)
76
+ export async function tryRenew(settings)
42
77
  {
43
78
  console.log("try to refresh");
44
79
  const renewSettings = { ...settings, jwtToken : undefined };
@@ -57,13 +92,13 @@ export async function jwtTokenRenew(settings)
57
92
  localStorage.setItem("IDAS_AuthJwtRefreshToken", refreshToken);
58
93
  }
59
94
 
60
- if (jwtTokenInvalid(settings))
95
+ if (isInvalid(settings))
61
96
  console.log("Token is already expired!");
62
97
  }
63
98
 
64
- export function jwtAuthenticateOnBackend(settings, authPath)
99
+ export function redirectToLogin(settings, authPath)
65
100
  {
66
- localStorage.setItem("IDAS_AuthJwtCallbackPath", authPath || "");
101
+ //localStorage.setItem("IDAS_AuthJwtCallbackPath", authPath || "");
67
102
  const authEndpoint = (new URL(window.location.href).origin) + authPath;
68
103
  let authUrlCallback = `${authEndpoint}?r=%target%&j=%jwt%&m=%mandant%`;
69
104
  authUrlCallback = authUrlCallback.replace("%target%", encodeURIComponent(window.location.href));
package/index.js CHANGED
@@ -9,10 +9,10 @@ import SaveButton from "./components/SaveButton.svelte";
9
9
 
10
10
  export {
11
11
  DataGrid, Datepicker, Inputbox, Dialog, GanTable,
12
- AddButton, RemoveButton, SaveButton,
12
+ AddButton, RemoveButton, SaveButton
13
13
  }
14
14
 
15
15
  import { IDASFactory } from "./api/IDAS";
16
16
  import { RESTClient } from "./api/RESTClient";
17
-
18
- export { IDASFactory, RESTClient };
17
+ import { initIDAS } from "./api/authUtils";
18
+ export { IDASFactory, RESTClient, initIDAS };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gandalan/weblibs",
3
- "version": "1.0.26",
3
+ "version": "1.0.27",
4
4
  "description": "WebLibs for Gandalan JS/TS/Svelte projects",
5
5
  "keywords": [
6
6
  "gandalan"