Store_Vuex init...

This commit is contained in:
최준흠 2022-09-12 16:08:58 +09:00
parent 83b7b68897
commit 057c089b94
5 changed files with 34 additions and 72 deletions

View File

@ -80,47 +80,23 @@ export default {
return dirty || validated ? valid : null return dirty || validated ? valid : null
}, },
async onSubmit() { async onSubmit() {
try { //
//1. await this.$store
const tokens = await this.getTokens()
//2. BackEnd
//console.log(tokens)
await this.sendAccessToken(tokens)
//3.next
const return_url = this.$route.params.return_url || 'home'
alert('로그인 성공..\n' + return_url)
this.$router.push({ name: return_url }).catch((e) => {
throw new Error(return_url + '로 Redirect 실패\n' + e.message)
})
} catch ({ message }) {
alert('로그인 실패...\n' + message)
}
},
async getTokens() {
return await this.$store
.dispatch('AuthStore/login', { .dispatch('AuthStore/login', {
email: this.form.email, email: this.form.email,
password: this.form.password password: this.form.password
}) })
.then((response) => { .then(() => {
//console.log(response) this.$router
//alert(' ..') .push({
return response.data.tokens name: this.$route.params.return_url || 'home'
})
.catch((e) => {
throw new Error('router 오류\n' + +e.response.data.message)
})
}) })
.catch((e) => { .catch((e) => {
throw new Error('인증서버에서 인증코드 받기 실패..\n' + e.message) alert(e)
})
},
async sendTokens(tokens) {
return await this.$store
.dispatch('TodoStore/sendTokens', tokens)
.then((response) => {
console.log(response)
alert('인증코드 전송 성공..')
return response.data
})
.catch((e) => {
throw new Error('인증코드 전송 실패..\n' + e.message)
}) })
} }
} }

View File

@ -246,9 +246,9 @@ export default {
} }
}, },
methods: { methods: {
setDatas(page = 1) { async setDatas(page = 1) {
this.$store await this.$store
.dispatch('TodoStore/setData', { .dispatch('TodoStore/setDatas', {
page: page, page: page,
perPage: this.perPage, perPage: this.perPage,
sortBy: this.sortBy, sortBy: this.sortBy,
@ -260,8 +260,8 @@ export default {
filterDateStart: this.filterDateStart, filterDateStart: this.filterDateStart,
filterDateEnd: this.filterDateEnd filterDateEnd: this.filterDateEnd
}) })
.catch((error) => { .catch((e) => {
alert(error) alert(e)
}) })
}, },
searchClick() { searchClick() {

View File

@ -1,5 +1,5 @@
import axios from 'axios' import axios from 'axios'
import jwt from './jwt' import jwt from '../../common/jwt'
//참조: https://yamoo9.github.io/axios/guide/interceptors.html //참조: https://yamoo9.github.io/axios/guide/interceptors.html
const apiServer = axios.create({ const apiServer = axios.create({
baseURL: process.env.VUE_APP_AUTH_HOST, baseURL: process.env.VUE_APP_AUTH_HOST,
@ -58,17 +58,18 @@ const actions = {
.post('/auth/login', params) .post('/auth/login', params)
.then((response) => { .then((response) => {
const { data } = response const { data } = response
context.commit('login', { //console.log(data)
tokens: data.tokens context.commit('login', { tokens: data })
})
return response return response
}) })
.catch((error) => { .catch((e) => {
return error throw new Error('login 오류\n' + e.response.data.message)
}) })
}, },
logout: async function (context, payload) { logout: async function (context, payload) {
return await context.commit('logout', payload) return await context.commit('logout', payload).catch((e) => {
throw new Error('logout 오류\n' + +e.response.data.message)
})
}, },
register: async function (context, payload) { register: async function (context, payload) {
const params = { const params = {
@ -80,13 +81,11 @@ const actions = {
.post('/auth/register', params) .post('/auth/register', params)
.then((response) => { .then((response) => {
const { data } = response const { data } = response
context.commit('login', { context.commit('login', { tokens: data.tokens })
tokens: data.tokens
})
return response return response
}) })
.catch((error) => { .catch((e) => {
return error throw new Error('register 오류\n' + +e.response.data.message)
}) })
} }
} }

View File

@ -1,5 +1,5 @@
import axios from 'axios' import axios from 'axios'
import jwt from './jwt' import jwt from '../../common/jwt'
//참조: https://yamoo9.github.io/axios/guide/interceptors.html //참조: https://yamoo9.github.io/axios/guide/interceptors.html
const apiServer = axios.create({ const apiServer = axios.create({
@ -30,37 +30,24 @@ const getters = {
} }
} }
const mutations = { const mutations = {
setData: function (state, data) { setDatas: function (state, data) {
state.total = data.total state.total = data.total
state.rows = data.rows state.rows = data.rows
} }
} }
const actions = { const actions = {
sendAccessToken: async function (context, accessToken) { setDatas: async function (context, params) {
//주의 .get의 parameter는 { params: params} 처럼 보내야함
return await apiServer
.get('/todo/accessToken', { params: accessToken })
.then((response) => {
const { data } = response
context.commit('setData', data)
return response
})
.catch((error) => {
return error
})
},
setData: async function (context, params) {
console.log(params) console.log(params)
//주의 .get의 parameter는 { params: params} 처럼 보내야함 //주의 .get의 parameter는 { params: params} 처럼 보내야함
return await apiServer return await apiServer
.get('/todo', { params: params }) .get('/todo', { params: params })
.then((response) => { .then((response) => {
const { data } = response const { data } = response
context.commit('setData', data) console.log(data)
return response context.commit('setDatas', data)
}) })
.catch((error) => { .catch((e) => {
return error throw new Error('todo List 오류\n' + +e.response.data.message)
}) })
} }
} }