@luizleon/sf.prefeiturasp.vuecomponents 0.0.32 → 0.0.34

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.
@@ -1,4 +1,4 @@
1
- import Keycloak, { KeycloakConfig } from "./../keycloak";
1
+ import Keycloak, { KeycloakConfig, KeycloakInitOptions } from "./../keycloak";
2
2
  interface User {
3
3
  name: string;
4
4
  email: string;
@@ -14,7 +14,7 @@ interface User {
14
14
  declare const UseAuthService: (config: KeycloakConfig) => {
15
15
  Instance: Keycloak;
16
16
  User: User;
17
- CallLogin: (onSuccess: () => void, onError: (error: any) => void) => void;
18
- CallLogout: () => void;
17
+ Init: (initOptions?: KeycloakInitOptions, callback?: () => void) => Promise<void>;
18
+ Logout: () => void;
19
19
  };
20
20
  export { UseAuthService };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luizleon/sf.prefeiturasp.vuecomponents",
3
- "version": "0.0.32",
3
+ "version": "0.0.34",
4
4
  "type": "module",
5
5
  "main": "dist/lib.umd.js",
6
6
  "module": "dist/lib.es.js",
@@ -1,4 +1,7 @@
1
- import Keycloak, { KeycloakConfig } from "./../keycloak";
1
+ import Keycloak, {
2
+ KeycloakConfig,
3
+ KeycloakInitOptions,
4
+ } from "./../keycloak";
2
5
 
3
6
  interface User {
4
7
  name: string;
@@ -13,25 +16,44 @@ interface User {
13
16
  IsInRole: (role: string) => boolean;
14
17
  }
15
18
 
19
+ async function CheckSilentLoginFile() {
20
+ const fileName = "silent-login.html";
21
+ const result = await fetch(location.origin + "/" + fileName)
22
+ .then((r) => {
23
+ return r.status === 200;
24
+ })
25
+ .catch(() => {
26
+ return false;
27
+ });
28
+ if (!result) {
29
+ console.warn(`Arquivo ${fileName} não localizado no projeto!`);
30
+ document.body.innerHTML = `<div style="padding: 3rem; display: flex; justify-content: center; align-items: center; height: 100vh; width: 100vw; font-size: 1.5rem;">O arquivo '${fileName}' não foi encontrado.</div>`;
31
+ }
32
+ return result;
33
+ }
34
+
16
35
  function AuthService(config: KeycloakConfig) {
17
36
  const keycloakInstance = new Keycloak(config);
18
37
 
19
38
  const User: User = {} as User;
20
39
 
21
- const Login = (
22
- onSuccess: () => void,
23
- onError: (error: any) => void
40
+ const Init = async (
41
+ initOptions: KeycloakInitOptions = {},
42
+ callback?: () => void
24
43
  ) => {
25
- keycloakInstance
26
- .init({ onLoad: "login-required" })
44
+ const check = await CheckSilentLoginFile();
45
+ if (!check) return;
46
+ return keycloakInstance
47
+ .init(initOptions)
27
48
  .then(async (authenticated) => {
28
49
  if (authenticated) {
29
50
  await FetchUserInfo();
30
51
  }
31
- onSuccess();
52
+ if (callback && typeof callback === "function") callback();
32
53
  })
33
54
  .catch((e: any) => {
34
- onError(e);
55
+ console.error(e);
56
+ document.body.innerHTML = `<div style="padding: 3rem; display: flex; justify-content: center; align-items: center; height: 100vh; width: 100vw; font-size: 1.5rem;">Não foi possível conectar no servidor de autenticação.</div>`;
35
57
  });
36
58
  };
37
59
 
@@ -65,8 +87,8 @@ function AuthService(config: KeycloakConfig) {
65
87
  return {
66
88
  Instance: keycloakInstance,
67
89
  User,
68
- CallLogin: Login,
69
- CallLogout: Logout,
90
+ Init,
91
+ Logout,
70
92
  };
71
93
  }
72
94