This commit is contained in:
이진석
2020-02-04 15:31:40 +09:00
parent 82087941b2
commit 32fef52f9e
6 changed files with 136 additions and 113 deletions

View File

@@ -1,7 +1,8 @@
import axios from 'axios'; import axios from 'axios';
import commonUtil from "../utils/commonUtil"; import commonUtil from "../utils/commonUtil";
function getArticles({page = 0, size = 10, q = ''}) { export default {
getArticles({page = 0, size = 10, q = ''}) {
return axios({ return axios({
url: '/api/articles', url: '/api/articles',
@@ -14,18 +15,16 @@ function getArticles({page = 0, size = 10, q = ''}) {
q q
} }
}); });
} },
getArticle(id) {
function getArticle(id) {
return axios({ return axios({
url: '/api/articles/' + id, url: '/api/articles/' + id,
headers: { headers: {
'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)() 'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
} }
}); });
} },
postArticle({title = '', content = ''}) {
function postArticle({title = '', content = ''}) {
return axios({ return axios({
url: '/api/articles', url: '/api/articles',
@@ -38,9 +37,8 @@ function postArticle({title = '', content = ''}) {
content content
} }
}); });
} },
updateArticle(id, {title = '', content = ''}) {
function updateArticle(id, {title = '', content = ''}) {
return axios({ return axios({
url: '/api/articles/' + id, url: '/api/articles/' + id,
method: 'put', method: 'put',
@@ -52,9 +50,8 @@ function updateArticle(id, {title = '', content = ''}) {
content content
} }
}); });
} },
removeArticle(id) {
function removeArticle(id) {
return axios({ return axios({
url: '/api/articles/' + id, url: '/api/articles/' + id,
@@ -63,12 +60,5 @@ function removeArticle(id) {
}, },
method: 'delete' method: 'delete'
}); });
} }
export default {
getArticles,
getArticle,
postArticle,
updateArticle,
removeArticle
} }

View File

@@ -1,6 +1,8 @@
import axios from "axios"; import axios from "axios";
function login(data) {
export default {
login(data) {
const { email, password } = data; const { email, password } = data;
return axios({ return axios({
method: 'post', method: 'post',
@@ -10,9 +12,17 @@ function login(data) {
password password
} }
}); });
} },
session(token) {
function register(data) { return axios({
method: 'get',
url: '/api/users',
headers: {
'Authorization': 'Bearer ' + token
}
});
},
register(data) {
const { email, name, password } = data; const { email, name, password } = data;
return axios({ return axios({
method: 'post', method: 'post',
@@ -23,20 +33,8 @@ function register(data) {
password password
} }
}); });
} },
logout() {
function session(token) { this.$cookie.set('accessToken', null, 0);
return axios({
method: 'get',
url: '/api/users',
headers: {
'Authorization': 'Bearer ' + token
} }
});
}
export default {
login,
session,
register
} }

View File

@@ -8,5 +8,4 @@ export default async function () {
'Authorization': 'Bearer ' + this.$cookie.get('accessToken') 'Authorization': 'Bearer ' + this.$cookie.get('accessToken')
} }
}); });
} }

View File

@@ -1,15 +1,24 @@
<template> <template>
<div> <div>
home <router-link to="/auth/login">로그인</router-link> <br>
<router-link to="/auth/login">로그인</router-link> <router-link to="/auth/register">회원가입</router-link> <br>
<router-link to="/auth/register">회원가입</router-link>
<router-link to="/articles">게시글</router-link>
</div> </div>
</template> </template>
<script> <script>
import authApi from "../api/authApi";
export default { export default {
name: "Welcome" name: "Welcome",
async beforeCreate() {
const accessToken = this.$cookie.get('accessToken');
try {
await authApi.session.bind(this)(accessToken);
await this.$router.replace('/articles');
} catch (e) {
console.log(e);
}
}
} }
</script> </script>

View File

@@ -9,12 +9,17 @@
<div> <div>
<router-link to="/articles/write">글쓰기</router-link> <router-link to="/articles/write">글쓰기</router-link>
</div> </div>
<div>
<button type="button" @click="logout">로그아웃</button>
</div>
</div> </div>
</template> </template>
<script> <script>
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";
export default { export default {
name: "List", name: "List",
@@ -41,6 +46,19 @@
alert('문제가 발생하였습니다.'); alert('문제가 발생하였습니다.');
console.log(err.response); console.log(err.response);
} }
},
methods: {
async logout() {
if (!confirm('정말 로그아웃 하시겠습니까?')) return;
try {
await authApi.logout.bind(this)();
await this.$router.push('/');
} catch (e) {
console.log(e);
}
}
} }
} }
</script> </script>

View File

@@ -23,6 +23,15 @@
password: '' password: ''
} }
}, },
async beforeCreate() {
const accessToken = this.$cookie.get('accessToken');
try {
await authApi.session.bind(this)(accessToken);
await this.$router.replace('/articles');
} catch (e) {
console.log(e);
}
},
methods: { methods: {
submit: async function(evt) { submit: async function(evt) {
evt.preventDefault(); evt.preventDefault();