This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
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.
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/com/example/daemawiki/domain/auth/service/Login.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,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<LoginResponse> 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()))); | ||
} | ||
|
||
|
||
} |