From 983e16926bc4f20cf6e29e7857e0f6a560386a7c Mon Sep 17 00:00:00 2001 From: 101 Date: Mon, 29 Jan 2024 13:06:18 +0900 Subject: [PATCH] =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../daemawiki/domain/auth/service/Login.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/main/java/com/example/daemawiki/domain/auth/service/Login.java diff --git a/src/main/java/com/example/daemawiki/domain/auth/service/Login.java b/src/main/java/com/example/daemawiki/domain/auth/service/Login.java new file mode 100644 index 00000000..ede5da97 --- /dev/null +++ b/src/main/java/com/example/daemawiki/domain/auth/service/Login.java @@ -0,0 +1,38 @@ +package com.example.daemawiki.domain.auth.service; + +import com.example.daemawiki.domain.auth.dto.LoginRequest; +import com.example.daemawiki.domain.auth.dto.LoginResponse; +import com.example.daemawiki.domain.user.repository.UserRepository; +import com.example.daemawiki.global.exception.PasswordMismatchException; +import com.example.daemawiki.global.exception.UserNotFoundException; +import com.example.daemawiki.global.security.Tokenizer; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Mono; + +@Service +public class Login { + private final UserRepository userRepository; + private final PasswordEncoder passwordEncoder; + private final Tokenizer tokenizer; + + public Login(UserRepository userRepository, PasswordEncoder passwordEncoder, Tokenizer tokenizer) { + this.userRepository = userRepository; + this.passwordEncoder = passwordEncoder; + this.tokenizer = tokenizer; + } + + public Mono execute(LoginRequest request) { + return userRepository.findByEmail(request.email()) + .switchIfEmpty(Mono.error(UserNotFoundException.EXCEPTION)) + .flatMap(user -> Mono.just(user) + .filter(u -> passwordEncoder.matches(u.getPassword(), request.password())) + .switchIfEmpty(Mono.error(PasswordMismatchException.EXCEPTION)) + .flatMap(u -> tokenizer.createToken(u.getEmail()) + .map(token -> LoginResponse.builder() + .token(token) + .build()))); + } + + +}