mirror of
https://github.com/Spythere/station-manager-2.0.git
synced 2026-05-04 14:08:14 +00:00
auth: dodano guard ścieżek
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { computed, defineComponent, watch } from 'vue';
|
||||
import { useRouter } from 'vue-router';
|
||||
import { useStore } from '../store';
|
||||
import { AuthState } from '../types/types';
|
||||
|
||||
export default () => {
|
||||
const store = useStore();
|
||||
const router = useRouter();
|
||||
|
||||
const routeAuthGuard = () => {
|
||||
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('/');
|
||||
if (state == AuthState.UNAUTHORIZED) router.push('/login');
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
return {
|
||||
routeAuthGuard,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user