Skip to content

Commit

Permalink
Merge pull request #112 from UMC-CommonPlant/main
Browse files Browse the repository at this point in the history
[Feat] token valid api
  • Loading branch information
uhyunglee authored May 4, 2024
2 parents f70207a + 2f88fe5 commit 6d53af8
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ public class AlarmController {
@PostMapping("/api/fcm")
public ResponseEntity<ErrorResponse> pushMessage(@RequestBody FCMMessageTestDto fcmTestDto) throws IOException {

// log.info(fcmTestDto.getTargetToken() + " " +fcmTestDto.getTitle() + " " + fcmTestDto.getBody());
log.info(fcmTestDto.getTitle() + " " + fcmTestDto.getBody());
log.info(fcmTestDto.getTargetToken() + " " +fcmTestDto.getTitle() + " " + fcmTestDto.getBody());
// log.info(fcmTestDto.getTitle() + " " + fcmTestDto.getBody());

fcmService.sendMessageTo(
//fcmTestDto.getTargetToken(),
fcmTestDto.getTargetToken(),
fcmTestDto.getTitle(),
fcmTestDto.getBody());
fcmTestDto.getBody()
);

return ResponseEntity.ok().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static class Message {
private Notification notification;
private String token;
private Data data;
private Apns apns;
}

/**
Expand Down Expand Up @@ -55,4 +56,28 @@ public static class Data {
private String description;
}

/**
* iOS 알림 추가 설정: 소리 등등
*/
@Builder
@AllArgsConstructor
@Getter
public static class Aps {
private String sound;
}

@Builder
@AllArgsConstructor
@Getter
public static class Payload {
private Aps aps;
}

@Builder
@AllArgsConstructor
@Getter
public static class Apns {
private Payload payload;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public class FCMMessageTestDto {

private String title;
private String body;
// private String targetToken;
private String targetToken;

public FCMMessageTestDto(){

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,23 @@ private String getAccessToken() throws IOException {
* @return
* @throws JsonProcessingException
*/
// private String makeMessage(String targetToken, String title, String body) throws JsonProcessingException {
private String makeMessage(String title, String body) throws JsonProcessingException {
private String makeMessage(String targetToken, String title, String body) throws JsonProcessingException {
// private String makeMessage(String title, String body) throws JsonProcessingException {
FCMMessageDto fcmMessage = FCMMessageDto.builder()
.message(FCMMessageDto.Message.builder()
//.token(targetToken)
.token(targetToken)
.notification(FCMMessageDto.Notification.builder()
.title(title)
.body(body)
.image(null)
.build()
)
.build()
)
.apns(FCMMessageDto.Apns.builder()
.payload(FCMMessageDto.Payload.builder()
.aps(FCMMessageDto.Aps.builder().sound("default").build())
.build())
.build())
.build())
.validate_only(false)
.build();

Expand All @@ -81,10 +85,10 @@ private String makeMessage(String title, String body) throws JsonProcessingExcep
* @param body
* @throws IOException
*/
// public void sendMessageTo(String targetToken, String title, String body) throws IOException {
public void sendMessageTo(String title, String body) throws IOException {
// String message = makeMessage(targetToken, title, body);
String message = makeMessage(title, body);
public void sendMessageTo(String targetToken, String title, String body) throws IOException {
// public void sendMessageTo(String title, String body) throws IOException {
String message = makeMessage(targetToken, title, body);
// String message = makeMessage(title, body);

OkHttpClient client = new OkHttpClient();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.umc.commonplant.domain.oauth.controller;

import com.umc.commonplant.domain.Jwt.JwtService;
import com.umc.commonplant.domain.oauth.service.OAuthService;
import com.umc.commonplant.global.dto.JsonResponse;
import com.umc.commonplant.global.exception.BadRequestException;
import com.umc.commonplant.global.exception.ErrorResponseStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
Expand All @@ -15,6 +18,7 @@
@RestController
public class OAuthController implements OAuthSwagger{
private final OAuthService oAuthService;
private final JwtService jwtService;

@GetMapping("/login/{provider}")
public ResponseEntity<JsonResponse> login(@RequestParam("accessToken") String accessToken, @PathVariable String provider){
Expand All @@ -27,4 +31,14 @@ public ResponseEntity<JsonResponse> login(@RequestParam("accessToken") String ac
return ResponseEntity.ok(new JsonResponse(true, 2001, "no User Info", email));
}
}

@GetMapping("/api/token")
public ResponseEntity<JsonResponse> validToken(){
String token = jwtService.getJwt();
boolean isValid = jwtService.validateToken(token);

return isValid ?
ResponseEntity.ok(new JsonResponse(true, 200, "유효한 토큰입니다", isValid))
: ResponseEntity.ok(new JsonResponse(false, 2004, "토큰을 확인하세요", null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ public interface OAuthSwagger {
@Parameter(name = "accessToken", description = "OAuth 서버가 발급한 액세스토큰", required = true)
@Parameter(name = "provider", description = "소셜로그인", example = "kakao", required = true)
public ResponseEntity<JsonResponse> login(@RequestParam("accessToken") String accessToken, @PathVariable String provider);

@Operation(summary = "토큰 유효성 검증", description = "토큰 유효성 검증")
public ResponseEntity<JsonResponse> validToken();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static class join{
private String name;
@Schema(description = "소셜로그인", example = "kakao")
private String provider;
@Schema(description = "회원번호", example = "0123456789")
private String providerId;
}
@NotBlank(message = "사용할 이름을 입력해주세요.")
@Pattern(regexp = "^[가-힣a-zA-Z0-9]{2,10}$" , message = "이름은 특수문자를 포함하지 않은 2~10자리여야 합니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ public class User extends BaseTime {
private String uuid;

@Builder
public User(String name, String email, String provider, String imgUrl, String uuid){
public User(String name, String email, String provider, String providerId, String imgUrl, String uuid){
this.name = name;
this.email = email;
this.provider = provider;
this.providerId = providerId;
this.imgUrl = imgUrl;
this.uuid = uuid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByname(String name);
Optional<User> findByEmail(String email);
Optional<User> findByUuid(String uuid);
Optional<User> findByProviderId(String providerId);

@Query("select u from User u where u.email=?1 and u.provider=?2")
User findByEmail(String email, String loginType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public class UserService {
public User getUser(String uuid){ // User 조회
return userRepository.findByUuid(uuid).orElseThrow(() -> new BadRequestException((NOT_FOUND_USER)));
}

public User getUserByProviderId(String providerId){
return userRepository.findByProviderId(providerId).orElseThrow(() -> new BadRequestException(NOT_FOUND_USER));
}

public User saveUser(UserDto.join req){
User user = User.builder()
.name(req.getName())
Expand All @@ -48,8 +53,9 @@ public String joinUser(UserDto.join req, MultipartFile image){
.imgUrl(imageUrl)
.uuid(uuid)
.email(req.getEmail())
.provider(req.getProvider()).
build();
.provider(req.getProvider())
.providerId(req.getProviderId())
.build();
userRepository.save(user);

return jwtService.createToken(user.getUuid());
Expand Down

0 comments on commit 6d53af8

Please sign in to comment.