Skip to content

Commit

Permalink
Merge pull request #8 from Likelion-Inner-Join/feat/login
Browse files Browse the repository at this point in the history
[Feat] applicant 로그인 api 추가
  • Loading branch information
gorapang authored Dec 1, 2024
2 parents f7fb923 + b8681fc commit 833bfd4
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum ErrorCode {

//valid
VALID_ERROR(false, HttpStatus.BAD_REQUEST.value(), "형식이 잘못되었습니다."),
APPLICANT_NOT_FOUNT(false, HttpStatus.UNAUTHORIZED.value(), "이메일 또는 비밀번호가 잘못되었습니다"),

//error
INTERNAL_SERVER_ERROR(false,HttpStatus.INTERNAL_SERVER_ERROR.value(), "서버 내부에서 문제가 발생했습니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public CommonResponse(T result) {

// 오류 발생 (result 없음)
public CommonResponse(ErrorCode errorCode) {
this.isSuccess = ErrorCode.SUCCESS.getIsSuccess();
this.isSuccess = errorCode.getIsSuccess();
this.code = errorCode.getCode();
this.message = errorCode.getMessage();
}

// 오류 발생
public CommonResponse(ErrorCode errorCode, T result) {
this.isSuccess = ErrorCode.SUCCESS.getIsSuccess();
this.isSuccess = errorCode.getIsSuccess();
this.code = errorCode.getCode();
this.message = errorCode.getMessage();
this.result = result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.likelion.innerjoin.user.controller;


import com.likelion.innerjoin.common.response.CommonResponse;
import com.likelion.innerjoin.user.model.dto.request.LoginRequestDto;
import com.likelion.innerjoin.user.model.dto.response.LoginResponseDto;
import com.likelion.innerjoin.user.service.LoginService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Slf4j
public class LoginController {
private final LoginService loginService;

@PostMapping("/login")
public CommonResponse<LoginResponseDto> login(@RequestBody LoginRequestDto loginRequestDto, HttpSession session) {
return new CommonResponse<>(loginService.login(loginRequestDto, session));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.likelion.innerjoin.user.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class ApplicantNotFoundException extends RuntimeException {
private String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.likelion.innerjoin.user.exception;

import com.likelion.innerjoin.common.exception.ErrorCode;
import com.likelion.innerjoin.common.response.CommonResponse;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class LoginExceptionHandler {
@ExceptionHandler(ApplicantNotFoundException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public CommonResponse<?> applicantNotFound(ApplicantNotFoundException e, HttpServletRequest request) {
log.warn("APPLICANT-001> 요청 URI: " + request.getRequestURI() + ", 에러 메세지: " + e.getMessage());
return new CommonResponse<>(ErrorCode.APPLICANT_NOT_FOUNT);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.likelion.innerjoin.user.model.dto.request;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class LoginRequestDto {
private String email;
private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.likelion.innerjoin.user.model.dto.response;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class LoginResponseDto {
String studentNumber;
String email;
String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.likelion.innerjoin.user.repository;

import com.likelion.innerjoin.user.model.entity.Applicant;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ApplicantRepository extends JpaRepository<Applicant, Long> {
Applicant findByEmailAndPassword(String email, String password);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.likelion.innerjoin.user.service;


import com.likelion.innerjoin.user.exception.ApplicantNotFoundException;
import com.likelion.innerjoin.user.model.dto.request.LoginRequestDto;
import com.likelion.innerjoin.user.model.dto.response.LoginResponseDto;
import com.likelion.innerjoin.user.model.entity.Applicant;
import com.likelion.innerjoin.user.repository.ApplicantRepository;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.NoSuchElementException;

@Service
@RequiredArgsConstructor
public class LoginService {
public final ApplicantRepository applicantRepository;

public LoginResponseDto login(LoginRequestDto loginRequestDto, HttpSession session) {
try{
Applicant applicant =
applicantRepository.findByEmailAndPassword(
loginRequestDto.getEmail(),
loginRequestDto.getPassword()
);

if(applicant == null) {throw new ApplicantNotFoundException("아이디나 비밀번호가 잘못되었습니다.");}

session.setAttribute("userId", applicant.getId());
session.setAttribute("role", "applicant");
return new LoginResponseDto(applicant.getStudentNumber(), applicant.getEmail(), applicant.getName());
}catch (NoSuchElementException e){
throw new ApplicantNotFoundException("아이디나 비밀번호가 잘못되었습니다.");
}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ spring:
hibernate:
default_batch_fetch_size: 500
format_sql: true
server:
servlet:
session:
timeout: 30m

0 comments on commit 833bfd4

Please sign in to comment.