Skip to content

Commit

Permalink
Merge branch 'feat/auth' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wwingyou committed Jan 21, 2025
2 parents 5dd3ba8 + f066e5c commit b0b007f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
9 changes: 0 additions & 9 deletions src/main/java/com/goolbitg/api/security/AuthUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,4 @@ public static String getLoginUserId() {
}
return id;
}

public static UserDetails createUserDetails(String userId) {
UserDetails details = org.springframework.security.core.userdetails.User
.withUsername(userId)
.password("")
.authorities(List.of())
.build();
return details;
}
}
46 changes: 41 additions & 5 deletions src/main/java/com/goolbitg/api/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.goolbitg.api.service;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.time.LocalDate;
import java.util.Optional;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goolbitg.api.controller.UserController;
import com.goolbitg.api.entity.User;
import com.goolbitg.api.entity.UserStats;
import com.goolbitg.api.entity.UserSurvey;
Expand Down Expand Up @@ -106,18 +112,49 @@ public LoginResponseDto login(AuthRequestDto request) {
throw UserException.userNotExist(jwt.getSubject());
}
User user = result.get();
UserSurvey survey = userSurveyRepository.findById(user.getId())
.orElseThrow();

UserDetails details = AuthUtil.createUserDetails(user.getId());

String accessToken = jwtManager.create(details);
String accessToken = jwtManager.create(user.getId());
String refreshToken = createRefreshToken(user.getId());

LoginResponseDto dto = new LoginResponseDto();
dto.setAccessToken(accessToken);
dto.setRefreshToken(refreshToken);

Link nextLink = getNextRegisterLink(user, survey);
dto.setRegisterComplete(nextLink == null);
if (nextLink != null)
dto.add(nextLink);

return dto;
}

private Link getNextRegisterLink(User user, UserSurvey survey) {
WebMvcLinkBuilder builder = null;
try {
if (user.getNickname() == null)
builder = linkTo(methodOn(UserController.class).postUserInfo(null));
else if (survey.getCheck1() == null)
builder = linkTo(methodOn(UserController.class).postUserInfo(null));
else if (survey.getAvgIncomePerMonth() == null)
builder = linkTo(methodOn(UserController.class).postUserInfo(null));
} catch (Exception e) {
log.error("getNextRegisterLink() falied.");
}

if (builder != null)
return builder.withRel("next").withType("POST");

return null;
}

private Boolean validateRegisterComplete(User user, UserSurvey survey) {
return user.getNickname() != null &&
survey.getCheck1() != null &&
survey.getAvgIncomePerMonth() != null;
}

@Override
@Transactional
public void register(AuthRequestDto request) {
Expand Down Expand Up @@ -157,8 +194,7 @@ public AuthResponseDto getAccessToken(TokenRefreshRequestDto request) {
throw AuthException.tokenExpired(refreshToken);
}
String userId = result.get();
UserDetails details = AuthUtil.createUserDetails(userId);
String accessToken = jwtManager.create(details);
String accessToken = jwtManager.create(userId);

AuthResponseDto dto = new AuthResponseDto();
dto.setAccessToken(accessToken);
Expand Down
32 changes: 4 additions & 28 deletions src/main/resources/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1800,8 +1800,6 @@ components:
registerComplete:
type: boolean
description: 정보등록 완료 여부
nextLinkDto:
$ref: '#/components/schemas/LinkDto'
required:
- registerComplete
UserInfoDto:
Expand Down Expand Up @@ -1882,28 +1880,6 @@ components:
properties:
duplicated:
type: boolean
LinkDto:
type: object
properties:
required:
type: boolean
description: 필수 여부
method:
type: string
enum:
- get
- post
- put
- delete
description: 요청 메소드
url:
type: string
format: uri
description: 요청 주소
example:
required: true
method: post
url: 'http://localhost:8080/api/v1/users/me/info'
examples:
PrivateChallenge:
value:
Expand Down Expand Up @@ -2022,13 +1998,13 @@ components:
registerComplete: true
LoginResponse_InProgress:
value:
_links:
next:
href: "http://localhost:8080/v1/users/me/info"
type: "POST"
accessToken: token
refreshToken: token
registerComplete: false
nextLinkDto:
required: true
method: post
url: http://localhost:8080/api/v1/users/me/info
ChallengeGroup_Form_Normal:
value:
ownerId: id0001
Expand Down

0 comments on commit b0b007f

Please sign in to comment.