auth: dodano guard ścieżek

This commit is contained in:
2023-01-31 17:13:34 +01:00
parent 6288fbbde1
commit 2cb8b1dc15
5 changed files with 47 additions and 24 deletions
+11 -21
View File
@@ -5,38 +5,28 @@
</template>
<script lang="ts">
import { computed, defineComponent, watch } from 'vue';
import { defineComponent } from 'vue';
import dataMixin from './mixins/dataMixin';
import { useStore } from './store';
import PopUpCard from './components/PopUpCard.vue';
import axios, { AxiosResponse } from 'axios';
import { RouterView, useRouter } from 'vue-router';
import { ILoginResponse, IUser, AuthState } from './types/types';
import axios from 'axios';
import { RouterView } from 'vue-router';
import { IUser, AuthState } from './types/types';
import useRouteGuard from './mixins/useRouteGuard';
import { useStore } from './store';
export default defineComponent({
mixins: [dataMixin],
components: { PopUpCard },
setup() {
const store = useStore();
const router = useRouter();
router.beforeEach(async (to, from, next) => {
if (store.authState == AuthState.AUTHORIZED && to.path == '/login') return next({ path: '/' });
return next();
});
watch(
computed(() => store.authState),
(state) => {
if (router.currentRoute.value.path == '/login' && state == AuthState.AUTHORIZED) router.push('/');
}
);
const { routeAuthGuard } = useRouteGuard();
routeAuthGuard();
return {
store,
store: useStore(),
};
},
methods: {
async autoLogin() {
const token = window.localStorage.getItem('auth-token');