Skip to content

Commit e0aeae5

Browse files
committed
refactor: simplify route authorization logic and improve key usage in LoginView
1 parent adb9626 commit e0aeae5

2 files changed

Lines changed: 11 additions & 33 deletions

File tree

adminforth/spa/src/router/index.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,11 @@ const router = createRouter({
1515
title: 'Login',
1616
customLayout: true
1717
},
18-
// beforeEnter: async (to, from, next) => {
19-
// if(localStorage.getItem('isAuthorized') === 'true') {
20-
// // check if url has next=... and redirect to it
21-
// console.log('to.query', to.query)
22-
// if (to.query.next) {
23-
// next(to.query.next.toString())
24-
// } else {
25-
// next({name: 'home'});
26-
// }
27-
// } else {
28-
// next()
29-
// }
30-
// }
18+
beforeEnter: (to) => {
19+
if (localStorage.getItem('isAuthorized') === 'true') {
20+
return to.query.next ? to.query.next.toString() : { name: 'home' };
21+
}
22+
}
3123
},
3224
{
3325
path: '/resource/:resourceId',

adminforth/spa/src/views/LoginView.vue

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@
8787
</div>
8888

8989
<component
90-
v-for="c in coreStore?.config?.loginPageInjections?.underInputs || []"
90+
v-for="(c, index) in coreStore?.config?.loginPageInjections?.underInputs || []"
91+
:key="`under-inputs-${index}`"
9192
:is="getCustomComponent(formatComponent(c))"
9293
:meta="formatComponent(c).meta"
9394
@update:disableLoginButton="setDisableLoginButton($event)"
@@ -106,7 +107,8 @@
106107
{{ $t('Login to your account') }}
107108
</Button>
108109
<component
109-
v-for="c in coreStore?.config?.loginPageInjections?.underLoginButton || []"
110+
v-for="(c, index) in coreStore?.config?.loginPageInjections?.underLoginButton || []"
111+
:key="`under-login-button-${index}`"
110112
:is="getCustomComponent(formatComponent(c))"
111113
:meta="formatComponent(c).meta"
112114
@update:disableLoginButton="setDisableLoginButton($event)"
@@ -125,25 +127,21 @@
125127
<script setup lang="ts">
126128
127129
import { getCustomComponent, formatComponent } from '@/utils';
128-
import { onBeforeMount, onMounted, ref, computed } from 'vue';
130+
import { onMounted, ref, computed } from 'vue';
129131
import { useCoreStore } from '@/stores/core';
130132
import { useUserStore } from '@/stores/user';
131133
import { IconEyeSolid, IconEyeSlashSolid } from '@iconify-prerendered/vue-flowbite';
132134
import { callAdminForthApi, loadFile } from '@/utils';
133-
import { useRoute, useRouter } from 'vue-router';
135+
import { useRouter } from 'vue-router';
134136
import { Button, Checkbox, Input } from '@/afcl';
135-
import { useI18n } from 'vue-i18n';
136137
import ErrorMessage from '@/components/ErrorMessage.vue';
137138
138-
const { t } = useI18n();
139-
140139
const passwordInput = ref<InstanceType<typeof Input> | null>(null);
141140
const usernameInput = ref<InstanceType<typeof Input> | null>(null);
142141
const rememberMeValue= ref(false);
143142
const username = ref('');
144143
const password = ref('');
145144
146-
const route = useRoute();
147145
const router = useRouter();
148146
const inProgress = ref<boolean>(false);
149147
const isSuccess = ref<boolean>(false);
@@ -160,18 +158,6 @@ const backgroundPosition = computed(() => {
160158
});
161159
162160
163-
onBeforeMount(() => {
164-
if (localStorage.getItem('isAuthorized') === 'true') {
165-
// if route has next param, redirect
166-
coreStore.fetchMenuAndResource();
167-
if (route.query.next) {
168-
router.push(route.query.next.toString());
169-
} else {
170-
router.push({ name: 'home' });
171-
}
172-
}
173-
})
174-
175161
onMounted(async () => {
176162
coreStore.getLoginFormConfig();
177163
if (coreStore.config?.demoCredentials) {

0 commit comments

Comments
 (0)