Skip to content

Commit

Permalink
feat: redirect to home page on basis of role
Browse files Browse the repository at this point in the history
  • Loading branch information
KabinKhandThakuri committed Dec 9, 2024
1 parent f48294a commit 897e410
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/vue-user/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/vue-user/src/supertokens/helper.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
Expand Down Expand Up @@ -37,9 +36,8 @@ export async function verifySessionRoles(claims: string[]): Promise<boolean> {
} else {
const userStore = useUserStore();

const { removeUser } = userStore;
const { logout } = userStore;
// all user roles claim check failed
removeUser();
await logout();
}
}
Expand Down
45 changes: 24 additions & 21 deletions packages/vue-user/src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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();
Expand All @@ -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;
};
Expand Down

0 comments on commit 897e410

Please sign in to comment.