继续完善登录状态管理功能
This commit is contained in:
parent
2ee3f12888
commit
a420b5d908
@ -1,30 +1,37 @@
|
||||
const state = {
|
||||
user: null
|
||||
}
|
||||
import { createStore } from 'vuex';
|
||||
import axios from 'axios';
|
||||
|
||||
const mutations = {
|
||||
setUser(state, user) {
|
||||
state.user = user
|
||||
}
|
||||
}
|
||||
|
||||
const actions = {
|
||||
login({ commit }, user) {
|
||||
commit('setUser', user)
|
||||
const store = createStore({
|
||||
state: {
|
||||
user: null,
|
||||
isAuthenticated: false,
|
||||
},
|
||||
logout({ commit }) {
|
||||
commit('setUser', null)
|
||||
}
|
||||
}
|
||||
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 getters = {
|
||||
isAuthenticated: state => !!state.user
|
||||
}
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state,
|
||||
mutations,
|
||||
actions,
|
||||
getters
|
||||
}
|
||||
export default store;
|
||||
|
||||
@ -2,7 +2,11 @@
|
||||
import { ref } from 'vue';
|
||||
import { ElInput, ElButton, ElCheckbox, ElForm, ElFormItem, ElMessage } from 'element-plus';
|
||||
import 'element-plus/dist/index.css';
|
||||
import axios from 'axios';
|
||||
import { useStore } from 'vuex';
|
||||
import { useRouter } from 'vue-router';
|
||||
|
||||
const store = useStore();
|
||||
const router = useRouter();
|
||||
|
||||
const username = ref('');
|
||||
const password = ref('');
|
||||
@ -12,26 +16,20 @@ const handleLogin = async () => {
|
||||
console.log('Logging in with', { username: username.value, password: password.value, rememberMe: rememberMe.value });
|
||||
|
||||
try {
|
||||
const response = await axios.post('/api/login', {
|
||||
const response = await store.dispatch('login', {
|
||||
username: username.value,
|
||||
password: password.value,
|
||||
});
|
||||
|
||||
if (response.status === 200) {
|
||||
const data = response.data;
|
||||
ElMessage.success(data.message);
|
||||
// 保存token到本地存储或Vuex状态管理
|
||||
localStorage.setItem('token', data.token);
|
||||
// 处理登录成功逻辑,例如跳转到主页
|
||||
// router.push('/home');
|
||||
ElMessage.success('登录成功');
|
||||
router.push('/profile');
|
||||
} else {
|
||||
// 处理非200的情况
|
||||
ElMessage.error('登录失败,请稍后再试');
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.response && error.response.status === 401) {
|
||||
const data = error.response.data;
|
||||
ElMessage.error(data.message);
|
||||
ElMessage.error('用户名或密码错误');
|
||||
} else {
|
||||
ElMessage.error('登录失败,请稍后再试');
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user