-
Notifications
You must be signed in to change notification settings - Fork 0
Add register service #18
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package ru.scamburger.Soundger.controllers; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import ru.scamburger.Soundger.dto.UserCredentialsRequestDto; | ||
| import ru.scamburger.Soundger.service.RegisterService; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api") | ||
| public class RegisterServiceController { | ||
|
|
||
| private final RegisterService registerService; | ||
|
|
||
| public RegisterServiceController(RegisterService registerService) { | ||
Listanuv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.registerService = registerService; | ||
| } | ||
|
|
||
| @PostMapping("/register") | ||
| @Transactional | ||
| public ResponseEntity<String> register(@RequestBody UserCredentialsRequestDto userDto) { | ||
| if (registerService.doRegister(userDto.getUsername(), userDto.getPassword())) { | ||
| return new ResponseEntity<>("User create", HttpStatus.OK); | ||
| } else { | ||
| return new ResponseEntity<>("Username exist on server", HttpStatus.CONFLICT); | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. при удачной регистрации нужно вернуть в ответе новоиспеченного юзера (в json) нужна будет новая dto, т.к. нельзя отдавай сущность из базы as-is (как есть), потому что там может быть секретная инфа (по типу пароля, например)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ладно
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bubuntoid
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. давай логин, айди и токен |
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package ru.scamburger.Soundger.service; | ||
|
|
||
| public interface RegisterService { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UserService
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ок |
||
| boolean doRegister(String username, String password); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. возвращаемый тип должен быть User метод предлагаю переименовать в registerUser()
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ок |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package ru.scamburger.Soundger.service; | ||
|
|
||
| import org.jasypt.util.password.PasswordEncryptor; | ||
| import org.springframework.stereotype.Service; | ||
| import ru.scamburger.Soundger.dao.AuthTokenDao; | ||
| import ru.scamburger.Soundger.entity.AuthToken; | ||
| import ru.scamburger.Soundger.entity.User; | ||
|
|
||
| import java.util.Date; | ||
| import java.util.UUID; | ||
|
|
||
| @Service | ||
| public class RegisterServiceImpl implements RegisterService { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. переименовать в след за UserService
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ок |
||
|
|
||
| private final int tokenLifetimeInMilliseconds = 1000 * 60 * 60 * 24; | ||
| private final PasswordEncryptor passwordEncryptor; | ||
| private final AuthTokenDao authTokenDao; | ||
|
|
||
| public RegisterServiceImpl(AuthTokenDao authTokenDao, PasswordEncryptor passwordEncryptor) { | ||
| this.authTokenDao = authTokenDao; | ||
| this.passwordEncryptor = passwordEncryptor; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean doRegister(String username, String password) { | ||
| AuthToken authToken = new AuthToken(); | ||
| authToken.setToken(UUID.randomUUID().toString()); | ||
| authToken.setExpiredAt(new Date(new Date().getTime() + tokenLifetimeInMilliseconds)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. токен не нужен, т.к. юзер еще не авторизировался
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. он null должен быть в целом, тут по сути вообще никаких действий с этой сущностью (AuthToken) не должно быть
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Мб в данном случае сделать как везде?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. окей, тогда не забудь в новое дто добавить соответствующее поле authToken или просто token |
||
| User user = new User(); | ||
| user.setUsername(username); | ||
| user.setPassword(passwordEncryptor.encryptPassword(password)); | ||
| authToken.setUser(user); | ||
| try { | ||
| authTokenDao.saveAuthToken(authToken); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. почему сохраняется authToken а не user?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Верхний тред чекни
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это то понятно, но стремно выходит, что логически ты должен сохранить юзера, а вызывается метод на сохранение токена, давай сделаем userDao.saveUser, он уже в свою очередь токен за собой подтянет (точно так же как saveAuthToken подтягивает юзера) |
||
| return true; | ||
| } catch (Exception e) { | ||
| throw e; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/api/user
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ок