@iservice365/layer-common 0.2.0 → 0.2.2
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 +12 -0
- package/composables/useLocalAuth.ts +4 -14
- package/composables/useLocalSetup.ts +6 -45
- package/composables/useMember.ts +8 -1
- package/middleware/01.auth.ts +2 -2
- package/middleware/member.ts +4 -0
- package/nuxt.config.ts +1 -0
- package/package.json +1 -1
- package/plugins/API.ts +2 -23
- package/plugins/secure-member.client.ts +54 -0
- package/types/local.d.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @iservice365/layer-common
|
|
2
2
|
|
|
3
|
+
## 0.2.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- c627e9b: Switch to session based auth
|
|
8
|
+
|
|
9
|
+
## 0.2.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- ff09e54: Breaking change - switched to session based authentication
|
|
14
|
+
|
|
3
15
|
## 0.2.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
|
@@ -4,14 +4,6 @@ export default function useLocalAuth() {
|
|
|
4
4
|
const currentUser = useState<TUser | null>("currentUser", () => null);
|
|
5
5
|
|
|
6
6
|
function authenticate() {
|
|
7
|
-
// Get access token from cookies
|
|
8
|
-
const accessToken = useCookie("accessToken", cookieConfig).value;
|
|
9
|
-
|
|
10
|
-
if (!accessToken) {
|
|
11
|
-
// Redirect to login page if no access token
|
|
12
|
-
navigateTo({ name: "index" });
|
|
13
|
-
}
|
|
14
|
-
|
|
15
7
|
const user = useCookie("user", cookieConfig).value;
|
|
16
8
|
|
|
17
9
|
const { data: getCurrentUserReq, error: getCurrentUserErr } =
|
|
@@ -40,15 +32,13 @@ export default function useLocalAuth() {
|
|
|
40
32
|
});
|
|
41
33
|
}
|
|
42
34
|
|
|
43
|
-
function
|
|
44
|
-
useCookie("
|
|
45
|
-
useCookie("refreshToken", cookieConfig).value = refreshToken;
|
|
35
|
+
function setSession({ sid = "", user = "" }) {
|
|
36
|
+
useCookie("sid", cookieConfig).value = sid;
|
|
46
37
|
useCookie("user", cookieConfig).value = user;
|
|
47
38
|
}
|
|
48
39
|
|
|
49
40
|
function clearCookies() {
|
|
50
|
-
useCookie("
|
|
51
|
-
useCookie("refreshToken", cookieConfig).value = null;
|
|
41
|
+
useCookie("sid", cookieConfig).value = null;
|
|
52
42
|
useCookie("user", cookieConfig).value = null;
|
|
53
43
|
useCookie("organization", cookieConfig).value = null;
|
|
54
44
|
}
|
|
@@ -134,10 +124,10 @@ export default function useLocalAuth() {
|
|
|
134
124
|
return {
|
|
135
125
|
authenticate,
|
|
136
126
|
login,
|
|
127
|
+
setSession,
|
|
137
128
|
logout,
|
|
138
129
|
clearCookies,
|
|
139
130
|
getCurrentUser,
|
|
140
|
-
setToken,
|
|
141
131
|
forgotPassword,
|
|
142
132
|
resetPassword,
|
|
143
133
|
currentUser,
|
|
@@ -1,52 +1,13 @@
|
|
|
1
|
-
export function useLocalSetup(
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
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
|
|
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
|
-
|
|
11
|
+
id,
|
|
51
12
|
};
|
|
52
13
|
}
|
package/composables/useMember.ts
CHANGED
|
@@ -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,
|
package/middleware/01.auth.ts
CHANGED
|
@@ -2,9 +2,9 @@ export default defineNuxtRouteMiddleware(async () => {
|
|
|
2
2
|
const { cookieConfig } = useRuntimeConfig().public;
|
|
3
3
|
|
|
4
4
|
// Get access token from cookies
|
|
5
|
-
const
|
|
5
|
+
const sid = useCookie("sid", cookieConfig).value;
|
|
6
6
|
|
|
7
|
-
if (!
|
|
7
|
+
if (!sid) {
|
|
8
8
|
// Redirect to login page if no access token
|
|
9
9
|
return navigateTo({ name: "index" });
|
|
10
10
|
}
|
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
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
|
|
11
|
-
options.headers.set("Authorization",
|
|
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
|
+
});
|