@eeplatform/nuxt-layer-common 1.2.3 → 1.2.5

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,17 @@
1
1
  # @eeplatform/nuxt-layer-common
2
2
 
3
+ ## 1.2.5
4
+
5
+ ### Patch Changes
6
+
7
+ - 3bf853d: Add util function
8
+
9
+ ## 1.2.4
10
+
11
+ ### Patch Changes
12
+
13
+ - 9036391: Revise API plugin
14
+
3
15
  ## 1.2.3
4
16
 
5
17
  ### Patch Changes
@@ -8,14 +8,6 @@ export default function useLocalAuth() {
8
8
  return;
9
9
  }
10
10
 
11
- // Get access token from cookies
12
- const accessToken = useCookie("accessToken", cookieConfig).value;
13
-
14
- if (!accessToken) {
15
- // Redirect to login page if no access token
16
- navigateTo({ name: "index" });
17
- }
18
-
19
11
  const user = useCookie("user", cookieConfig).value;
20
12
 
21
13
  const { data: getCurrentUserReq, error: getCurrentUserErr } =
@@ -44,6 +36,11 @@ export default function useLocalAuth() {
44
36
  });
45
37
  }
46
38
 
39
+ function setSession({ sid = "", user = "" } = {}) {
40
+ useCookie("sid", cookieConfig).value = sid;
41
+ useCookie("user", cookieConfig).value = user;
42
+ }
43
+
47
44
  function setToken({
48
45
  refreshToken = "",
49
46
  accessToken = "",
@@ -64,18 +61,9 @@ export default function useLocalAuth() {
64
61
  }
65
62
 
66
63
  async function logout() {
67
- const refreshToken = useCookie("refreshToken", cookieConfig).value;
68
- if (refreshToken) {
69
- try {
70
- await useNuxtApp().$api(`/api/auth/logout/${refreshToken}`, {
71
- method: "DELETE",
72
- });
73
-
74
- clearCookies();
75
- } catch (error) {
76
- console.error("Logout failed:", error);
77
- }
78
- }
64
+ return useNuxtApp().$api("/api/auth/logout", {
65
+ method: "DELETE",
66
+ });
79
67
  }
80
68
 
81
69
  function getCurrentUser() {
@@ -142,6 +130,7 @@ export default function useLocalAuth() {
142
130
  return {
143
131
  authenticate,
144
132
  login,
133
+ setSession,
145
134
  logout,
146
135
  clearCookies,
147
136
  getCurrentUser,
@@ -1,44 +1,10 @@
1
- export function useLocalSetup(type: string, org?: string) {
2
- const userId = computed(() => useCookie("user").value ?? "");
3
-
4
- const { getByUserType } = useMember();
5
-
6
- const { data: userMemberData, error: userMemberError } = useLazyAsyncData(
7
- "get-member-by-id",
8
- () => getByUserType(userId.value, type, org),
9
- { watch: [userId] }
10
- );
11
-
12
- watchEffect(() => {
13
- if (userMemberError.value) {
14
- navigateTo({
15
- name: "index",
16
- });
17
- }
18
- });
19
-
20
- const { getRoleById } = useRole();
21
-
22
- const roleId = computed(() => userMemberData.value?.role ?? "");
23
-
1
+ export function useLocalSetup() {
24
2
  const userAppRole = useState<Record<string, any> | null>(
25
3
  "userAppRole",
26
4
  () => null
27
5
  );
28
6
 
29
- const { data: getRoleByIdReq } = useLazyAsyncData(
30
- "get-role-by-id",
31
- () => getRoleById(roleId.value),
32
- { watch: [roleId], immediate: false }
33
- );
34
-
35
- watchEffect(() => {
36
- if (getRoleByIdReq.value) {
37
- userAppRole.value = getRoleByIdReq.value;
38
- }
39
- });
40
-
41
- const id = computed(() => userMemberData.value?.org ?? "id");
7
+ const id = useState<string | null>("memberShipOrgId", () => null);
42
8
 
43
9
  return {
44
10
  userAppRole,
@@ -265,6 +265,12 @@ export default function useUtils() {
265
265
  return params;
266
266
  }
267
267
 
268
+ function toOrdinal(n: number): string {
269
+ const s = ["th", "st", "nd", "rd"];
270
+ const v = n % 100;
271
+ return n + (s[(v - 20) % 10] || s[v] || s[0]);
272
+ }
273
+
268
274
  return {
269
275
  requiredRule,
270
276
  emailRule,
@@ -290,5 +296,6 @@ export default function useUtils() {
290
296
  getRouteParam,
291
297
  replaceMatch,
292
298
  setRouteParams,
299
+ toOrdinal,
293
300
  };
294
301
  }
@@ -4,10 +4,9 @@ export default defineNuxtRouteMiddleware(async () => {
4
4
 
5
5
  const { cookieConfig } = useRuntimeConfig().public;
6
6
 
7
- // Get access token from cookies
8
- const accessToken = useCookie("accessToken", cookieConfig).value;
7
+ const sid = useCookie("sid", cookieConfig).value;
9
8
 
10
- if (!accessToken) {
9
+ if (!sid) {
11
10
  // Redirect to login page if no access token
12
11
  return navigateTo({ name: "index" });
13
12
  }
package/nuxt.config.ts CHANGED
@@ -15,6 +15,7 @@ export default defineNuxtConfig({
15
15
  secure: true,
16
16
  maxAge: 30 * 24 * 60 * 60,
17
17
  },
18
+ APP: (process.env.APP as string) ?? "App",
18
19
  APP_NAME: (process.env.APP_NAME as string) ?? "App",
19
20
  APP_NAME_ROUTE: (process.env.APP_NAME_ROUTE as string) ?? "index",
20
21
  S3_BUCKET_ENDPOINT: (process.env.S3_BUCKET_ENDPOINT as string) ?? "",
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@eeplatform/nuxt-layer-common",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "1.2.3",
5
+ "version": "1.2.5",
6
6
  "main": "./nuxt.config.ts",
7
7
  "publishConfig": {
8
8
  "access": "public"
package/plugins/API.ts CHANGED
@@ -1,6 +1,3 @@
1
- import { useFetch } from "nuxt/app";
2
- type useFetchType = typeof useFetch;
3
-
4
1
  export default defineNuxtPlugin(() => {
5
2
  const { cookieConfig, BASE_URL } = useRuntimeConfig().public;
6
3
 
@@ -10,49 +7,15 @@ export default defineNuxtPlugin(() => {
10
7
  retryStatusCodes: [401],
11
8
  retryDelay: 500,
12
9
  onRequest({ options }) {
13
- const accessToken = useCookie("accessToken", cookieConfig).value;
14
- options.headers.set("Authorization", `Bearer ${accessToken}`);
15
- },
16
-
17
- async onResponseError({ response }) {
18
- if (response.status === 401) {
19
- await $fetch("/api/auth/refresh", {
20
- method: "POST",
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
 
36
- const useAPI: useFetchType = (path, options = {}) => {
37
- options.lazy = true;
38
- options.retry = 1;
39
- options.retryStatusCodes = [401];
40
- options.retryDelay = 500;
41
- options.baseURL = (BASE_URL as string) ?? "/";
42
-
43
- options.headers = {
44
- ...options.headers,
45
- Authorization: `Bearer ${useCookie("accessToken", cookieConfig).value}`,
46
- };
47
-
48
- return useFetch(path, options);
49
- };
50
-
51
15
  // Expose to useNuxtApp().$api
52
16
  return {
53
17
  provide: {
54
18
  api,
55
- useAPI,
56
19
  },
57
20
  };
58
21
  });
@@ -0,0 +1,51 @@
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 isSecured = to.meta?.secured;
10
+ if (!isSecured) 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
+ name: "index",
27
+ });
28
+ }
29
+ });
30
+
31
+ watchEffect(() => {
32
+ if (userMemberData.value) {
33
+ id.value = userMemberData.value.org ?? "";
34
+ }
35
+ });
36
+
37
+ const roleId = computed(() => userMemberData.value?.role ?? "");
38
+
39
+ const { data: getRoleByIdReq } = useLazyAsyncData(
40
+ "get-role-by-id",
41
+ () => getRoleById(roleId.value),
42
+ { watch: [roleId], immediate: false }
43
+ );
44
+
45
+ watchEffect(() => {
46
+ if (getRoleByIdReq.value) {
47
+ userAppRole.value = getRoleByIdReq.value;
48
+ }
49
+ });
50
+ });
51
+ });