Skip to content

Commit

Permalink
Merge pull request #37 from Donut-DONationUTile/feature/auth/giver
Browse files Browse the repository at this point in the history
fix: giver token 오류 해결
  • Loading branch information
Ganghee-Lee-0522 authored Feb 18, 2024
2 parents 78c7dd0 + bfc0a69 commit f33c81a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/java/zero/eight/donut/config/jwt/AuthUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zero.eight.donut.config.jwt;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -14,6 +15,7 @@

import java.util.Collection;

@Slf4j
@RequiredArgsConstructor
@Component
public class AuthUtils {
Expand All @@ -24,6 +26,8 @@ public class AuthUtils {
private final ReceiverRepository receiverRepository;

public Giver getGiver() {
log.info("사용자 이메일 -> {}", getCurrentUserEmail());
log.info("사용자 -> {}", giverRepository.findByEmail(getCurrentUserEmail()));
return giverRepository.findByEmail(getCurrentUserEmail()).get();
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/zero/eight/donut/config/jwt/JwtUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.jsonwebtoken.*;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand All @@ -21,6 +22,7 @@
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@Component
public class JwtUtils {
Expand Down Expand Up @@ -88,6 +90,8 @@ public boolean validateToken(String token) {
}
try {
Claims claims = Jwts.parser().setSigningKey(jwtSecretKey).parseClaimsJws(token).getBody();
log.info("token \"role\" : " + claims.get("role"));
log.info("token \"name\" : " + claims.get("name"));
return true;
} catch (MalformedJwtException e) {
throw new UnauthorizedException(Error.INVALID_JWT_EXCEPTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zero.eight.donut.common.response.ApiResponse;
import zero.eight.donut.service.AuthService;
import zero.eight.donut.service.HistoryGiverService;

import java.time.LocalDateTime;
Expand All @@ -16,6 +17,7 @@
@RequestMapping("/api/history/giver")
public class HistoryGiverController {
private final HistoryGiverService historyGiverService;
private final AuthService authService;

@GetMapping("/info/{donateDate}")
public ApiResponse<?> getDonationList(@PathVariable("donateDate")LocalDateTime donateDate){
Expand All @@ -26,4 +28,9 @@ public ApiResponse<?> getDonationList(@PathVariable("donateDate")LocalDateTime d
public ApiResponse<?> getDonationDetail(@PathVariable("giftId")Long giftId){
return historyGiverService.getDonationDetail(giftId);
}

@GetMapping("/giver/get")
public ApiResponse<?> getGiverEntity() {
return authService.getGiverEntity();
}
}
9 changes: 8 additions & 1 deletion src/main/java/zero/eight/donut/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zero.eight.donut.common.response.ApiResponse;
import zero.eight.donut.config.jwt.AuthUtils;
import zero.eight.donut.config.jwt.JwtUtils;
import zero.eight.donut.domain.Benefit;
import zero.eight.donut.domain.Donation;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class AuthService {
private final DonationRepository donationRepository;
private final PasswordEncoder bCryptPasswordEncoder;
private final JwtUtils jwtUtils;
private final AuthUtils authUtils;

@Transactional
public ApiResponse<?> googleSignIn(String idToken) {
Expand Down Expand Up @@ -80,7 +82,7 @@ public ApiResponse<?> googleSignIn(String idToken) {
giver = giverRepository.findByEmail(email);

MemberDto member = MemberDto.builder()
.name(giver.get().getName())
.name(giver.get().getEmail())
.role(Role.ROLE_GIVER)
.build();

Expand Down Expand Up @@ -452,4 +454,9 @@ private Boolean tokenVerifier(GoogleIdToken.Payload payload) {

return true;
}

// for giver token error test
public ApiResponse<?> getGiverEntity() {
return ApiResponse.success(authUtils.getGiver());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public ApiResponse<?> getDonationList(LocalDateTime donateDate){
}
Giver giver = authUtils.getGiver();
log.info("기부자 정보 확인 -> {}", giver.getName());
log.info("giver email -> {}", giver.getEmail());

//기부한 기간 계산
Integer period = Period.between(giver.getCreatedAt().toLocalDate(), LocalDate.now()).getYears();
Expand Down

0 comments on commit f33c81a

Please sign in to comment.