first commit
This commit is contained in:
123
src/store/accountStore.js
Normal file
123
src/store/accountStore.js
Normal file
@@ -0,0 +1,123 @@
|
||||
import * as memberApi from '@/api/member'
|
||||
import Vue from 'vue';
|
||||
|
||||
const enhanceAccessToken = () => {
|
||||
const {accessToken} = localStorage
|
||||
if (!accessToken) return
|
||||
Vue.axios.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
|
||||
}
|
||||
|
||||
enhanceAccessToken();
|
||||
|
||||
const accountStore = {
|
||||
namespaced: true,
|
||||
state: {
|
||||
userId: '',
|
||||
nickname: '',
|
||||
accessToken: '',
|
||||
profileImage: '',
|
||||
},
|
||||
getters: {
|
||||
isAuthenticated(state) {
|
||||
state.userId = state.userId || localStorage.userId
|
||||
state.nickname = state.nickname || localStorage.nickname
|
||||
state.profileImage = state.profileImage || localStorage.profileImage
|
||||
state.accessToken = state.accessToken || localStorage.accessToken
|
||||
|
||||
return state.accessToken !== undefined &&
|
||||
state.accessToken !== null &&
|
||||
state.accessToken !== 'null' &&
|
||||
state.accessToken !== ''
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
LOGIN(state, {userId, nickname, token, profileImage}) {
|
||||
state.userId = userId
|
||||
localStorage.userId = userId
|
||||
|
||||
state.nickname = nickname
|
||||
localStorage.nickname = nickname
|
||||
|
||||
state.profileImage = profileImage
|
||||
localStorage.profileImage = profileImage
|
||||
|
||||
state.accessToken = token
|
||||
localStorage.accessToken = token
|
||||
|
||||
Vue.axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
|
||||
},
|
||||
|
||||
LOGOUT(state) {
|
||||
state.userId = ''
|
||||
state.nickname = ''
|
||||
state.profileImage = ''
|
||||
state.accessToken = ''
|
||||
|
||||
localStorage.clear()
|
||||
if (location.pathname === '/') {
|
||||
location.reload()
|
||||
} else {
|
||||
location.replace("/")
|
||||
}
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
async LOGIN({commit}, {email, password}) {
|
||||
let result = false
|
||||
let errorMessage = null
|
||||
|
||||
try {
|
||||
let res = await memberApi.login(email, password)
|
||||
if (res.data.success === true) {
|
||||
commit("LOGIN", res.data.data)
|
||||
result = true
|
||||
} else {
|
||||
errorMessage = res.data.message
|
||||
}
|
||||
} catch (e) {
|
||||
errorMessage = '로그인 정보를 확인해주세요.'
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (result) {
|
||||
resolve();
|
||||
} else {
|
||||
reject(errorMessage)
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
async LOGOUT({commit}) {
|
||||
let result = false
|
||||
let errorMessage = null
|
||||
|
||||
try {
|
||||
let res = await memberApi.logout()
|
||||
if (res.data.success === true) {
|
||||
Vue.axios.defaults.headers.common['Authorization'] = undefined;
|
||||
commit("LOGOUT")
|
||||
result = true
|
||||
} else {
|
||||
errorMessage = res.data.message
|
||||
}
|
||||
} catch (e) {
|
||||
errorMessage = '로그인 정보를 확인해주세요.'
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (result) {
|
||||
resolve();
|
||||
} else {
|
||||
reject(errorMessage)
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
LOGOUT_WITHOUT_NETWORK({commit}) {
|
||||
Vue.axios.defaults.headers.common['Authorization'] = undefined;
|
||||
commit("LOGOUT")
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export default accountStore
|
12
src/store/index.js
Normal file
12
src/store/index.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
|
||||
Vue.use(Vuex)
|
||||
|
||||
import accountStore from "@/store/accountStore";
|
||||
|
||||
export default new Vuex.Store({
|
||||
modules: {
|
||||
accountStore: accountStore
|
||||
}
|
||||
})
|
Reference in New Issue
Block a user