Skip to content

Commit f507768

Browse files
committed
(TP-77) feat: add registration and code-request return
1 parent 0b733e6 commit f507768

3 files changed

Lines changed: 41 additions & 6 deletions

File tree

rentplace/src/main/java/kattsyn/dev/rentplace/controllers/AuthController.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@RestController
1515
@RequestMapping("${api.path}/auth")
1616
@RequiredArgsConstructor
17-
@Tag(name = "Аутентификация", description = "Для аутентификации, регистрации, обновлении и запроса токенов и отправки запросов кода на почту.")
17+
@Tag(name = "Authentication", description = "Для аутентификации, регистрации, обновлении и запроса токенов и отправки запросов кода на почту.")
1818
public class AuthController {
1919

2020
private final AuthService authService;
@@ -25,9 +25,8 @@ public class AuthController {
2525
summary = "Запросить код по почте",
2626
description = "Запрос на получение кода авторизации по почте"
2727
)
28-
public ResponseEntity<JwtResponse> login(@RequestBody CodeRequest codeRequest) {
29-
verificationCodeService.generateAndSendCode(codeRequest.getEmail());
30-
return ResponseEntity.ok().build();
28+
public ResponseEntity<CodeResponse> requestCode(@RequestBody CodeRequest codeRequest) {
29+
return ResponseEntity.ok(verificationCodeService.generateAndSendCode(codeRequest.getEmail()));
3130
}
3231

3332
@Operation(
@@ -68,6 +67,32 @@ public ResponseEntity<JwtResponse> login(@RequestBody JwtRequest authRequest/*,
6867
.body(tokens);
6968
}
7069

70+
@Operation(
71+
summary = "Запрос на регистрацию",
72+
description = "Получает email и код с почты, а также имя и фамилию пользователя. Возвращает JWT токены"
73+
)
74+
@PostMapping("/register")
75+
public ResponseEntity<JwtResponse> register(@RequestBody RegisterRequest registerRequest/*,
76+
HttpServletResponse response*/) throws AuthException {
77+
JwtResponse tokens = authService.register(registerRequest);
78+
79+
return ResponseEntity.ok()
80+
.body(tokens);
81+
}
82+
83+
@Operation(
84+
summary = "Проверка валидности введенного кода пользователем",
85+
description = "Проверяет правильность введенного кода, отправленного на почту. Использовать в случае, если пользователь новый."
86+
)
87+
@PostMapping("/validate-code")
88+
public ResponseEntity<Void> checkCode(@RequestBody JwtRequest authRequest/*,
89+
HttpServletResponse response*/) throws AuthException {
90+
authService.validateCode(authRequest);
91+
return ResponseEntity.ok().build();
92+
}
93+
94+
95+
7196
@Operation(
7297
summary = "Запрос на обновление AccessToken'а",
7398
description = "Получает RefreshToken, возвращает новый AccessToken"

rentplace/src/main/java/kattsyn/dev/rentplace/services/VerificationCodeService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package kattsyn.dev.rentplace.services;
22

3+
import kattsyn.dev.rentplace.dtos.CodeResponse;
4+
35
public interface VerificationCodeService {
46
boolean validateCode(String email, String code);
57

6-
void generateAndSendCode(String email);
8+
CodeResponse generateAndSendCode(String email);
79

810
String generateCode();
911

rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/VerificationCodeServiceImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package kattsyn.dev.rentplace.services.impl;
22

3+
import kattsyn.dev.rentplace.dtos.CodeResponse;
34
import kattsyn.dev.rentplace.entities.VerificationCode;
5+
import kattsyn.dev.rentplace.enums.AuthType;
46
import kattsyn.dev.rentplace.exceptions.NotFoundException;
57
import kattsyn.dev.rentplace.repositories.VerificationCodeRepository;
68
import kattsyn.dev.rentplace.services.EmailService;
9+
import kattsyn.dev.rentplace.services.UserService;
710
import kattsyn.dev.rentplace.services.VerificationCodeService;
811
import lombok.AllArgsConstructor;
912
import lombok.extern.slf4j.Slf4j;
@@ -19,6 +22,7 @@ public class VerificationCodeServiceImpl implements VerificationCodeService {
1922

2023
private final VerificationCodeRepository verificationCodeRepository;
2124
private final EmailService emailService;
25+
private final UserService userService;
2226

2327
@Override
2428
public boolean validateCode(String email, String code) {
@@ -29,7 +33,7 @@ public boolean validateCode(String email, String code) {
2933
}
3034

3135
@Override
32-
public void generateAndSendCode(String email) {
36+
public CodeResponse generateAndSendCode(String email) {
3337
log.info("Generating verification code for email {}", email);
3438
String code = generateCode();
3539

@@ -41,7 +45,11 @@ public void generateAndSendCode(String email) {
4145
verificationCodeRepository.save(verificationCode);
4246
log.info("Generated verification code {}", verificationCode.getCode());
4347

48+
CodeResponse codeResponse = new CodeResponse(userService.existsByEmail(email) ? AuthType.AUTH_LOGIN : AuthType.AUTH_REGISTER);
49+
4450
emailService.sendVerificationCode(email, code);
51+
52+
return codeResponse;
4553
}
4654

4755
@Override

0 commit comments

Comments
 (0)