Skip to content

Commit

Permalink
feat: 토큰 발급 시 게스트 유저 자동 생성할 수 있게 함
Browse files Browse the repository at this point in the history
  • Loading branch information
umi0410 committed Apr 3, 2022
1 parent 1eae74a commit 645d868
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
public class LoginInput {
private String username;
private String password;
private Boolean createGuest;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.khumu.community.application.port.in;

import com.khumu.community.application.dto.LoginOutput;
import com.khumu.community.application.dto.UserDto;
import com.khumu.community.application.dto.input.CreateUserInput;
import com.khumu.community.application.dto.input.LoginInput;
import com.khumu.community.application.entity.User;
import com.khumu.community.application.port.out.repository.UserRepository;
Expand All @@ -18,17 +20,23 @@
@Service
@Slf4j
public class AuthService {
private final UserService userService;
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final JwtTokenProvider tokenProvider;

public LoginOutput issueToken(LoginInput body) throws UsernameNotFoundException {
User user = userRepository.findById(body.getUsername()).orElseThrow(WrongCredentialException::new);
if (!passwordEncoder.matches(body.getPassword(), user.getPassword())) {
throw new WrongCredentialException();
User user = null;
if (body.getCreateGuest() != null && body.getCreateGuest()) {
UserDto userDto = userService.create(CreateUserInput.builder().kind("guest").build());
user = userRepository.findById(userDto.getUsername()).get();
} else{
user = userRepository.findById(body.getUsername()).orElseThrow(WrongCredentialException::new);
if (!passwordEncoder.matches(body.getPassword(), user.getPassword())) {
throw new WrongCredentialException();
}
}


return LoginOutput.builder()
.access(tokenProvider.generateAccessToken(generateClaims(user)))
.build();
Expand Down

0 comments on commit 645d868

Please sign in to comment.