update
This commit is contained in:
@@ -24,13 +24,13 @@ export default {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
postArticle({title = '', content = ''}) {
|
postArticle({title = '', content = ''}, authorization) {
|
||||||
|
|
||||||
return axios({
|
return axios({
|
||||||
url: '/api/articles',
|
url: '/api/articles',
|
||||||
method: 'post',
|
method: 'post',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
|
'Authorization': authorization
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
title,
|
title,
|
||||||
@@ -38,12 +38,12 @@ export default {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
updateArticle(id, {title = '', content = ''}) {
|
updateArticle(id, {title = '', content = ''}, authorization) {
|
||||||
return axios({
|
return axios({
|
||||||
url: '/api/articles/' + id,
|
url: '/api/articles/' + id,
|
||||||
method: 'put',
|
method: 'put',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': commonUtil.getAuthenticationHeaderBearer.bind(this)()
|
'Authorization': authorization
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
title,
|
title,
|
||||||
|
|||||||
@@ -17,8 +17,8 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import articleApi from "../../api/articleApi";
|
import articleService from "../../services/articleService";
|
||||||
import authentication from "../../middlewares/authentication";
|
import authService from "../../services/authService";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "Write",
|
name: "Write",
|
||||||
@@ -30,67 +30,38 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
async beforeCreate() {
|
async beforeCreate() {
|
||||||
authentication.session = authentication.session.bind(this);
|
authService.banishIfUserUnAuthenticated = authService.banishIfUserUnAuthenticated.bind(this);
|
||||||
articleApi.getArticle = articleApi.getArticle.bind(this);
|
articleService.postArticle = articleService.postArticle.bind(this);
|
||||||
articleApi.postArticle = articleApi.postArticle.bind(this);
|
articleService.updateArticle = articleService.updateArticle.bind(this);
|
||||||
articleApi.updateArticle = articleApi.updateArticle.bind(this);
|
articleService.getArticle = articleService.getArticle.bind(this);
|
||||||
|
articleService.doseSessionHasPermission = articleService.doseSessionHasPermission.bind(this);
|
||||||
},
|
},
|
||||||
async created() {
|
async created() {
|
||||||
await authentication.session();
|
|
||||||
|
|
||||||
const id = this.$route.query.id;
|
const id = this.$route.query.id;
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
try {
|
await authService.banishIfUserUnAuthenticated();
|
||||||
const { data } = await articleApi.getArticle(id);
|
const { title, content, user } = await articleService.getArticle(id);
|
||||||
const {title, content} = data;
|
await articleService.doseSessionHasPermission(user);
|
||||||
this.title = title;
|
|
||||||
this.content = content;
|
this.title = title;
|
||||||
this.isEdit = true;
|
this.content = content;
|
||||||
} catch (err) {
|
this.isEdit = true;
|
||||||
alert('문제가 발생하였습니다.');
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
async create(evt) {
|
async create(evt) {
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
|
|
||||||
const {title, content} = this;
|
const {title, content} = this;
|
||||||
|
await articleService.postArticle({title, content});
|
||||||
const data = {
|
|
||||||
title,
|
|
||||||
content
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
await articleApi.postArticle(data);
|
|
||||||
await this.$router.push('/articles');
|
|
||||||
} catch (err) {
|
|
||||||
alert('문제가 발생하였습니다.');
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
},
|
},
|
||||||
async update(evt) {
|
async update(evt) {
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
|
|
||||||
const id = this.$route.query.id;
|
const id = this.$route.query.id;
|
||||||
|
|
||||||
const {title, content} = this;
|
const {title, content} = this;
|
||||||
const data = {
|
await articleService.updateArticle(id, {title, content});
|
||||||
title,
|
|
||||||
content
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
await articleApi.updateArticle(id, data);
|
|
||||||
await this.$router.push('/articles');
|
|
||||||
} catch (err) {
|
|
||||||
alert('문제가 발생하였습니다.');
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import authApi from "../../api/authApi";
|
import authApi from "../../api/authApi";
|
||||||
|
import authService from "../../services/authService";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "Register",
|
name: "Register",
|
||||||
@@ -24,6 +25,8 @@
|
|||||||
beforeCreate() {
|
beforeCreate() {
|
||||||
authApi.register = authApi.register.bind(this);
|
authApi.register = authApi.register.bind(this);
|
||||||
authApi.session = authApi.session.bind(this);
|
authApi.session = authApi.session.bind(this);
|
||||||
|
|
||||||
|
authService.register = authService.register.bind(this);
|
||||||
},
|
},
|
||||||
async created() {
|
async created() {
|
||||||
try {
|
try {
|
||||||
@@ -37,13 +40,15 @@
|
|||||||
register: async function(evt) {
|
register: async function(evt) {
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
const { email, name, password } = this;
|
const { email, name, password } = this;
|
||||||
try {
|
await authService.register({email, name, password});
|
||||||
await authApi.register({email, name, password});
|
|
||||||
} catch (err) {
|
// try {
|
||||||
if (err.response.status === 409) {
|
// await authApi.register({email, name, password});
|
||||||
alert('이미 존재하는 이메일입니다.');
|
// } catch (err) {
|
||||||
}
|
// if (err.response.status === 409) {
|
||||||
}
|
// alert('이미 존재하는 이메일입니다.');
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import articleApi from "../api/articleApi";
|
import articleApi from "../api/articleApi";
|
||||||
import commonUtil from "../utils/commonUtil";
|
import commonUtil from "../utils/commonUtil";
|
||||||
|
import authApi from "../api/authApi";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
async getArticles({page = 0, size = 10}) {
|
async getArticles({page = 0, size = 10}) {
|
||||||
@@ -30,5 +31,44 @@ export default {
|
|||||||
alert('문제가 발생하였습니다.');
|
alert('문제가 발생하였습니다.');
|
||||||
console.log(e);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import authApi from "../api/authApi";
|
import authApi from "../api/authApi";
|
||||||
import authentication from "../middlewares/authentication";
|
|
||||||
import commonUtil from "../utils/commonUtil";
|
import commonUtil from "../utils/commonUtil";
|
||||||
|
|
||||||
export default {
|
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() {
|
async logout() {
|
||||||
try {
|
try {
|
||||||
this.$cookie.set('accessToken', null, 0);
|
this.$cookie.set('accessToken', null, 0);
|
||||||
@@ -33,7 +43,7 @@ export default {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.');
|
alert('토큰이 존재하지 않거나 유효하지 않은 토큰입니다.');
|
||||||
await this.$router.replace('/');
|
await this.$router.replace('/');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.example.vue.domain.auth;
|
|||||||
|
|
||||||
import com.example.vue.domain.user.UserResponseDto;
|
import com.example.vue.domain.user.UserResponseDto;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -22,6 +23,7 @@ public class AuthController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/register")
|
@PostMapping(value = "/register")
|
||||||
|
@Transactional
|
||||||
public UserResponseDto register(@RequestBody @Valid RegisterRequestDto registerRequestDto) {
|
public UserResponseDto register(@RequestBody @Valid RegisterRequestDto registerRequestDto) {
|
||||||
return authService.register(registerRequestDto);
|
return authService.register(registerRequestDto);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ public class User {
|
|||||||
this.password = registerRequestDto.getPassword();
|
this.password = registerRequestDto.getPassword();
|
||||||
this.email = registerRequestDto.getEmail();
|
this.email = registerRequestDto.getEmail();
|
||||||
this.name = registerRequestDto.getName();
|
this.name = registerRequestDto.getName();
|
||||||
|
this.createdAt = LocalDateTime.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
38
src/test/java/com/example/vue/user/AuthServiceTest.java
Normal file
38
src/test/java/com/example/vue/user/AuthServiceTest.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user