bind logic 변경

This commit is contained in:
이진석
2020-02-05 10:45:38 +09:00
parent 32fef52f9e
commit f8bd78a31e
9 changed files with 59 additions and 28 deletions

View File

@@ -2,6 +2,13 @@ import axios from 'axios';
import commonUtil from "../utils/commonUtil";
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 = ''}) {
return axios({

View File

@@ -2,6 +2,12 @@ import axios from "axios";
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) {
const { email, password } = data;
return axios({
@@ -13,12 +19,12 @@ export default {
}
});
},
session(token) {
session() {
return axios({
method: 'get',
url: '/api/users',
headers: {
'Authorization': 'Bearer ' + token
'Authorization': 'Bearer ' + this.$cookie.get('accessToken')
}
});
},
@@ -38,3 +44,4 @@ export default {
this.$cookie.set('accessToken', null, 0);
}
}

View File

@@ -1,6 +1,10 @@
import axios from "axios";
export default async function () {
export default {
bind(context) {
this.session = this.session.bind(context);
},
async session() {
await axios({
method: 'get',
url: '/api/users',
@@ -9,3 +13,4 @@ export default async function () {
}
});
}
}

View File

@@ -11,9 +11,10 @@
export default {
name: "Welcome",
async beforeCreate() {
const accessToken = this.$cookie.get('accessToken');
authApi.bind(this);
try {
await authApi.session.bind(this)(accessToken);
await authApi.session();
await this.$router.replace('/articles');
} catch (e) {
console.log(e);

View File

@@ -30,9 +30,11 @@
}
},
async beforeCreate() {
articleApi.bind(this);
try {
const articleId = this.$route.params.id;
const result = await articleApi.getArticle.bind(this)(articleId);
const result = await articleApi.getArticle(articleId);
this.article = result.data;
this.init = true;
@@ -46,7 +48,7 @@
if(!confirm('정말 삭제하시겠습니까?')) return;
try {
await articleApi.removeArticle.bind(this)(articleId);
await articleApi.removeArticle(articleId);
await this.$router.push('/articles');
} catch (e) {
alert('문제가 발생하였습니다.');

View File

@@ -30,8 +30,12 @@
}
},
async beforeCreate() {
authentication.bind(this);
articleApi.bind(this);
authApi.bind(this);
try {
await authentication.bind(this)();
await authentication.session();
} catch (e) {
alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.');
await this.$router.replace('/');
@@ -39,7 +43,7 @@
}
try {
const result = await articleApi.getArticles.bind(this)({});
const result = await articleApi.getArticles({});
this.articles = result.data;
this.pending = false;
} catch (err) {
@@ -52,7 +56,7 @@
if (!confirm('정말 로그아웃 하시겠습니까?')) return;
try {
await authApi.logout.bind(this)();
await authApi.logout();
await this.$router.push('/');
} catch (e) {
console.log(e);

View File

@@ -30,8 +30,11 @@
}
},
async beforeCreate() {
authentication.bind(this);
articleApi.bind(this);
try {
authentication.bind(this)();
await authentication.session();
} catch (err) {
alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.');
await this.$router.replace('/');
@@ -42,7 +45,7 @@
if (id) {
try {
const result = await articleApi.getArticle.bind(this)(id);
const result = await articleApi.getArticle(id);
const {title, content} = result.data;
this.title = title;
this.content = content;
@@ -53,7 +56,7 @@
}
},
methods: {
create: async function(evt) {
async create(evt) {
evt.preventDefault();
const {title, content} = this;
@@ -64,7 +67,7 @@
};
try {
await articleApi.postArticle.bind(this)(data);
await articleApi.postArticle(data);
await this.$router.push('/articles');
} catch (err) {
alert('문제가 발생하였습니다.');
@@ -84,7 +87,7 @@
};
try {
await articleApi.updateArticle.bind(this)(id, data);
await articleApi.updateArticle(id, data);
await this.$router.push('/articles');
} catch (err) {
alert('문제가 발생하였습니다.');

View File

@@ -24,9 +24,12 @@
}
},
async beforeCreate() {
const accessToken = this.$cookie.get('accessToken');
authApi.bind(this);
try {
await authApi.session.bind(this)(accessToken);
await authApi.session();
await this.$router.replace('/articles');
} catch (e) {
console.log(e);
@@ -48,9 +51,8 @@
},
session: async function(evt) {
const accessToken = this.$cookie.get('accessToken');
try {
const result = await authApi.session(accessToken);
const result = await authApi.session();
console.log(result);
} catch (err) {
console.log(err);

View File

@@ -25,7 +25,7 @@
evt.preventDefault();
const { email, name, password } = this;
try {
const result = await authApi.register({email, name, password});
await authApi.register({email, name, password});
} catch (err) {
if (err.response.status === 409) {
alert('이미 존재하는 이메일입니다.');