From 0f66d00f96b406cb2bf54003aa9745d3db962108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B5=D0=BB=D0=B5=D0=B7=D0=BE=D0=B2=20=D0=9F=D0=B0?= =?UTF-8?q?=D0=B2=D0=B5=D0=BB=20=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D1=8C=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Wed, 2 Apr 2025 12:02:43 +0800 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=8B=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83=20=D0=B0=D1=83?= =?UTF-8?q?=D1=82=D0=B5=D0=BD=D1=82=D0=B8=D1=84=D0=B8=D0=BA=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=BD=D0=B0=20middleware?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/app.js | 20 +--------------- resources/js/middleware/auth.js | 33 +++++++++++++++++++++++++++ resources/js/router.js | 25 ++++++++++++++++++++ resources/publicWishlist.js | 8 +------ resources/store/user.js | 10 +++++--- resources/views/Auth/Login.vue | 3 --- resources/views/Auth/Registration.vue | 1 - resources/views/Welcome.vue | 28 +++++++++++++++-------- 8 files changed, 85 insertions(+), 43 deletions(-) create mode 100644 resources/js/middleware/auth.js create mode 100644 resources/js/router.js diff --git a/resources/app.js b/resources/app.js index 6779ee6..bb2c692 100644 --- a/resources/app.js +++ b/resources/app.js @@ -6,7 +6,6 @@ import 'vuetify/styles' import * as components from 'vuetify/components' import * as directives from 'vuetify/directives' import '@mdi/font/css/materialdesignicons.css' -import { createMemoryHistory, createRouter } from 'vue-router' import {createPinia} from "pinia"; const vuetify = createVuetify({ @@ -14,24 +13,7 @@ const vuetify = createVuetify({ directives }) -import AuthButtons from './views/Auth/AuthButtons.vue' -import Login from './views/Auth/Login.vue' -import Registration from './views/Auth/Registration.vue' -import Welcome from './views/Welcome.vue' -import Wishlist from "./views/Wishlist/Wishlist.vue"; - -const routes = [ - { path: '/', component: Welcome }, - { path: '/auth_options', component: AuthButtons }, - { path: '/login', component: Login }, - { path: '/registration', component: Registration }, - { path: '/wishlist', component: Wishlist }, -] - -const router = createRouter({ - history: createMemoryHistory(), - routes, -}) +import router from './js/router.js' const pinia = createPinia() diff --git a/resources/js/middleware/auth.js b/resources/js/middleware/auth.js new file mode 100644 index 0000000..854418b --- /dev/null +++ b/resources/js/middleware/auth.js @@ -0,0 +1,33 @@ +import {useUserStore} from '../../store/user.js' +import router from '../router.js' + +function auth(to, from, next){ + if (to.path === '/'){ + next({path: '/wishlist'}); + } + if (to.path === '/auth_options' || to.path === '/login' || to.path === '/registration'){ + if (useUserStore().token !== null){ + next({path: '/wishlist'}); + } else { + next(); + } + } + if (useUserStore().user === null && useUserStore().token !== null){ + useUserStore().checkUser().then((result)=>{ + if (result === true){ + next(); + } else { + next({path: '/auth_options'}); + } + }).catch((error)=>{ + useUserStore().nullifyUser(); + next({path: '/auth_options'}); + }); + } else if (useUserStore().token === null){ + next({path: '/auth_options'}); + } else { + next(); + } +} + +export default auth; diff --git a/resources/js/router.js b/resources/js/router.js new file mode 100644 index 0000000..aed4f5b --- /dev/null +++ b/resources/js/router.js @@ -0,0 +1,25 @@ +import Welcome from "../views/Welcome.vue"; +import AuthButtons from "../views/Auth/AuthButtons.vue"; +import Login from "../views/Auth/Login.vue"; +import Registration from "../views/Auth/Registration.vue"; +import Wishlist from "../views/Wishlist/Wishlist.vue"; +import {createMemoryHistory, createRouter} from "vue-router"; + +const routes = [ + { path: '/', component: Welcome }, + { path: '/auth_options', component: AuthButtons }, + { path: '/login', component: Login }, + { path: '/registration', component: Registration }, + { path: '/wishlist', component: Wishlist }, +] + +const router = createRouter({ + history: createMemoryHistory(), + routes, +}) + +import auth from './middleware/auth.js'; + +router.beforeEach(auth); + +export default router; diff --git a/resources/publicWishlist.js b/resources/publicWishlist.js index 7a7e90f..d5e129f 100644 --- a/resources/publicWishlist.js +++ b/resources/publicWishlist.js @@ -16,12 +16,6 @@ const vuetify = createVuetify({ const pinia = createPinia() -const routes = [ -] - -const router = createRouter({ - history: createMemoryHistory(), - routes, -}) +import router from './js/router.js' createApp(PublicApp).use(vuetify).use(router).use(pinia).mount("#app") diff --git a/resources/store/user.js b/resources/store/user.js index 6fa0eb2..38ceaa2 100644 --- a/resources/store/user.js +++ b/resources/store/user.js @@ -14,8 +14,9 @@ export const useUserStore = defineStore('user', { this.token = token; localStorage.setItem('auth_token', token); }, - checkUser() { - axios.get( + async checkUser() { + let result = null; + await axios.get( '/api/auth/user', { headers: @@ -26,9 +27,12 @@ export const useUserStore = defineStore('user', { } ).then((res) => { this.setUser(res.data); + result = true; }).catch((error) => { this.nullifyUser(); - }) + result = error; + }); + return result; }, async login(email, password, rememberMe) { await axios.post( diff --git a/resources/views/Auth/Login.vue b/resources/views/Auth/Login.vue index d234192..020aa62 100644 --- a/resources/views/Auth/Login.vue +++ b/resources/views/Auth/Login.vue @@ -47,9 +47,6 @@ export default { if (isLogged){ this.errorMessage = ''; this.errorMessageContainerStyle = 'display: none;'; - // this.$router.push('/'); - // console.log(window.location.href); - // window.location.replace(window.location.href); } else { this.errorMessage = 'Authentication error'; this.errorMessageContainerStyle = ''; diff --git a/resources/views/Auth/Registration.vue b/resources/views/Auth/Registration.vue index 8302abf..35659eb 100644 --- a/resources/views/Auth/Registration.vue +++ b/resources/views/Auth/Registration.vue @@ -52,7 +52,6 @@ export default { if (isRegistred){ this.errorMessage = ''; this.errorMessageContainerStyle = 'display: none;'; - this.$router.push('/'); } else { this.errorMessage = 'Registration error'; this.errorMessageContainerStyle = ''; diff --git a/resources/views/Welcome.vue b/resources/views/Welcome.vue index 002cd95..2a3228d 100644 --- a/resources/views/Welcome.vue +++ b/resources/views/Welcome.vue @@ -28,7 +28,7 @@