Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] token valid api #112

Merged
merged 6 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading