@luizleon/sf.prefeiturasp.vuecomponents 0.0.26 → 0.0.28

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 from "./../keycloak";
1
+ import Keycloak, { KeycloakConfig } from "./../keycloak";
2
2
  interface User {
3
3
  name: string;
4
4
  email: string;
@@ -11,13 +11,10 @@ interface User {
11
11
  sub: string;
12
12
  IsInRole: (role: string) => boolean;
13
13
  }
14
- declare const AuthService: {
14
+ declare const UseAuthService: (config: KeycloakConfig) => {
15
15
  Instance: Keycloak;
16
16
  User: User;
17
- SetUrl: (url: string) => void;
18
- SetRealm: (realm: string) => void;
19
- SetClientId: (clientId: string) => void;
20
17
  CallLogin: (onSuccess: () => void, onError: (error: any) => void) => void;
21
18
  CallLogout: () => void;
22
19
  };
23
- export { AuthService };
20
+ export { UseAuthService };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luizleon/sf.prefeiturasp.vuecomponents",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "type": "module",
5
5
  "main": "dist/lib.umd.js",
6
6
  "module": "dist/lib.es.js",
@@ -1,22 +1,26 @@
1
1
  <script setup lang="ts">
2
- import { AuthService } from "../../services/authService";
2
+ import { computed, onMounted, ref } from "vue";
3
3
  import { UseDialogService } from "../../services/dialogService";
4
4
 
5
5
  const dialogService = UseDialogService();
6
6
 
7
- const letters = Letters();
7
+ const letters = ref("");
8
+
9
+ const emit = defineEmits<{
10
+ (event: "logout"): void;
11
+ }>();
8
12
 
9
13
  async function Logout() {
10
14
  const confirm = await dialogService.ConfirmAsync({
11
- text: `${AuthService.User.username} - ${AuthService.User.name}.<br /><br />Você deseja sair da conta?`,
15
+ text: `Você deseja sair da conta?`,
12
16
  confirmLabel: "sair",
13
17
  });
14
18
  if (!confirm) return;
15
- AuthService.CallLogout();
19
+ emit("logout");
16
20
  }
17
21
 
18
22
  function Letters() {
19
- const name = AuthService.User.name;
23
+ const name = sessionStorage.getItem("name");
20
24
  let letters = "?";
21
25
  if (!name) return letters;
22
26
 
@@ -32,18 +36,22 @@ function Letters() {
32
36
  return letters.length > 0 ? letters : "?";
33
37
  }
34
38
 
35
- function Color() {
36
- const primeiraLetra = letters[0];
39
+ const color = computed(() => {
40
+ const primeiraLetra = letters.value[0];
37
41
  const cor =
38
42
  primeiraLetra === "?"
39
43
  ? 0
40
44
  : (primeiraLetra.charCodeAt(0) - 64) % 19;
41
45
  return Math.max(cor, 0);
42
- }
46
+ });
47
+
48
+ onMounted(() => {
49
+ letters.value = Letters();
50
+ });
43
51
  </script>
44
52
 
45
53
  <template>
46
- <div id="sf-header-avatar" :data-cor="Color()" @click="Logout">
54
+ <div id="sf-header-avatar" :data-cor="color" @click="Logout">
47
55
  {{ letters }}
48
56
  </div>
49
57
  </template>
@@ -6,6 +6,10 @@ import MenuIcon from "../internal/MenuIcon.vue";
6
6
  import ThemeToggle from "../internal/ThemeToggle.vue";
7
7
 
8
8
  const navService = UseNavMenuService();
9
+
10
+ const emit = defineEmits<{
11
+ (event: "logout"): void;
12
+ }>();
9
13
  </script>
10
14
 
11
15
  <template>
@@ -26,7 +30,7 @@ const navService = UseNavMenuService();
26
30
  </div>
27
31
  <slot name="action"> </slot>
28
32
  <ThemeToggle />
29
- <HeaderAvatar />
33
+ <HeaderAvatar @logout="" />
30
34
  </header>
31
35
  <nav :class="{ visible: navService.IsVisible }">
32
36
  <div class="sf-layout-nav-header">
package/src/index.ts CHANGED
@@ -7,7 +7,8 @@ import SfButton from "./components/button/Button.vue";
7
7
 
8
8
  import { UseNavMenuService } from "./services/navMenuService";
9
9
  import { UseDialogService } from "./services/dialogService";
10
- import { AuthService } from "./services/authService";
10
+ import { ThemeToggleBase } from "./components/internal/ThemeToggle";
11
+ import { UseAuthService } from "./services/authService";
11
12
  import { AppResult } from "./common/appResult";
12
13
 
13
14
  import { Cor, Tamanho } from "./enum";
@@ -24,8 +25,8 @@ export {
24
25
  SfContent,
25
26
  SfTabNavigation,
26
27
  SfButton,
27
- AuthService,
28
28
  AppResult,
29
+ UseAuthService,
29
30
  UseNavMenuService,
30
31
  UseDialogService,
31
32
  Cor,
@@ -52,8 +53,6 @@ export {
52
53
  window.addEventListener("resize", Ajusta);
53
54
  })();
54
55
 
55
- import { ThemeToggleBase } from "./components/internal/ThemeToggle";
56
-
57
56
  /**
58
57
  * Tema inicial
59
58
  */
package/src/keycloak.js CHANGED
@@ -23,7 +23,7 @@
23
23
  *
24
24
  * Adaptado para expor setToken.
25
25
  *
26
- * Adpatado parausar URL absoluta em configUrl (linha 829).
26
+ * Adpatado para usar URL absoluta em configUrl (linha 829). *
27
27
  */
28
28
 
29
29
  import base64 from 'base64-js';
@@ -1,4 +1,4 @@
1
- import Keycloak from "./../keycloak";
1
+ import Keycloak, { KeycloakConfig } from "./../keycloak";
2
2
 
3
3
  interface User {
4
4
  name: string;
@@ -13,65 +13,64 @@ interface User {
13
13
  IsInRole: (role: string) => boolean;
14
14
  }
15
15
 
16
- const keycloakInstance = new Keycloak();
16
+ function AuthService(config: KeycloakConfig) {
17
+ const keycloakInstance = new Keycloak(config);
17
18
 
18
- const Login = (
19
- onSuccess: () => void,
20
- onError: (error: any) => void
21
- ) => {
22
- keycloakInstance
23
- .init({ onLoad: "login-required" })
24
- .then(async (authenticated) => {
25
- if (authenticated) {
26
- await FetchUserInfo();
27
- }
28
- onSuccess();
29
- })
30
- .catch((e: any) => {
31
- onError(e);
32
- });
33
- };
19
+ const User: User = {} as User;
34
20
 
35
- const FetchUserInfo = async () => {
36
- const info: any = await keycloakInstance.loadUserInfo();
37
- AuthService.User.email = info.email ?? "";
38
- AuthService.User.emailVerified = info.email_verified === true;
39
- AuthService.User.firstName = info.given_name ?? "";
40
- AuthService.User.lastName = info.family_name ?? "";
41
- AuthService.User.name = info.name ?? "";
42
- AuthService.User.sub = info.sub ?? "";
43
- AuthService.User.username = info.preferred_username ?? "";
44
- AuthService.User.roles = [...(info.role ?? [])];
45
- AuthService.User.groups = [...(info.group ?? [])];
46
- AuthService.User.IsInRole = (role: string) => {
47
- return AuthService.User.roles.includes(role);
21
+ const Login = (
22
+ onSuccess: () => void,
23
+ onError: (error: any) => void
24
+ ) => {
25
+ keycloakInstance
26
+ .init({ onLoad: "login-required" })
27
+ .then(async (authenticated) => {
28
+ if (authenticated) {
29
+ await FetchUserInfo();
30
+ }
31
+ onSuccess();
32
+ })
33
+ .catch((e: any) => {
34
+ onError(e);
35
+ });
48
36
  };
49
- };
50
37
 
51
- const Logout = () => {
52
- keycloakInstance
53
- .logout({
54
- redirectUri: location.origin,
55
- })
56
- .then(() => {
57
- sessionStorage.removeItem("user-info");
58
- });
59
- };
38
+ const FetchUserInfo = async () => {
39
+ const info: any = await keycloakInstance.loadUserInfo();
40
+ User.email = info.email ?? "";
41
+ User.emailVerified = info.email_verified === true;
42
+ User.firstName = info.given_name ?? "";
43
+ User.lastName = info.family_name ?? "";
44
+ User.name = info.name ?? "";
45
+ User.sub = info.sub ?? "";
46
+ User.username = info.preferred_username ?? "";
47
+ User.roles = [...(info.role ?? [])];
48
+ User.groups = [...(info.group ?? [])];
49
+ User.IsInRole = (role: string) => {
50
+ return User.roles.includes(role);
51
+ };
52
+ sessionStorage.setItem("name", User.name);
53
+ };
54
+
55
+ const Logout = () => {
56
+ keycloakInstance
57
+ .logout({
58
+ redirectUri: location.origin,
59
+ })
60
+ .then(() => {
61
+ sessionStorage.removeItem("name");
62
+ });
63
+ };
64
+
65
+ return {
66
+ Instance: keycloakInstance,
67
+ User,
68
+ CallLogin: Login,
69
+ CallLogout: Logout,
70
+ };
71
+ }
60
72
 
61
- const AuthService = {
62
- Instance: keycloakInstance,
63
- User: {} as User,
64
- SetUrl: (url: string) => {
65
- keycloakInstance.authServerUrl = url;
66
- },
67
- SetRealm: (realm: string) => {
68
- keycloakInstance.realm = realm;
69
- },
70
- SetClientId: (clientId: string) => {
71
- keycloakInstance.clientId = clientId;
72
- },
73
- CallLogin: Login,
74
- CallLogout: Logout,
75
- };
73
+ const UseAuthService = (config: KeycloakConfig) =>
74
+ AuthService(config);
76
75
 
77
- export { AuthService };
76
+ export { UseAuthService };