mirror of
https://github.com/Spythere/station-manager-2.0.git
synced 2026-05-03 13:38:13 +00:00
update: auth & update modal
This commit is contained in:
+49
-8
@@ -5,16 +5,34 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { defineComponent } from 'vue';
|
import { computed, defineComponent, watch } from 'vue';
|
||||||
import dataMixin from './mixins/dataMixin';
|
import dataMixin from './mixins/dataMixin';
|
||||||
import { useStore } from './store';
|
import { useStore } from './store';
|
||||||
import PopUpCard from './components/PopUpCard.vue';
|
import PopUpCard from './components/PopUpCard.vue';
|
||||||
|
import axios, { AxiosResponse } from 'axios';
|
||||||
|
import { RouterView, useRouter } from 'vue-router';
|
||||||
|
import { ILoginResponse, IUser, AuthState } from './types/types';
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
mixins: [dataMixin],
|
mixins: [dataMixin],
|
||||||
components: { PopUpCard },
|
components: { PopUpCard },
|
||||||
setup() {
|
setup() {
|
||||||
const store = useStore();
|
const store = useStore();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
router.beforeEach(async (to, from, next) => {
|
||||||
|
if (store.authState != AuthState.AUTHORIZED && to.path != '/login') return next({ path: '/login' });
|
||||||
|
|
||||||
|
return next();
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(
|
||||||
|
computed(() => store.authState),
|
||||||
|
(state) => {
|
||||||
|
if (router.currentRoute.value.path == '/login' && state == AuthState.AUTHORIZED) router.push('/');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
store,
|
store,
|
||||||
};
|
};
|
||||||
@@ -22,15 +40,36 @@ export default defineComponent({
|
|||||||
methods: {
|
methods: {
|
||||||
async autoLogin() {
|
async autoLogin() {
|
||||||
const token = window.localStorage.getItem('auth-token');
|
const token = window.localStorage.getItem('auth-token');
|
||||||
if (token) {
|
if (!token) {
|
||||||
this.store.isAuthorized = true;
|
this.store.authState = AuthState.UNAUTHORIZED;
|
||||||
this.store.token = token;
|
return;
|
||||||
this.store.user = JSON.parse(window.localStorage.getItem('user')!);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.store.token = token;
|
||||||
|
|
||||||
|
const data = axios.post<{ user: IUser }>(`${this.API_URL}/auth/token`, { token: this.store.token });
|
||||||
|
|
||||||
|
data
|
||||||
|
.then((res) => {
|
||||||
|
console.log(res.data);
|
||||||
|
|
||||||
|
this.store.user = res.data.user;
|
||||||
|
this.store.authState = AuthState.AUTHORIZED;
|
||||||
|
this.$router.push('/');
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
this.store.isAuthorized = false;
|
||||||
|
window.localStorage.removeItem('auth-token');
|
||||||
|
|
||||||
|
this.store.authState = AuthState.UNAUTHORIZED;
|
||||||
|
this.$router.push('/login');
|
||||||
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
this.autoLogin();
|
this.autoLogin();
|
||||||
|
|
||||||
if (window.localStorage.getItem('notifyDiscord') !== null) {
|
if (window.localStorage.getItem('notifyDiscord') !== null) {
|
||||||
this.store.notifyDiscord = Boolean(Number(window.localStorage.getItem('notifyDiscord')));
|
this.store.notifyDiscord = Boolean(Number(window.localStorage.getItem('notifyDiscord')));
|
||||||
}
|
}
|
||||||
@@ -97,13 +136,15 @@ button:focus-visible {
|
|||||||
transform: translate(-50%, -50%);
|
transform: translate(-50%, -50%);
|
||||||
|
|
||||||
width: 90vw;
|
width: 90vw;
|
||||||
max-width: 350px;
|
max-width: 550px;
|
||||||
|
|
||||||
padding: 0.5em 1em;
|
padding: 0.5em 1em;
|
||||||
margin-top: 1em;
|
margin-top: 1em;
|
||||||
|
|
||||||
background-color: #2e2e2e;
|
border-radius: 1em;
|
||||||
box-shadow: 0 0 6px 1px #131313;
|
|
||||||
|
background-color: #1d1c33;
|
||||||
|
box-shadow: 0 0 6px 1px #414141;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scrollbar
|
// Scrollbar
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ import { defineComponent } from 'vue';
|
|||||||
import dataMixin from '../mixins/dataMixin';
|
import dataMixin from '../mixins/dataMixin';
|
||||||
import routesMixin from '../mixins/routesMixin';
|
import routesMixin from '../mixins/routesMixin';
|
||||||
import { useStore } from '../store';
|
import { useStore } from '../store';
|
||||||
import { Availability, ChangeProp, HeaderTypes, SceneryRowItem } from '../types/types';
|
import { AuthState, Availability, ChangeProp, HeaderTypes, SceneryRowItem } from '../types/types';
|
||||||
import { getAvailabilityValue } from '../types/typeUitls';
|
import { getAvailabilityValue } from '../types/typeUitls';
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
@@ -146,7 +146,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
signOut() {
|
signOut() {
|
||||||
this.store.token = null;
|
this.store.token = null;
|
||||||
this.store.isAuthorized = false;
|
this.store.authState = AuthState.UNAUTHORIZED;
|
||||||
|
|
||||||
window.localStorage.removeItem('auth-token');
|
window.localStorage.removeItem('auth-token');
|
||||||
window.localStorage.removeItem('user');
|
window.localStorage.removeItem('user');
|
||||||
@@ -184,7 +184,6 @@ export default defineComponent({
|
|||||||
);
|
);
|
||||||
|
|
||||||
this.store.changesResponse = response.data;
|
this.store.changesResponse = response.data;
|
||||||
alert('Pomyślnie wprowadzono zmiany!');
|
|
||||||
|
|
||||||
this.loadData();
|
this.loadData();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
<template>
|
||||||
|
<div class="bg-dimmer" @click="closeCard"></div>
|
||||||
|
<div class="g-card popup-card">
|
||||||
|
<div class="card_content">
|
||||||
|
<h1>Raport zmian</h1>
|
||||||
|
|
||||||
|
<p v-for="(ch, i) in changesResponseComp" :key="i" :style="{ color: ch.resolved ? 'lime' : 'crimson' }">
|
||||||
|
{{ ch.resolved ? `✅` : `❌` }} {{ ch.message }}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card_actions">
|
||||||
|
<button @click="closeCard">OK!</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script lang="ts">
|
||||||
|
import { defineComponent } from 'vue';
|
||||||
|
import { useStore } from '../store';
|
||||||
|
|
||||||
|
export default defineComponent({
|
||||||
|
setup() {
|
||||||
|
return {
|
||||||
|
store: useStore(),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
computed: {
|
||||||
|
changesResponseComp() {
|
||||||
|
return this.store.changesResponse.map((ch) => ({
|
||||||
|
resolved: ch.split(' ')[0] == 'v',
|
||||||
|
message: ch.replace(/^[v|x] /, ''),
|
||||||
|
}));
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
methods: {
|
||||||
|
closeCard() {
|
||||||
|
this.store.changesResponse.length = 0;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss" scoped>
|
||||||
|
.bg-dimmer {
|
||||||
|
position: fixed;
|
||||||
|
z-index: 998;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100vw;
|
||||||
|
height: 100vh;
|
||||||
|
|
||||||
|
background-color: #0000004f;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card_actions {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
@@ -18,15 +18,5 @@ const router = createRouter({
|
|||||||
routes,
|
routes,
|
||||||
});
|
});
|
||||||
|
|
||||||
router.beforeEach((to, from, next) => {
|
|
||||||
const token = window.localStorage.getItem('auth-token');
|
|
||||||
|
|
||||||
if (!token && to.path != '/login') return next({ path: '/login' });
|
|
||||||
|
|
||||||
if (token && to.path == '/login') return next({ path: '/' });
|
|
||||||
// else if (to.path == '/login') return next({ path: '/' });
|
|
||||||
|
|
||||||
return next();
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
+4
-1
@@ -1,11 +1,13 @@
|
|||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import { Availability, ChangeProp, HeaderTypes, IStore } from './types/types';
|
import { AuthState, Availability, ChangeProp, HeaderTypes, IStore } from './types/types';
|
||||||
import { getAvailabilityValue } from './types/typeUitls';
|
import { getAvailabilityValue } from './types/typeUitls';
|
||||||
|
|
||||||
export const useStore = defineStore('store', {
|
export const useStore = defineStore('store', {
|
||||||
state: () =>
|
state: () =>
|
||||||
({
|
({
|
||||||
dataState: 'LOADING',
|
dataState: 'LOADING',
|
||||||
|
authState: AuthState.LOADING,
|
||||||
|
|
||||||
unsavedChanges: false,
|
unsavedChanges: false,
|
||||||
stationList: [],
|
stationList: [],
|
||||||
backupList: [],
|
backupList: [],
|
||||||
@@ -20,6 +22,7 @@ export const useStore = defineStore('store', {
|
|||||||
user: null,
|
user: null,
|
||||||
isAuthorized: false,
|
isAuthorized: false,
|
||||||
notifyDiscord: true,
|
notifyDiscord: true,
|
||||||
|
|
||||||
alertMessage: '',
|
alertMessage: '',
|
||||||
confirmMessage: '',
|
confirmMessage: '',
|
||||||
|
|
||||||
|
|||||||
@@ -62,8 +62,16 @@ export type ChangeItem = {
|
|||||||
toRemove?: boolean;
|
toRemove?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export enum AuthState {
|
||||||
|
'LOADING' = 0,
|
||||||
|
'AUTHORIZED' = 1,
|
||||||
|
'UNAUTHORIZED' = 2,
|
||||||
|
}
|
||||||
|
|
||||||
export interface IStore {
|
export interface IStore {
|
||||||
dataState: string;
|
dataState: string;
|
||||||
|
authState: AuthState;
|
||||||
|
|
||||||
unsavedChanges: boolean;
|
unsavedChanges: boolean;
|
||||||
stationList: SceneryRowItem[];
|
stationList: SceneryRowItem[];
|
||||||
backupList: SceneryRowItem[];
|
backupList: SceneryRowItem[];
|
||||||
@@ -83,3 +91,13 @@ export interface IStore {
|
|||||||
|
|
||||||
changesResponse: string[];
|
changesResponse: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ILoginResponse {
|
||||||
|
token: string;
|
||||||
|
user: IUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IUser {
|
||||||
|
name: string;
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|||||||
+18
-12
@@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="login">
|
<div class="login" v-if="store.authState == AuthState.UNAUTHORIZED">
|
||||||
<div class="login-header">
|
<div class="login-header">
|
||||||
<img src="/icon-logo.svg" alt="logo" />
|
<img src="/icon-logo.svg" alt="logo" />
|
||||||
<h1>Stacjownik Station Manager</h1>
|
<h1>Stacjownik Station Manager</h1>
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
<br />
|
<br />
|
||||||
<input type="password" id="password" v-model="password" />
|
<input type="password" id="password" v-model="password" />
|
||||||
<br />
|
<br />
|
||||||
<button>Zaloguj</button>
|
<button>{{ loginState == LoginState.LOADING ? 'Logowanie...' : 'Zaloguj się' }}</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -24,13 +24,12 @@ import axios from 'axios';
|
|||||||
import { defineComponent } from 'vue';
|
import { defineComponent } from 'vue';
|
||||||
import dataMixin from '../mixins/dataMixin';
|
import dataMixin from '../mixins/dataMixin';
|
||||||
import { useStore } from '../store';
|
import { useStore } from '../store';
|
||||||
|
import { AuthState, ILoginResponse } from '../types/types';
|
||||||
|
|
||||||
interface LoginResponse {
|
enum LoginState {
|
||||||
token: string;
|
INITIALIZED = 0,
|
||||||
user: {
|
LOADING = 1,
|
||||||
name: string;
|
LOADED = 2,
|
||||||
id: string;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
@@ -40,18 +39,20 @@ export default defineComponent({
|
|||||||
return {
|
return {
|
||||||
name: '',
|
name: '',
|
||||||
password: '',
|
password: '',
|
||||||
|
loginState: LoginState.INITIALIZED,
|
||||||
|
LoginState,
|
||||||
store: useStore(),
|
store: useStore(),
|
||||||
|
AuthState,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
async signIn(e: Event) {
|
async signIn(e: Event) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
this.loginState = LoginState.LOADING;
|
||||||
console.log(import.meta.env.VITE_API_URL);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data: LoginResponse = (
|
const data: ILoginResponse = (
|
||||||
await axios.post(
|
await axios.post(
|
||||||
`${this.API_URL}/auth/login`,
|
`${this.API_URL}/auth/login`,
|
||||||
{ username: this.name, password: this.password },
|
{ username: this.name, password: this.password },
|
||||||
@@ -63,7 +64,9 @@ export default defineComponent({
|
|||||||
)
|
)
|
||||||
).data;
|
).data;
|
||||||
|
|
||||||
this.store.isAuthorized = true;
|
this.store.authState = AuthState.AUTHORIZED;
|
||||||
|
this.loginState = LoginState.LOADED;
|
||||||
|
|
||||||
this.store.token = data.token;
|
this.store.token = data.token;
|
||||||
this.store.user = data.user;
|
this.store.user = data.user;
|
||||||
|
|
||||||
@@ -73,6 +76,9 @@ export default defineComponent({
|
|||||||
this.loadData();
|
this.loadData();
|
||||||
this.$router.push('/');
|
this.$router.push('/');
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
this.store.authState = AuthState.UNAUTHORIZED;
|
||||||
|
this.loginState = LoginState.LOADED;
|
||||||
|
|
||||||
if (!e.response || e.response.status === undefined) {
|
if (!e.response || e.response.status === undefined) {
|
||||||
this.store.alertMessage = 'Wystąpił błąd podczas łączenia z serwerem!';
|
this.store.alertMessage = 'Wystąpił błąd podczas łączenia z serwerem!';
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="manager">
|
<div class="manager" v-if="store.authState == AuthState.AUTHORIZED">
|
||||||
<RoutesModal v-if="store.currentStation" />
|
<RoutesModal v-if="store.currentStation" />
|
||||||
|
<UpdateCard v-if="store.changesResponse.length > 0" />
|
||||||
|
|
||||||
<hr color="white" />
|
<hr color="white" />
|
||||||
<TableActions />
|
<TableActions />
|
||||||
@@ -59,16 +60,18 @@ import { defineComponent } from 'vue';
|
|||||||
import changeMixin from '../mixins/changeMixin';
|
import changeMixin from '../mixins/changeMixin';
|
||||||
import dataMixin from '../mixins/dataMixin';
|
import dataMixin from '../mixins/dataMixin';
|
||||||
import { useStore } from '../store';
|
import { useStore } from '../store';
|
||||||
import { SceneryRowItem, Availability } from '../types/types';
|
import { SceneryRowItem, Availability, AuthState } from '../types/types';
|
||||||
import RoutesModal from '../components/RoutesModal.vue';
|
import RoutesModal from '../components/RoutesModal.vue';
|
||||||
import TableActions from '../components/TableActions.vue';
|
import TableActions from '../components/TableActions.vue';
|
||||||
import routesMixin from '../mixins/routesMixin';
|
import routesMixin from '../mixins/routesMixin';
|
||||||
|
import UpdateCard from '../components/UpdateCard.vue';
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
components: { RoutesModal, TableActions },
|
components: { RoutesModal, TableActions, UpdateCard },
|
||||||
mixins: [dataMixin, changeMixin, routesMixin],
|
mixins: [dataMixin, changeMixin, routesMixin],
|
||||||
|
|
||||||
data: () => ({
|
data: () => ({
|
||||||
|
AuthState,
|
||||||
headerNameList: {
|
headerNameList: {
|
||||||
name: 'Nazwa',
|
name: 'Nazwa',
|
||||||
url: 'URL',
|
url: 'URL',
|
||||||
|
|||||||
Reference in New Issue
Block a user