@iservice365/layer-common 0.2.0 → 0.2.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @iservice365/layer-common
2
2
 
3
+ ## 0.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - ff09e54: Breaking change - switched to session based authentication
8
+
3
9
  ## 0.2.0
4
10
 
5
11
  ### Minor Changes
@@ -40,15 +40,13 @@ export default function useLocalAuth() {
40
40
  });
41
41
  }
42
42
 
43
- function setToken({ refreshToken = "", accessToken = "", user = "" }) {
44
- useCookie("accessToken", cookieConfig).value = accessToken;
45
- useCookie("refreshToken", cookieConfig).value = refreshToken;
43
+ function setSession({ sid = "", user = "" }) {
44
+ useCookie("sid", cookieConfig).value = sid;
46
45
  useCookie("user", cookieConfig).value = user;
47
46
  }
48
47
 
49
48
  function clearCookies() {
50
- useCookie("accessToken", cookieConfig).value = null;
51
- useCookie("refreshToken", cookieConfig).value = null;
49
+ useCookie("sid", cookieConfig).value = null;
52
50
  useCookie("user", cookieConfig).value = null;
53
51
  useCookie("organization", cookieConfig).value = null;
54
52
  }
@@ -134,10 +132,10 @@ export default function useLocalAuth() {
134
132
  return {
135
133
  authenticate,
136
134
  login,
135
+ setSession,
137
136
  logout,
138
137
  clearCookies,
139
138
  getCurrentUser,
140
- setToken,
141
139
  forgotPassword,
142
140
  resetPassword,
143
141
  currentUser,
@@ -1,52 +1,13 @@
1
- export function useLocalSetup(type: string) {
2
- const { currentUser } = useLocalAuth();
3
-
4
- const userId = computed(() => currentUser.value?._id ?? "");
5
-
6
- const { getByUserIdType } = useMember();
7
-
8
- const { data: userMemberData } = useAsyncData(
9
- "get-member-by-id",
10
- () => getByUserIdType(userId.value, type),
11
- { watch: [userId], lazy: true }
1
+ export function useLocalSetup() {
2
+ const userAppRole = useState<Record<string, any> | null>(
3
+ "userAppRole",
4
+ () => null
12
5
  );
13
6
 
14
- const { getRoleById } = useRole();
15
-
16
- const roleId = computed(() => userMemberData.value?.role ?? "");
17
- const orgId = computed(() => userMemberData.value?.org ?? "");
18
-
19
- const userAppRole = useState<TRole | null>("userAppRole", () => null);
20
-
21
- const { data: getRoleByIdReq } = useAsyncData(
22
- "get-role-by-id",
23
- () => getRoleById(roleId.value),
24
- { watch: [roleId], immediate: false, lazy: true }
25
- );
26
-
27
- watchEffect(() => {
28
- if (getRoleByIdReq.value) {
29
- userAppRole.value = getRoleByIdReq.value;
30
- }
31
- });
32
-
33
- const { getByServiceProviderOrgIdType } = useServiceProvider();
34
-
35
- const { data: serviceProvider } = useAsyncData(
36
- "get-all-service-providers",
37
- () =>
38
- getByServiceProviderOrgIdType({
39
- id: orgId.value,
40
- type: "property_management_agency",
41
- }),
42
- {
43
- watch: [orgId],
44
- lazy: true,
45
- }
46
- );
7
+ const id = useState<string | null>("memberShipOrgId", () => null);
47
8
 
48
9
  return {
49
10
  userAppRole,
50
- serviceProvider,
11
+ id,
51
12
  };
52
13
  }
@@ -29,6 +29,13 @@ export default function useMember() {
29
29
  return useNuxtApp().$api<TMember>(`/api/members/user/${user}/app/${type}`);
30
30
  }
31
31
 
32
+ function getByUserType(user: string, type: string, org?: string) {
33
+ return useNuxtApp().$api<TMember>(`/api/members/user/${user}/app/${type}`, {
34
+ method: "GET",
35
+ query: { org },
36
+ });
37
+ }
38
+
32
39
  async function getAll({
33
40
  page = 1,
34
41
  search = "",
@@ -92,7 +99,7 @@ export default function useMember() {
92
99
  page,
93
100
  pages,
94
101
  pageRange,
95
-
102
+ getByUserType,
96
103
  getAll,
97
104
  getByUserId,
98
105
  createUserByVerification,
@@ -0,0 +1,4 @@
1
+ export default defineNuxtRouteMiddleware(() => {
2
+ if (import.meta.server) return;
3
+ const adminMember = useCookie("admin-member").value ?? "";
4
+ });
package/nuxt.config.ts CHANGED
@@ -13,6 +13,7 @@ export default defineNuxtConfig({
13
13
  secure: true,
14
14
  maxAge: 30 * 24 * 60 * 60,
15
15
  },
16
+ APP: (process.env.APP as string) ?? "App",
16
17
  API_DO_STORAGE_ENDPOINT:
17
18
  (process.env.API_DO_STORAGE_ENDPOINT as string) ?? "",
18
19
  APP_NAME: (process.env.APP_NAME as string) ?? "App",
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@iservice365/layer-common",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "0.2.0",
5
+ "version": "0.2.1",
6
6
  "main": "./nuxt.config.ts",
7
7
  "publishConfig": {
8
8
  "access": "public"
package/plugins/API.ts CHANGED
@@ -7,29 +7,8 @@ export default defineNuxtPlugin(() => {
7
7
  retryStatusCodes: [401],
8
8
  retryDelay: 500,
9
9
  onRequest({ options }) {
10
- const accessToken = useCookie("accessToken", cookieConfig).value;
11
- options.headers.set("Authorization", `Bearer ${accessToken}`);
12
- },
13
-
14
- async onResponseError({ response }) {
15
- if (response.status === 401) {
16
- await $fetch("/api/auth", {
17
- baseURL: "/",
18
- method: "PUT",
19
- server: false,
20
- credentials: "include",
21
- body: JSON.stringify({
22
- token: useCookie("refreshToken", cookieConfig).value,
23
- }),
24
-
25
- onResponse({ response }) {
26
- if (response.status === 200) {
27
- useCookie("accessToken", cookieConfig).value =
28
- response._data.token;
29
- }
30
- },
31
- });
32
- }
10
+ const sid = useCookie("sid", cookieConfig).value ?? "";
11
+ options.headers.set("Authorization", sid);
33
12
  },
34
13
  });
35
14
 
@@ -0,0 +1,54 @@
1
+ export default defineNuxtPlugin(() => {
2
+ const router = useRouter();
3
+ const { getByUserType } = useMember();
4
+ const { getRoleById } = useRole();
5
+
6
+ const { userAppRole, id } = useLocalSetup();
7
+
8
+ router.afterEach((to) => {
9
+ const isMember = to.meta?.memberOnly;
10
+ if (!isMember) return;
11
+
12
+ const APP = useRuntimeConfig().public.APP;
13
+ const org = (to.params.org as string) ?? "";
14
+
15
+ const userId = computed(() => useCookie("user").value ?? "");
16
+
17
+ const { data: userMemberData, error: userMemberError } = useLazyAsyncData(
18
+ "get-member-by-id",
19
+ () => getByUserType(userId.value, APP, org),
20
+ { watch: [userId] }
21
+ );
22
+
23
+ watchEffect(() => {
24
+ if (userMemberError.value) {
25
+ navigateTo(
26
+ {
27
+ name: "index",
28
+ },
29
+ { replace: true }
30
+ );
31
+ }
32
+ });
33
+
34
+ watchEffect(() => {
35
+ if (userMemberData.value) {
36
+ id.value = userMemberData.value.org ?? "";
37
+ }
38
+ });
39
+
40
+ const roleId = computed(() => userMemberData.value?.role ?? "");
41
+
42
+ const { data: getRoleByIdReq } = useLazyAsyncData(
43
+ "get-role-by-id",
44
+ () => getRoleById(roleId.value),
45
+ { watch: [roleId], immediate: false }
46
+ );
47
+
48
+ watchEffect(() => {
49
+ if (getRoleByIdReq.value) {
50
+ userAppRole.value = getRoleByIdReq.value;
51
+ }
52
+ });
53
+ });
54
+ });
package/types/local.d.ts CHANGED
@@ -14,6 +14,7 @@ declare type TNavigationItem = {
14
14
  icon?: string;
15
15
  route?: TNavigationRoute;
16
16
  children?: TNavigationItem[];
17
+ link?: string
17
18
  };
18
19
 
19
20
  declare type TKeyValuePair<