mirror of
https://github.com/Spythere/station-manager-2.0.git
synced 2026-05-03 21:48:14 +00:00
auth: dodano guard ścieżek
This commit is contained in:
@@ -1,2 +1,2 @@
|
|||||||
VITE_API_URL="https://spythere.pl"
|
VITE_API_URL="https://spythere.pl"
|
||||||
VITE_API_URL_DEV="http://localhost:3000"
|
VITE_API_URL_DEV="https://spythere.pl"
|
||||||
+11
-21
@@ -5,38 +5,28 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { computed, defineComponent, watch } from 'vue';
|
import { defineComponent } from 'vue';
|
||||||
import dataMixin from './mixins/dataMixin';
|
import dataMixin from './mixins/dataMixin';
|
||||||
import { useStore } from './store';
|
|
||||||
import PopUpCard from './components/PopUpCard.vue';
|
import PopUpCard from './components/PopUpCard.vue';
|
||||||
import axios, { AxiosResponse } from 'axios';
|
import axios from 'axios';
|
||||||
import { RouterView, useRouter } from 'vue-router';
|
import { RouterView } from 'vue-router';
|
||||||
import { ILoginResponse, IUser, AuthState } from './types/types';
|
import { IUser, AuthState } from './types/types';
|
||||||
|
import useRouteGuard from './mixins/useRouteGuard';
|
||||||
|
import { useStore } from './store';
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
mixins: [dataMixin],
|
mixins: [dataMixin],
|
||||||
components: { PopUpCard },
|
components: { PopUpCard },
|
||||||
|
|
||||||
setup() {
|
setup() {
|
||||||
const store = useStore();
|
const { routeAuthGuard } = useRouteGuard();
|
||||||
const router = useRouter();
|
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('/');
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
store,
|
store: useStore(),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
async autoLogin() {
|
async autoLogin() {
|
||||||
const token = window.localStorage.getItem('auth-token');
|
const token = window.localStorage.getItem('auth-token');
|
||||||
|
|||||||
@@ -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,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
+3
-2
@@ -1,6 +1,5 @@
|
|||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import { AuthState, Availability, ChangeProp, HeaderTypes, IStore } from './types/types';
|
import { AuthState, IStore } from './types/types';
|
||||||
import { getAvailabilityValue } from './types/typeUitls';
|
|
||||||
|
|
||||||
export const useStore = defineStore('store', {
|
export const useStore = defineStore('store', {
|
||||||
state: () =>
|
state: () =>
|
||||||
@@ -26,6 +25,8 @@ export const useStore = defineStore('store', {
|
|||||||
alertMessage: '',
|
alertMessage: '',
|
||||||
confirmMessage: '',
|
confirmMessage: '',
|
||||||
|
|
||||||
|
maxVisibleResults: 15,
|
||||||
|
|
||||||
changesResponse: [],
|
changesResponse: [],
|
||||||
} as IStore),
|
} as IStore),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -89,6 +89,8 @@ export interface IStore {
|
|||||||
alertMessage: string;
|
alertMessage: string;
|
||||||
confirmMessage: string;
|
confirmMessage: string;
|
||||||
|
|
||||||
|
maxVisibleResults: number;
|
||||||
|
|
||||||
changesResponse: string[];
|
changesResponse: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user