From 897e41061fbd418a2739b5fc384b7ef39f821781 Mon Sep 17 00:00:00 2001 From: Kabin Date: Mon, 9 Dec 2024 14:45:44 +0545 Subject: [PATCH] feat: redirect to home page on basis of role --- packages/vue-user/src/store.ts | 2 +- packages/vue-user/src/supertokens/helper.ts | 4 +- packages/vue-user/src/views/Login.vue | 45 +++++++++++---------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/packages/vue-user/src/store.ts b/packages/vue-user/src/store.ts index a05dc9d0b..a333f8f0a 100644 --- a/packages/vue-user/src/store.ts +++ b/packages/vue-user/src/store.ts @@ -39,7 +39,7 @@ const useUserStore = defineStore("user", () => { const login = async (credentials: LoginCredentials) => { const response = await doLogin(credentials); - setUser(response); + return response; }; const logout = async () => { diff --git a/packages/vue-user/src/supertokens/helper.ts b/packages/vue-user/src/supertokens/helper.ts index 5aa0296ac..591b93162 100644 --- a/packages/vue-user/src/supertokens/helper.ts +++ b/packages/vue-user/src/supertokens/helper.ts @@ -1,7 +1,6 @@ import Session from "supertokens-web-js/recipe/session"; import { UserRoleClaim } from "supertokens-web-js/recipe/userroles"; -import logout from "./logout"; import useUserStore from "../store"; export async function verifySessionRoles(claims: string[]): Promise { @@ -37,9 +36,8 @@ export async function verifySessionRoles(claims: string[]): Promise { } else { const userStore = useUserStore(); - const { removeUser } = userStore; + const { logout } = userStore; // all user roles claim check failed - removeUser(); await logout(); } } diff --git a/packages/vue-user/src/views/Login.vue b/packages/vue-user/src/views/Login.vue index 19127737b..86052786b 100644 --- a/packages/vue-user/src/views/Login.vue +++ b/packages/vue-user/src/views/Login.vue @@ -38,7 +38,6 @@ export default { import { useConfig } from "@dzangolab/vue3-config"; import { useI18n } from "@dzangolab/vue3-i18n"; import { Errors, Page } from "@dzangolab/vue3-ui"; -import { storeToRefs } from "pinia"; import { ref } from "vue"; import { useRouter } from "vue-router"; @@ -60,8 +59,7 @@ const messages = useTranslations(); const { t } = useI18n({ messages }); const userStore = useUserStore(); -const { login } = userStore; -const { user } = storeToRefs(userStore); +const { login, setUser } = userStore; const router = useRouter(); @@ -72,24 +70,29 @@ const loading = ref(false); const handleSubmit = async (credentials: LoginCredentials) => { loading.value = true; - await login(credentials).catch((error) => { - errors.value = [ - { - code: error.message, - message: t(`user.login.errors.${error.message}`), - }, - ]; - }); - - if (user.value) { - if ( - config && - config.user?.supportedRoles && - (await verifySessionRoles(config.user?.supportedRoles)) - ) { - router.push({ name: "home" }); - } - } + await login(credentials) + .then(async (response) => { + if (response) { + const supportedRoles = config?.user?.supportedRoles; + + if ( + (supportedRoles && (await verifySessionRoles(supportedRoles))) || + !supportedRoles?.length + ) { + setUser(response); + + router.push({ name: "home" }); + } + } + }) + .catch((error) => { + errors.value = [ + { + code: error.message, + message: t(`user.login.errors.${error.message}`), + }, + ]; + }); loading.value = false; };