어렵구나 ...

This commit is contained in:
이진석
2020-02-06 12:42:14 +09:00
parent 18f2425dfe
commit 85f24d9d0f
9 changed files with 125 additions and 73 deletions

View File

@@ -2,12 +2,12 @@ import axios from 'axios';
import commonUtil from "../utils/commonUtil";
export default {
getArticles({page = 0, size = 10, q = ''}) {
getArticles({page = 0, size = 10, q = ''}, authorization) {
return axios({
url: '/api/articles',
headers: {
'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
'Authorization': authorization
},
params: {
page,
@@ -16,11 +16,11 @@ export default {
}
});
},
getArticle(id) {
getArticle({articleId}, authorization) {
return axios({
url: '/api/articles/' + id,
url: '/api/articles/' + articleId,
headers: {
'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
'Authorization': authorization
}
});
},
@@ -51,12 +51,12 @@ export default {
}
});
},
removeArticle(id) {
removeArticle({articleId}, authorization) {
return axios({
url: '/api/articles/' + id,
url: '/api/articles/' + articleId,
headers: {
'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
'Authorization': authorization
},
method: 'delete'
});

View File

@@ -14,12 +14,12 @@ export default {
}
});
},
session() {
session(authorization) {
return axios({
method: 'get',
url: '/api/users',
headers: {
'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
'Authorization': authorization
}
});
},
@@ -35,8 +35,5 @@ export default {
}
});
},
logout() {
this.$cookie.set('accessToken', null, 0);
}
}

View File

@@ -8,7 +8,7 @@ export default {
method: 'get',
url: '/api/users',
headers: {
'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
'Authorization': commonUtil.getAuthenticationHeaderBearer(this.$cookie.get('accessToken'))
}
});
} catch (e) {

View File

@@ -1,5 +1,7 @@
<template>
<div>
<div>
</div>
<div>
title : <span>{{ article.title }}</span>
</div>
@@ -19,46 +21,36 @@
</template>
<script>
import articleApi from "../../api/articleApi";
import authService from "../../services/authService";
import articleService from "../../services/articleService";
export default {
name: "Detail",
data() {
return {
article: {},
init: false
init: false,
test: ''
}
},
async beforeCreate() {
articleApi.getArticle = articleApi.getArticle.bind(this);
articleApi.removeArticle = articleApi.removeArticle.bind(this);
authService.banishIfUserUnAuthenticated = authService.banishIfUserUnAuthenticated.bind(this);
articleService.getArticle = articleService.getArticle.bind(this);
articleService.removeArticle = articleService.removeArticle.bind(this);
},
async created() {
try {
const articleId = this.$route.params.id;
const result = await articleApi.getArticle(articleId);
this.article = result.data;
this.init = true;
} catch (e) {
console.log(e);
}
await articleService.getArticle(this.$route.params.id);
},
methods: {
async remove() {
const articleId = this.$route.params.id;
if(!confirm('정말 삭제하시겠습니까?')) return;
try {
await articleApi.removeArticle(articleId);
await this.$router.push('/articles');
} catch (e) {
alert('문제가 발생하였습니다.');
console.log(e);
}
}
await articleService.removeArticle(articleId);
},
}
}
</script>

View File

@@ -28,6 +28,8 @@
import authentication from "../../middlewares/authentication";
import authApi from "../../api/authApi";
import commonUtil from "../../utils/commonUtil";
import authService from "../../services/authService";
import articleService from "../../services/articleService";
export default {
name: "List",
@@ -38,40 +40,18 @@
}
},
async beforeCreate() {
authentication.session = authentication.session.bind(this);
articleApi.getArticles = articleApi.getArticles.bind(this);
authApi.logout = authApi.logout.bind(this);
articleService.getArticles = articleService.getArticles.bind(this);
authService.logout = authService.logout.bind(this);
authService.banishIfUserUnAuthenticated = authService.banishIfUserUnAuthenticated.bind(this);
},
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);
}
await authService.banishIfUserUnAuthenticated();
await articleService.getArticles({});
},
methods: {
async logout() {
if (!confirm('정말 로그아웃 하시겠습니까?')) return;
try {
await authApi.logout();
await this.$router.push('/');
} catch (e) {
console.log(e);
}
await authService.logout();
},
clickUser(evt) {
console.log(evt.target);

View File

@@ -11,6 +11,7 @@
<script>
import authApi from "../../api/authApi";
import authService from "../../services/authService";
export default {
name: "Login",
@@ -22,6 +23,7 @@
},
beforeCreate() {
authApi.session = authApi.session.bind(this);
authService.login = authService.login.bind(this);
},
async created() {
try {
@@ -36,14 +38,19 @@
evt.preventDefault();
const { email, password } = this;
try {
const result = await authApi.login({email, password});
const { token } = result.data;
this.$cookie.set('accessToken', token, 1000);
await this.$router.push('/articles');
} catch (err) {
console.log(err);
}
await authService.login(email,password);
// try {
// const result = await authApi.login({email, password});
// const { token } = result.data;
// this.$cookie.set('accessToken', token, 1000);
// await this.$router.push('/articles');
// } catch (err) {
// const message = err.response.data.message;
// if (~message.indexOf('패스워드')) {
// alert('패스워드가 일치하지 않습니다.');
// }
// }
},
}

View File

@@ -0,0 +1,37 @@
import articleApi from "../api/articleApi";
import commonUtil from "../utils/commonUtil";
export default {
async getArticles({page = 0, size = 10}) {
try {
const result = await articleApi.getArticles({page, size}, commonUtil.getAuthenticationHeaderBearer(this.$cookie.get('accessToken')));
this.articles = result.data;
this.pending = false;
} catch (err) {
alert('문제가 발생하였습니다.');
console.log(err.response);
}
},
async getArticle(articleId) {
try {
const authorization = commonUtil.getAuthenticationHeaderBearer(this.$cookie.get('accessToken'));
const result = await articleApi.getArticle({articleId}, authorization);
this.article = result.data;
this.init = true;
} catch (e) {
alert('문제가 발생하였습니다.');
console.log(e);
}
},
async removeArticle(articleId) {
try {
const authorization = commonUtil.getAuthenticationHeaderBearer(this.$cookie.get('accessToken'));
await articleApi.removeArticle({articleId}, authorization);
await this.$router.push('/articles');
} catch (e) {
alert('문제가 발생하였습니다.');
console.log(e);
}
}
}

View File

@@ -0,0 +1,39 @@
import authApi from "../api/authApi";
import authentication from "../middlewares/authentication";
import commonUtil from "../utils/commonUtil";
export default {
async login(email, password) {
try {
const result = await authApi.login({email, password});
const { token } = result.data;
this.$cookie.set('accessToken', token, 1000);
await this.$router.push('/articles');
} catch (err) {
const message = err.response.data.message;
if (~message.indexOf('패스워드')) {
alert('패스워드가 일치하지 않습니다.');
}
}
},
async logout() {
try {
this.$cookie.set('accessToken', null, 0);
await this.$router.push('/');
} catch (e) {
console.log(e);
}
},
async progressIfUserAuthenticated() {
},
async banishIfUserUnAuthenticated() {
try {
await authApi.session(commonUtil.getAuthenticationHeaderBearer(this.$cookie.get('accessToken')));
} catch (e) {
alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.');
await this.$router.replace('/');
return;
}
}
}

View File

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