Skip to content

Commit

Permalink
fix: cors and response of login (#3)
Browse files Browse the repository at this point in the history
* fix: cors 설정

* fix: modify response field of login
  • Loading branch information
aiaiaiai1 authored Jun 29, 2024
1 parent 012e16d commit 0d07654
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
11 changes: 6 additions & 5 deletions src/main/java/gymmi/controller/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import gymmi.request.LoginRequest;
import gymmi.request.RegistrationRequest;
import gymmi.request.ReissueRequest;
import gymmi.response.TokensResponse;
import gymmi.response.LoginResponse;
import gymmi.response.TokenResponse;
import gymmi.service.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -27,14 +28,14 @@ public ResponseEntity<Void> registerUser(@Validated @RequestBody RegistrationReq
}

@PostMapping("/auth/welcome")
public ResponseEntity<TokensResponse> login(@Validated @RequestBody LoginRequest request) {
TokensResponse response = authService.login(request);
public ResponseEntity<LoginResponse> login(@Validated @RequestBody LoginRequest request) {
LoginResponse response = authService.login(request);
return ResponseEntity.ok().body(response);
}

@PostMapping("/auth/reissue")
public ResponseEntity<TokensResponse> reissue(@Validated @RequestBody ReissueRequest request) {
TokensResponse response = authService.reissue(request);
public ResponseEntity<TokenResponse> reissue(@Validated @RequestBody ReissueRequest request) {
TokenResponse response = authService.reissue(request);
return ResponseEntity.ok().body(response);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/gymmi/global/WebMvcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class WebMvcConfig implements WebMvcConfigurer {
public void addCorsMappings(final CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods(ALLOWED_METHOD_NAMES.split(","))
.allowedOrigins("*");
.allowedOriginPatterns("*")
.allowedHeaders("*")
.allowCredentials(true);
}

@Override
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/gymmi/response/LoginResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gymmi.response;

import lombok.Builder;
import lombok.Getter;

@Getter
public class LoginResponse {

private final Long userId;
private final String nickname;
private final String profileURL;
private final String accessToken;
private final String refreshToken;

@Builder
public LoginResponse(Long userId, String nickname, String profileURL, String accessToken, String refreshToken) {
this.userId = userId;
this.nickname = nickname;
this.profileURL = profileURL;
this.accessToken = accessToken;
this.refreshToken = refreshToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import lombok.Getter;

@Getter
public class TokensResponse {
public class TokenResponse {

private final String accessToken;
private final String refreshToken;

public TokensResponse(String accessToken, String refreshToken) {
public TokenResponse(String accessToken, String refreshToken) {
this.accessToken = accessToken;
this.refreshToken = refreshToken;
}
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/gymmi/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import gymmi.request.LoginRequest;
import gymmi.request.RegistrationRequest;
import gymmi.request.ReissueRequest;
import gymmi.response.TokensResponse;
import gymmi.response.LoginResponse;
import gymmi.response.TokenResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -39,18 +40,26 @@ public void registerUser(RegistrationRequest request) {
}

@Transactional
public TokensResponse login(LoginRequest request) {
public LoginResponse login(LoginRequest request) {
User user = userRepository.findByLoginId(request.getLoginId())
.orElseThrow(() -> new NotMatchedException("아이디와 비밀번호를 확인해 주세요."));

if (!user.canAuthenticate(request.getLoginId(), request.getPassword())) {
throw new NotMatchedException("아이디와 비밀번호를 확인해 주세요.");
}
return generateAndSaveTokensAbout(user);

TokenResponse tokenResponse = generateAndSaveTokensAbout(user);
return LoginResponse.builder()
.userId(user.getId())
.nickname(user.getNickname())
.profileURL("")
.refreshToken(tokenResponse.getRefreshToken())
.accessToken(tokenResponse.getAccessToken())
.build();
}

@Transactional
public TokensResponse reissue(ReissueRequest request) {
public TokenResponse reissue(ReissueRequest request) {
Long userId = tokenProcessor.parseRefreshToken(request.getRefreshToken());
User user = userRepository.getByUserId(userId);
Logined logined = loginedRepository.getByUserId(userId);
Expand All @@ -63,12 +72,12 @@ public TokensResponse reissue(ReissueRequest request) {
return generateAndSaveTokensAbout(user);
}

private TokensResponse generateAndSaveTokensAbout(User user) {
private TokenResponse generateAndSaveTokensAbout(User user) {
String accessToken = tokenProcessor.generateAccessToken(user.getId());
String refreshToken = tokenProcessor.generateRefreshToken(user.getId());
Logined logined = loginedRepository.getByUserId(user.getId());
logined.saveRefreshToken(refreshToken);
return new TokensResponse(accessToken, refreshToken);
return new TokenResponse(accessToken, refreshToken);
}

@Transactional
Expand Down

0 comments on commit 0d07654

Please sign in to comment.