@goweekdays/layer-common 1.2.2 → 1.2.3
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 +6 -0
- package/composables/useLocalAuth.ts +14 -2
- package/middleware/org.ts +3 -6
- package/package.json +2 -2
- package/plugins/member.client.ts +66 -0
- package/plugins/secure.client.ts +34 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@ export default function useLocalAuth() {
|
|
|
2
2
|
const { cookieConfig } = useRuntimeConfig().public;
|
|
3
3
|
|
|
4
4
|
const currentUser = useState<TUser | null>("currentUser", () => null);
|
|
5
|
+
const membership = useState<TMember | null>("membership", () => null);
|
|
6
|
+
const permissions = useState<string[]>("permissions", () => []);
|
|
7
|
+
|
|
8
|
+
const hasPermission = (permissionKey: string): ComputedRef<boolean> => {
|
|
9
|
+
return computed(() => {
|
|
10
|
+
if (!permissions.value.length) return false;
|
|
11
|
+
return permissions.value.some((perm) => perm === permissionKey);
|
|
12
|
+
});
|
|
13
|
+
};
|
|
5
14
|
|
|
6
15
|
function authenticate() {
|
|
7
16
|
if (currentUser.value) {
|
|
@@ -30,7 +39,7 @@ export default function useLocalAuth() {
|
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
async function login({ email = "", password = "", role = "" }) {
|
|
33
|
-
return useNuxtApp().$api<
|
|
42
|
+
return useNuxtApp().$api<Record<string, any>>("/api/auth/login", {
|
|
34
43
|
method: "POST",
|
|
35
44
|
body: JSON.stringify({ email, password, role }),
|
|
36
45
|
});
|
|
@@ -106,13 +115,16 @@ export default function useLocalAuth() {
|
|
|
106
115
|
}
|
|
107
116
|
|
|
108
117
|
return {
|
|
118
|
+
currentUser,
|
|
119
|
+
membership,
|
|
120
|
+
permissions,
|
|
121
|
+
hasPermission,
|
|
109
122
|
authenticate,
|
|
110
123
|
login,
|
|
111
124
|
logout,
|
|
112
125
|
getCurrentUser,
|
|
113
126
|
forgotPassword,
|
|
114
127
|
resetPassword,
|
|
115
|
-
currentUser,
|
|
116
128
|
verify,
|
|
117
129
|
signUp,
|
|
118
130
|
};
|
package/middleware/org.ts
CHANGED
|
@@ -5,12 +5,9 @@ const hexSchema = z
|
|
|
5
5
|
.regex(/^[0-9a-fA-F]{24}$/, "Invalid organization ID");
|
|
6
6
|
|
|
7
7
|
export default defineNuxtRouteMiddleware((to) => {
|
|
8
|
-
const
|
|
8
|
+
const org = (to.params.org as string) ?? "";
|
|
9
9
|
|
|
10
|
-
if (
|
|
11
|
-
return navigateTo(
|
|
12
|
-
{ name: "require-organization-membership" },
|
|
13
|
-
{ replace: true }
|
|
14
|
-
);
|
|
10
|
+
if (org && !hexSchema.safeParse(org).success) {
|
|
11
|
+
return navigateTo({ name: "require-organization" }, { replace: true });
|
|
15
12
|
}
|
|
16
13
|
});
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@goweekdays/layer-common",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "1.2.
|
|
5
|
+
"version": "1.2.3",
|
|
6
6
|
"main": "./nuxt.config.ts",
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"access": "public"
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"@changesets/cli": "^2.27.9",
|
|
20
20
|
"@nuxt/eslint": "latest",
|
|
21
21
|
"eslint": "^9.14.0",
|
|
22
|
-
"nuxt": "^3.
|
|
22
|
+
"nuxt": "^3.19.2",
|
|
23
23
|
"typescript": "^5.6.3",
|
|
24
24
|
"vite-plugin-vuetify": "^2.0.4",
|
|
25
25
|
"vue": "latest",
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export default defineNuxtPlugin(() => {
|
|
2
|
+
const router = useRouter();
|
|
3
|
+
const { getByUserType } = useMember();
|
|
4
|
+
const { getRoleById } = useRole();
|
|
5
|
+
const { membership, permissions } = useLocalAuth();
|
|
6
|
+
|
|
7
|
+
router.afterEach((to) => {
|
|
8
|
+
const isSecured = to.meta?.member;
|
|
9
|
+
if (!isSecured) return;
|
|
10
|
+
|
|
11
|
+
const APP = useRuntimeConfig().public.APP;
|
|
12
|
+
const org = (to.params.org as string) ?? "";
|
|
13
|
+
|
|
14
|
+
const userId = useCookie("user").value ?? "";
|
|
15
|
+
|
|
16
|
+
const {
|
|
17
|
+
data: userMemberData,
|
|
18
|
+
error: userMemberError,
|
|
19
|
+
refresh: refreshUserMemberData,
|
|
20
|
+
} = useLazyAsyncData(
|
|
21
|
+
"plugin-get-member-by-id" + userId,
|
|
22
|
+
() => getByUserType(userId, APP, org),
|
|
23
|
+
{ immediate: false }
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
if (!membership.value) {
|
|
27
|
+
refreshUserMemberData();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
watchEffect(() => {
|
|
31
|
+
if (userMemberError.value) {
|
|
32
|
+
navigateTo({
|
|
33
|
+
name: "index",
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
watchEffect(() => {
|
|
39
|
+
if (userMemberData.value) {
|
|
40
|
+
membership.value = userMemberData.value;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const roleId = computed(() => membership.value?.role ?? "");
|
|
45
|
+
|
|
46
|
+
const { data: roleData, refresh: refreshRoleData } = useLazyAsyncData(
|
|
47
|
+
"plugin-get-role-by-id" + roleId.value,
|
|
48
|
+
() => getRoleById(roleId.value),
|
|
49
|
+
{ immediate: false }
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
watchEffect(() => {
|
|
53
|
+
if (roleId.value) {
|
|
54
|
+
if (!permissions.value.length) {
|
|
55
|
+
refreshRoleData();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
watchEffect(() => {
|
|
61
|
+
if (roleData.value) {
|
|
62
|
+
permissions.value = roleData.value.permissions ?? [];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export default defineNuxtPlugin(() => {
|
|
2
|
+
const router = useRouter();
|
|
3
|
+
const { cookieConfig } = useRuntimeConfig().public;
|
|
4
|
+
const { currentUser } = useLocalAuth();
|
|
5
|
+
|
|
6
|
+
router.afterEach((to) => {
|
|
7
|
+
const isSecured = to.meta?.secured;
|
|
8
|
+
if (!isSecured) return;
|
|
9
|
+
|
|
10
|
+
const user = useCookie("user", cookieConfig).value;
|
|
11
|
+
|
|
12
|
+
if (currentUser.value) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const { data: getCurrentUserReq, error: getCurrentUserErr } =
|
|
17
|
+
useLazyAsyncData("get-current-user", () =>
|
|
18
|
+
useNuxtApp().$api<TUser>(`/api/users/id/${user}`)
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
watchEffect(() => {
|
|
22
|
+
if (getCurrentUserReq.value) {
|
|
23
|
+
currentUser.value = getCurrentUserReq.value;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
watchEffect(() => {
|
|
28
|
+
if (getCurrentUserErr.value) {
|
|
29
|
+
// Redirect to login page if user authentication fails
|
|
30
|
+
navigateTo({ name: "index" });
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
});
|