This commit is contained in:
이진석
2020-02-06 14:57:06 +09:00
parent dcd27758f7
commit 69f6314c31
8 changed files with 127 additions and 60 deletions

View File

@@ -24,13 +24,13 @@ export default {
}
});
},
postArticle({title = '', content = ''}) {
postArticle({title = '', content = ''}, authorization) {
return axios({
url: '/api/articles',
method: 'post',
headers: {
'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
'Authorization': authorization
},
data: {
title,
@@ -38,12 +38,12 @@ export default {
}
});
},
updateArticle(id, {title = '', content = ''}) {
updateArticle(id, {title = '', content = ''}, authorization) {
return axios({
url: '/api/articles/' + id,
method: 'put',
headers: {
'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
'Authorization': authorization
},
data: {
title,

View File

@@ -17,8 +17,8 @@
</template>
<script>
import articleApi from "../../api/articleApi";
import authentication from "../../middlewares/authentication";
import articleService from "../../services/articleService";
import authService from "../../services/authService";
export default {
name: "Write",
@@ -30,67 +30,38 @@
}
},
async beforeCreate() {
authentication.session = authentication.session.bind(this);
articleApi.getArticle = articleApi.getArticle.bind(this);
articleApi.postArticle = articleApi.postArticle.bind(this);
articleApi.updateArticle = articleApi.updateArticle.bind(this);
authService.banishIfUserUnAuthenticated = authService.banishIfUserUnAuthenticated.bind(this);
articleService.postArticle = articleService.postArticle.bind(this);
articleService.updateArticle = articleService.updateArticle.bind(this);
articleService.getArticle = articleService.getArticle.bind(this);
articleService.doseSessionHasPermission = articleService.doseSessionHasPermission.bind(this);
},
async created() {
await authentication.session();
const id = this.$route.query.id;
if (id) {
try {
const { data } = await articleApi.getArticle(id);
const {title, content} = data;
await authService.banishIfUserUnAuthenticated();
const { title, content, user } = await articleService.getArticle(id);
await articleService.doseSessionHasPermission(user);
this.title = title;
this.content = content;
this.isEdit = true;
} catch (err) {
alert('문제가 발생하였습니다.');
console.log(err);
}
}
},
methods: {
async create(evt) {
evt.preventDefault();
const {title, content} = this;
const data = {
title,
content
};
try {
await articleApi.postArticle(data);
await this.$router.push('/articles');
} catch (err) {
alert('문제가 발생하였습니다.');
console.log(err);
}
await articleService.postArticle({title, content});
},
async update(evt) {
evt.preventDefault();
const id = this.$route.query.id;
const {title, content} = this;
const data = {
title,
content
};
try {
await articleApi.updateArticle(id, data);
await this.$router.push('/articles');
} catch (err) {
alert('문제가 발생하였습니다.');
console.log(err);
}
await articleService.updateArticle(id, {title, content});
}
}
}

View File

@@ -11,6 +11,7 @@
<script>
import authApi from "../../api/authApi";
import authService from "../../services/authService";
export default {
name: "Register",
@@ -24,6 +25,8 @@
beforeCreate() {
authApi.register = authApi.register.bind(this);
authApi.session = authApi.session.bind(this);
authService.register = authService.register.bind(this);
},
async created() {
try {
@@ -37,13 +40,15 @@
register: async function(evt) {
evt.preventDefault();
const { email, name, password } = this;
try {
await authApi.register({email, name, password});
} catch (err) {
if (err.response.status === 409) {
alert('이미 존재하는 이메일입니다.');
}
}
await authService.register({email, name, password});
// try {
// await authApi.register({email, name, password});
// } catch (err) {
// if (err.response.status === 409) {
// alert('이미 존재하는 이메일입니다.');
// }
// }
}
}
}

View File

@@ -1,5 +1,6 @@
import articleApi from "../api/articleApi";
import commonUtil from "../utils/commonUtil";
import authApi from "../api/authApi";
export default {
async getArticles({page = 0, size = 10}) {
@@ -30,5 +31,44 @@ export default {
alert('문제가 발생하였습니다.');
console.log(e);
}
},
async postArticle(data) {
try {
await articleApi.postArticle(data, commonUtil.getAuthenticationHeaderBearer(this.$cookie.get('accessToken')));
await this.$router.push('/articles');
} catch (err) {
alert('문제가 발생하였습니다.');
console.log(err);
}
},
async updateArticle(id, data) {
try {
await articleApi.updateArticle(id, data, commonUtil.getAuthenticationHeaderBearer(this.$cookie.get('accessToken')));
await this.$router.push("/articles/" + id);
} catch (err) {
alert('문제가 발생하였습니다.');
console.log(err);
}
},
async doseSessionHasPermission(user) {
let session = null;
try {
const result = await authApi.session(commonUtil.getAuthenticationHeaderBearer(this.$cookie.get('accessToken')));
session = result.data;
} catch (err) {
alert('문제가 발생하였습니다.');
return;
}
try {
console.log(user, session);
if (user.id !== session.id) {
throw new Error("현재 사용자가 해당 게시글에 권한이 없습니다.");
}
} catch (err) {
alert(err.message);
await this.$router.replace("/articles/" + this.$route.query.id);
}
}
}

View File

@@ -1,5 +1,4 @@
import authApi from "../api/authApi";
import authentication from "../middlewares/authentication";
import commonUtil from "../utils/commonUtil";
export default {
@@ -16,6 +15,17 @@ export default {
}
}
},
async register(data) {
try {
await authApi.register(data);
alert('가입이 완료되었습니다. 로그인 해 주세요');
await this.$router.push('/auth/login');
} catch (err) {
if (err.response.status === 409) {
alert('이미 존재하는 이메일입니다.');
}
}
},
async logout() {
try {
this.$cookie.set('accessToken', null, 0);
@@ -33,7 +43,7 @@ export default {
} catch (e) {
alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.');
await this.$router.replace('/');
return;
}
}
},
}

View File

@@ -2,6 +2,7 @@ package com.example.vue.domain.auth;
import com.example.vue.domain.user.UserResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -22,6 +23,7 @@ public class AuthController {
}
@PostMapping(value = "/register")
@Transactional
public UserResponseDto register(@RequestBody @Valid RegisterRequestDto registerRequestDto) {
return authService.register(registerRequestDto);
}

View File

@@ -48,6 +48,7 @@ public class User {
this.password = registerRequestDto.getPassword();
this.email = registerRequestDto.getEmail();
this.name = registerRequestDto.getName();
this.createdAt = LocalDateTime.now();
}
}

View File

@@ -0,0 +1,38 @@
package com.example.vue.user;
import com.example.vue.BaseServiceTest;
import com.example.vue.domain.article.ArticleService;
import com.example.vue.domain.auth.AuthService;
import com.example.vue.domain.auth.RegisterRequestDto;
import com.example.vue.domain.user.UserResponseDto;
import com.example.vue.domain.user.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
@SpringBootTest
public class AuthServiceTest {
@Autowired
private BaseServiceTest baseTest;
@Autowired
private ArticleService articleService;
@Autowired
private UserService userService;
@Autowired
private AuthService authService;
@Test
@Transactional
public void registerTest() {
RegisterRequestDto registerRequestDto = new RegisterRequestDto();
registerRequestDto.setEmail("b@b.com");
registerRequestDto.setPassword("1111");
UserResponseDto userResponseDto = authService.register(registerRequestDto);
}
}