front update

This commit is contained in:
이진석
2020-02-06 10:56:55 +09:00
parent 71c972836d
commit 24fe526aaa
10 changed files with 81 additions and 86 deletions

View File

@@ -2,13 +2,6 @@ import axios from 'axios';
import commonUtil from "../utils/commonUtil"; import commonUtil from "../utils/commonUtil";
export default { export default {
bind(context) {
this.getArticle = this.getArticle.bind(context);
this.getArticles = this.getArticles.bind(context);
this.postArticle = this.postArticle.bind(context);
this.updateArticle = this.updateArticle.bind(context);
this.removeArticle = this.removeArticle.bind(context);
},
getArticles({page = 0, size = 10, q = ''}) { getArticles({page = 0, size = 10, q = ''}) {
return axios({ return axios({

View File

@@ -1,13 +1,8 @@
import axios from "axios"; import axios from "axios";
import commonUtil from "../utils/commonUtil";
export default { export default {
bind(context) {
this.login = this.login.bind(context);
this.session = this.session.bind(context);
this.register = this.register.bind(context);
this.logout = this.logout.bind(context);
},
login(data) { login(data) {
const { email, password } = data; const { email, password } = data;
return axios({ return axios({
@@ -24,7 +19,7 @@ export default {
method: 'get', method: 'get',
url: '/api/users', url: '/api/users',
headers: { headers: {
'Authorization': 'Bearer ' + this.$cookie.get('accessToken') 'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
} }
}); });
}, },

View File

@@ -1,16 +1,20 @@
import axios from "axios"; import axios from "axios";
import commonUtil from "../utils/commonUtil";
export default { export default {
bind(context) {
this.session = this.session.bind(context);
},
async session() { async session() {
await axios({ try {
method: 'get', await axios({
url: '/api/users', method: 'get',
headers: { url: '/api/users',
'Authorization': 'Bearer ' + this.$cookie.get('accessToken') headers: {
} 'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
}); }
});
} catch (e) {
alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.');
await this.$router.replace('/');
}
} }
} }

View File

@@ -11,8 +11,9 @@
export default { export default {
name: "Welcome", name: "Welcome",
async beforeCreate() { async beforeCreate() {
authApi.bind(this); authApi.session = authApi.session.bind(this);
},
async created() {
try { try {
await authApi.session(); await authApi.session();
await this.$router.replace('/articles'); await this.$router.replace('/articles');

View File

@@ -30,8 +30,10 @@
} }
}, },
async beforeCreate() { async beforeCreate() {
articleApi.bind(this); articleApi.getArticle = articleApi.getArticle.bind(this);
articleApi.removeArticle = articleApi.removeArticle.bind(this);
},
async created() {
try { try {
const articleId = this.$route.params.id; const articleId = this.$route.params.id;
const result = await articleApi.getArticle(articleId); const result = await articleApi.getArticle(articleId);
@@ -41,6 +43,7 @@
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
}, },
methods: { methods: {
async remove() { async remove() {

View File

@@ -27,6 +27,7 @@
import articleApi from "../../api/articleApi"; import articleApi from "../../api/articleApi";
import authentication from "../../middlewares/authentication"; import authentication from "../../middlewares/authentication";
import authApi from "../../api/authApi"; import authApi from "../../api/authApi";
import commonUtil from "../../utils/commonUtil";
export default { export default {
name: "List", name: "List",
@@ -37,28 +38,30 @@
} }
}, },
async beforeCreate() { async beforeCreate() {
authentication.bind(this); authentication.session = authentication.session.bind(this);
articleApi.bind(this); articleApi.getArticles = articleApi.getArticles.bind(this);
authApi.bind(this); authApi.logout = authApi.logout.bind(this);
try {
await authentication.session();
} catch (e) {
alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.');
await this.$router.replace('/');
return;
}
try {
const result = await articleApi.getArticles({});
this.articles = result.data;
this.pending = false;
} catch (err) {
alert('문제가 발생하였습니다.');
console.log(err.response);
}
}, },
methods: { async created() {
try {
await authentication.session();
} catch (e) {
alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.');
await this.$router.replace('/');
return;
}
try {
const result = await articleApi.getArticles({});
this.articles = result.data;
this.pending = false;
} catch (err) {
alert('문제가 발생하였습니다.');
console.log(err.response);
}
},
methods: {
async logout() { async logout() {
if (!confirm('정말 로그아웃 하시겠습니까?')) return; if (!confirm('정말 로그아웃 하시겠습니까?')) return;

View File

@@ -30,16 +30,13 @@
} }
}, },
async beforeCreate() { async beforeCreate() {
authentication.bind(this); authentication.session = authentication.session.bind(this);
articleApi.bind(this); articleApi.getArticle = articleApi.getArticle.bind(this);
articleApi.postArticle = articleApi.postArticle.bind(this);
try { articleApi.updateArticle = articleApi.updateArticle.bind(this);
await authentication.session(); },
} catch (err) { async created() {
alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.'); await authentication.session();
await this.$router.replace('/');
return;
}
const id = this.$route.query.id; const id = this.$route.query.id;

View File

@@ -1,14 +1,11 @@
<template> <template>
<div> <div>
<router-link to="/">home</router-link> <router-link to="/">home</router-link>
<form @submit="submit"> <form @submit="submit">
<input type="email" v-model="email"> <input type="email" v-model="email">
<input type="password" v-model="password"> <input type="password" v-model="password">
<button type="submit">확인</button> <button type="submit">확인</button>
</form> </form>
<button type="button" @click="session">세션확인</button>
</div> </div>
</template> </template>
@@ -23,17 +20,16 @@
password: '' password: ''
} }
}, },
async beforeCreate() { beforeCreate() {
authApi.bind(this); authApi.session = authApi.session.bind(this);
},
async created() {
try {
try { await authApi.session();
await authApi.session(); await this.$router.replace('/articles');
await this.$router.replace('/articles'); } catch (e) {
} catch (e) { console.log(e);
console.log(e); }
}
}, },
methods: { methods: {
submit: async function(evt) { submit: async function(evt) {
@@ -50,14 +46,6 @@
} }
}, },
session: async function(evt) {
try {
const result = await authApi.session();
console.log(result);
} catch (err) {
console.log(err);
}
}
} }
} }
</script> </script>

View File

@@ -20,7 +20,19 @@
password: '' password: ''
} }
}, },
methods: { beforeCreate() {
authApi.register = authApi.register.bind(this);
authApi.session = authApi.session.bind(this);
},
async created() {
try {
await authApi.session();
await this.$router.replace("/articles");
} catch (err) {
console.log(err);
}
},
methods: {
register: async function(evt) { register: async function(evt) {
evt.preventDefault(); evt.preventDefault();
const { email, name, password } = this; const { email, name, password } = this;

View File

@@ -1,7 +1,6 @@
function getAuthenticationHeaderBearer() {
return 'Bearer ' + this.$cookie.get('accessToken');
}
export default { export default {
getAuthenticationHeaderBearer getAuthenticationHeaderBearer() {
} return 'Bearer ' + this.$cookie.get('accessToken');
}
};