diff --git a/src/store/authentication.js b/src/store/authentication.js index 8d7e12e..82c2e83 100644 --- a/src/store/authentication.js +++ b/src/store/authentication.js @@ -3,48 +3,52 @@ import axios from 'axios'; const state = { user: null, isAuthenticated: false, + token: null, }; const mutations = { - setUser(state, user) { + setUser(state, {user, token}) { state.user = user; state.isAuthenticated = true; + state.token = token; }, clearUser(state) { state.user = null; state.isAuthenticated = false; + state.token = null; }, }; 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; - }, + async login({commit}, credentials) { + const response = await axios.post('/api/login', credentials); + if (response.status === 200) { + commit('setUser', {user: response.data.username, token: response.data.token}); + } + return response; + }, + logout({commit}) { + commit('clearUser'); + }, + async fetchUserProfile({commit}) { + const response = await axios.get('/api/user-profile'); + if (response.status === 200) { + commit('setUser', {user: response.data}); + } + return response; + }, }; const getters = { - isAuthenticated: (state) => state.isAuthenticated, - user: (state) => state.user, + isAuthenticated: (state) => state.isAuthenticated, + user: (state) => state.user, + token: (state) => state.token, }; export default { - namespaced: true, - state, - mutations, - actions, - getters, + namespaced: true, + state, + mutations, + actions, + getters, }; diff --git a/src/views/authentication/Register.vue b/src/views/authentication/Register.vue index 7c5f75f..91fead7 100644 --- a/src/views/authentication/Register.vue +++ b/src/views/authentication/Register.vue @@ -5,54 +5,54 @@ import 'element-plus/dist/index.css'; import axios from 'axios'; import {useRouter} from 'vue-router'; -const companyName = ref(''); -const contactInfo = ref(''); +const username = ref(''); +const phoneNumber = ref(''); const password = ref(''); const verificationCode = ref(''); const router = useRouter(); const handleRegister = async () => { console.log('Registering with', { - companyName: companyName.value, - contactInfo: contactInfo.value, + username: username.value, + phoneNumber: phoneNumber.value, password: password.value, verificationCode: verificationCode.value }); try { const response = await axios.post('/api/register', { - companyName: companyName.value, - contactInfo: contactInfo.value, + username: username.value, + phoneNumber: phoneNumber.value, password: password.value, verificationCode: verificationCode.value, }); if (response.status === 200) { - ElMessage.success('注册成功'); + ElMessage.success(response.data.message); router.push('/login'); } else { - ElMessage.error('注册失败,请稍后再试'); + ElMessage.error(response.data.message); } } catch (error) { - ElMessage.error('注册失败,请稍后再试'); + ElMessage.error(error.response.data.message || '注册失败,请稍后再试'); } }; const sendVerificationCode = async () => { - // try { - // const response = await axios.post('/api/send-verification-code', { - // contactInfo: contactInfo.value, - // }); - // - // if (response.status === 200) { - // ElMessage.success('验证码已发送'); - // } else { - // ElMessage.error('发送验证码失败,请稍后再试'); - // } - // } catch (error) { - // ElMessage.error('发送验证码失败,请稍后再试'); - // } - ElMessage.success('验证码已发送'); + try { + const response = await axios.post('/api/sendVerificationCode', { + phoneNumber: phoneNumber.value, + }); + + if (response.status === 200) { + verificationCode.value = response.data.code; + ElMessage.success('验证码已发送'); + } else { + ElMessage.error('发送验证码失败,请稍后再试'); + } + } catch (error) { + ElMessage.error('发送验证码失败,请稍后再试'); + } }; @@ -62,12 +62,12 @@ const sendVerificationCode = async () => {