From f776f756c80c5d95dbad66ec376c68fcb33ec97a Mon Sep 17 00:00:00 2001 From: heshunme Date: Sun, 30 Jun 2024 17:53:43 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E5=AE=8C=E5=96=84=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/authentication.js | 45 +++++++++++++---- src/store/authentication.js | 81 +++++++++++++++++------------- src/views/authentication/Login.vue | 2 +- 3 files changed, 82 insertions(+), 46 deletions(-) diff --git a/src/router/authentication.js b/src/router/authentication.js index 9b5ebe7..712d394 100644 --- a/src/router/authentication.js +++ b/src/router/authentication.js @@ -1,12 +1,35 @@ -// !!!这是一个示例,请根据实际情况修改!!! -import Login from '../views/authentication/Login.vue' -import Register from '../views/authentication/Register.vue' -import ManageProfile from '../views/authentication/ManageProfile.vue' -import Profile from '../views/authentication/Profile.vue' +import Login from '../views/authentication/Login.vue'; +import Register from '../views/authentication/Register.vue'; +import ManageProfile from '../views/authentication/ManageProfile.vue'; +import Profile from '../views/authentication/Profile.vue'; +import store from '../store'; -export default [ - { path: '/login', component: Login }, - { path: '/register', component: Register }, - { path: '/manageProfile', component: ManageProfile }, - { path: '/profile', component: Profile }, -] +const routes = [ + { path: '/login', component: Login }, + { path: '/register', component: Register }, + { path: '/manageProfile', component: ManageProfile }, + { + path: '/profile', + component: Profile, + meta: { requiresAuth: true }, + }, +]; + +const router = createRouter({ + history: createWebHistory(), + routes, +}); + +router.beforeEach((to, from, next) => { + if (to.matched.some(record => record.meta.requiresAuth)) { + if (!store.getters['authentication/isAuthenticated']) { + next('/login'); + } else { + next(); + } + } else { + next(); + } +}); + +export default routes; diff --git a/src/store/authentication.js b/src/store/authentication.js index cc60c35..6e953a5 100644 --- a/src/store/authentication.js +++ b/src/store/authentication.js @@ -1,37 +1,50 @@ -import { createStore } from 'vuex'; import axios from 'axios'; -const store = createStore({ - state: { - user: null, - isAuthenticated: false, - }, - mutations: { - setUser(state, user) { - state.user = user; - state.isAuthenticated = true; - }, - clearUser(state) { - state.user = null; - state.isAuthenticated = false; - }, - }, - actions: { - async login({ commit }, credentials) { - const response = await axios.post('/api/login', credentials); - if (response.status === 200) { - commit('setUser', response.data.username); - } - return response; - }, - logout({ commit }) { - commit('clearUser'); - }, - }, - getters: { - isAuthenticated: (state) => state.isAuthenticated, - user: (state) => state.user, - }, -}); +const state = { + user: null, + isAuthenticated: false, +}; -export default store; +const mutations = { + setUser(state, user) { + state.user = user; + state.isAuthenticated = true; + }, + clearUser(state) { + state.user = null; + state.isAuthenticated = false; + }, +}; + +const actions = { + async login({ commit }, credentials) { + const response = await axios.post('/api/login', credentials); + if (response.status === 200) { + commit('setUser', response.data.username); + } + return response; + }, + logout({ commit }) { + commit('clearUser'); + }, + async fetchUserProfile({ commit }) { + const response = await axios.get('/api/user-profile'); + if (response.status === 200) { + commit('setUser', response.data); + } + return response; + }, +}; + +const getters = { + isAuthenticated: (state) => state.isAuthenticated, + user: (state) => state.user, +}; + +export default { + namespaced: true, + state, + mutations, + actions, + getters, +}; diff --git a/src/views/authentication/Login.vue b/src/views/authentication/Login.vue index d7a3430..143a251 100644 --- a/src/views/authentication/Login.vue +++ b/src/views/authentication/Login.vue @@ -16,7 +16,7 @@ const handleLogin = async () => { console.log('Logging in with', { username: username.value, password: password.value, rememberMe: rememberMe.value }); try { - const response = await store.dispatch('login', { + const response = await store.dispatch('authentication/login', { username: username.value, password: password.value, });