Skip to content

Commit f1be180

Browse files
authored
Merge pull request #40 from sajanv88/bug_fix_tenant_id_login
Issue(39): Fixed login with tenant
2 parents 62e2999 + 4e4ab43 commit f1be180

File tree

8 files changed

+70
-25
lines changed

8 files changed

+70
-25
lines changed

components/admin/AdminHeader.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ const fullName = computed(() => {
3434
return config.value?.currentUser?.userName;
3535
});
3636
37+
const showTenant = computed(() => {
38+
if (config.value?.currentTenant?.id) {
39+
return config.value?.currentTenant?.name + "\\";
40+
}
41+
return "";
42+
});
43+
3744
const onMenuClickEvent = () => {
3845
navStore.toggleSideNavbar();
3946
emit("toggleNav");
@@ -57,7 +64,7 @@ const onMenuClickEvent = () => {
5764
<h1
5865
class="hidden md:block text-lg tracking-tight font-semibold capitalize md:text-xl lg:text-2xl"
5966
>
60-
{{ fullName }}
67+
{{ showTenant }}{{ fullName }}
6168
</h1>
6269
<IconButton @click="onMenuClickEvent" classname="inline md:hidden">
6370
<Icon icon="menu" />

middleware/router.global.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ const navCallback = (nav: INavigation): string | string[] => {
88
return nav.link;
99
};
1010
let validPaths = navList.map(navCallback).filter(Boolean).flat(2);
11-
validPaths = [...validPaths, "/admin/profile", "/error/notfound"];
1211

12+
validPaths = [...validPaths, "/admin/profile", "/error/notfound"];
13+
console.log(...validPaths, "validPaths");
1314
export default defineNuxtRouteMiddleware((to) => {
1415
if (to.fullPath === "/") {
1516
return;
1617
}
18+
1719
if (!validPaths.includes(to.fullPath)) {
1820
return navigateTo("/error/notfound");
1921
}

server/api/abpServiceProxy/[...].ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { joinURL } from "ufo";
22

33
export default defineEventHandler(async (event) => {
44
const config = useRuntimeConfig();
5-
const session = await getSession(event, { password: config.sessionSecret });
6-
const { data } = session;
5+
const authSessions = getCookie(event, "auth_session");
6+
if(!authSessions) {
7+
throw new Error("Unauthorized: Please login.");
8+
}
9+
const auth = JSON.parse(authSessions);
710

8-
if (!data.tokenSet) {
11+
if (!auth.tokenSet) {
912
setResponseStatus(event, 401, "Unauthorized");
1013
return {
1114
status: 401,
@@ -18,7 +21,7 @@ export default defineEventHandler(async (event) => {
1821

1922
const verificationToken = getCookie(event, "XSRF-TOKEN");
2023
const headers = new Headers();
21-
headers.set("Authorization", `Bearer ${data.tokenSet.access_token}`);
24+
headers.set("Authorization", `Bearer ${auth.tokenSet.access_token}`);
2225
if (verificationToken) {
2326
headers.set("RequestVerificationToken", verificationToken);
2427
}
@@ -57,6 +60,6 @@ export default defineEventHandler(async (event) => {
5760
};
5861
}
5962

60-
const jsonData = await response.json();
61-
return jsonData;
63+
return await response.json();
64+
6265
});

server/api/auth/[...].ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,24 @@ export default defineEventHandler(async (event) => {
6161
},
6262
);
6363
const claims = tokenSet.claims();
64-
await updateSession(
64+
const sessions = await useSession(
6565
event,
66-
{ password: config.sessionSecret },
67-
{
68-
tokenSet: tokenSet,
69-
user: claims,
70-
},
66+
{ password: config.sessionSecret }
7167
);
72-
68+
await sessions.update({
69+
tokenSet: tokenSet,
70+
user: claims,
71+
});
72+
setCookie(event, "auth_session", JSON.stringify({tokenSet: tokenSet, user: claims}), {
73+
httpOnly: true,
74+
secure: process.env.NODE_ENV === "production",
75+
});
76+
if(claims.tenantid) {
77+
setCookie(event, "__tenant", JSON.stringify(claims.tenantid), {
78+
httpOnly: true,
79+
secure: process.env.NODE_ENV === "production",
80+
});
81+
}
7382
await sendRedirect(event, "/admin");
7483
} catch (e: unknown) {
7584
if (e instanceof Error) {
@@ -80,13 +89,17 @@ export default defineEventHandler(async (event) => {
8089
} else if (event.path == "/api/auth/signup") {
8190
await sendRedirect(event, `${config.openiddict.issuer}/Account/Register`);
8291
} else if (event.path == "/api/auth/signout") {
83-
await getSession(event, { password: config.sessionSecret });
8492
const logoutUrl = client.endSessionUrl({
8593
client_id: config.openiddict.clientId,
94+
post_logout_redirect_uri: config.openiddict.postLogoutRedirectUrl,
8695
});
87-
await sendRedirect(event, logoutUrl);
96+
console.log("Logging out: ", logoutUrl);
97+
deleteCookie(event, "auth_session");
98+
await sendRedirect(event, "/");
99+
88100
} else if (event.path == `/${config.openiddict.postLogoutRedirectUrl}`) {
89-
await clearSession(event, { password: config.sessionSecret });
101+
// Todo: check and remove this
102+
deleteCookie(event, "auth_session");
90103
await sendRedirect(event, "/");
91104
} else {
92105
await sendRedirect(event, "/error/notfound", 404);

server/api/jwt.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
export default defineEventHandler(async (event) => {
2-
const config = useRuntimeConfig();
3-
const session = await getSession(event, { password: config.sessionSecret });
2+
const authSessions = getCookie(event, "auth_session");
3+
if(!authSessions) {
4+
throw new Error("Unauthorized: Please login.");
5+
}
6+
const auth = JSON.parse(authSessions);
47

5-
if (Object.keys(session.data).length === 0) {
8+
if (!auth || !auth.tokenSet.access_token) {
69
setResponseStatus(event, 401, "Unauthorized");
710
return {
811
status: 401,
912
message: "Unauthorized: Please login.",
1013
};
1114
}
1215

13-
const tokenSet = session.data.tokenSet;
16+
const tokenSet = auth.tokenSet;
1417
return { jwt: tokenSet };
1518
});

server/api/user.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
export default defineEventHandler(async (event) => {
22
const config = useRuntimeConfig();
3-
const session = await getSession(event, { password: config.sessionSecret });
3+
const authSessions = getCookie(event, "auth_session");
4+
if(!authSessions) {
5+
throw new Error("Unauthorized: Please login.");
6+
}
7+
const auth = JSON.parse(authSessions);
8+
const token = auth.tokenSet.access_token;
9+
410
const abpUrl = config.abpApiEndpoint + "/account/my-profile";
511

6-
if (Object.keys(session.data).length === 0) {
12+
13+
if (!auth || !token) {
714
setResponseStatus(event, 401, "Unauthorized");
815

916
return {
1017
status: 401,
1118
message: "Unauthorized: Please login.",
1219
};
1320
}
21+
1422
const profile = await fetch(abpUrl, {
1523
headers: {
16-
Authorization: `Bearer ${session.data.tokenSet.access_token}`,
24+
Authorization: `Bearer ${token}`
1725
},
1826
});
1927

store/emailingStore.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ export const useEmailing = defineStore("emailing", {
5252
statusCode: error.value.statusCode ?? 500,
5353
message: error.value.message,
5454
};
55+
const toast = useToast();
56+
toast.show({
57+
message: "Error fetching email settings",
58+
type: "destructive",
59+
show: true,
60+
autoClose: true,
61+
dismissible: true,
62+
});
5563
this.isLoading = false;
5664
throw error.value;
5765
}

store/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const useAbpConfiguration = defineStore("abpConfiguration", {
7171
async fetch() {
7272
const url = `${getAbpServiceProxy("/abp/application-configuration")}`;
7373
const { data, error } = await useFetch(url);
74+
console.log(data, "abp configuration response");
7475

7576
if (error.value) {
7677
this.error = {

0 commit comments

Comments
 (0)