-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] add TestController for client test
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/org/pingle/pingleserver/controller/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |