Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
로그인 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Jan 29, 2024
1 parent ccf63a7 commit 983e169
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main/java/com/example/daemawiki/domain/auth/service/Login.java
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())));
}


}

0 comments on commit 983e169

Please sign in to comment.