From 173c9da8df71730f1a1b020d75bc8fd70f89c74c Mon Sep 17 00:00:00 2001 From: tkdwns414 Date: Tue, 5 Mar 2024 11:55:40 +0900 Subject: [PATCH] [feat] add TestController for client test --- .../controller/TestController.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/org/pingle/pingleserver/controller/TestController.java diff --git a/src/main/java/org/pingle/pingleserver/controller/TestController.java b/src/main/java/org/pingle/pingleserver/controller/TestController.java new file mode 100644 index 0000000..b087d58 --- /dev/null +++ b/src/main/java/org/pingle/pingleserver/controller/TestController.java @@ -0,0 +1,32 @@ +package org.pingle.pingleserver.controller; + +import lombok.RequiredArgsConstructor; +import org.pingle.pingleserver.domain.enums.URole; +import org.pingle.pingleserver.repository.UserRepository; +import org.pingle.pingleserver.utils.JwtUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Profile; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@Profile({"local", "dev"}) +@RequestMapping("/test") +@RequiredArgsConstructor +public class TestController { + private final JwtUtil jwtUtil; + private final UserRepository userRepository; + + @Value("${token-test.key}") + private String key; + + @GetMapping("/token/{userId}") + public ResponseEntity getToken(@PathVariable("userId") Long userId, + @RequestHeader(value = "Test-Key", required = false) String testKey){ + if (!key.isBlank() && !key.equals(testKey)) { + return ResponseEntity.badRequest().body("Invalid test key"); + } + userRepository.findByIdOrThrow(userId); + return ResponseEntity.ok(jwtUtil.generateTokens(userId, URole.USER)); + } +}