-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
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
56 changes: 56 additions & 0 deletions
56
...in/java/com/mutbook/week4_mission/app/domain/member/controller/ApiV1MemberController.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,56 @@ | ||
package com.mutbook.week4_mission.app.domain.member.controller; | ||
|
||
import com.mutbook.week4_mission.app.base.dto.RsData; | ||
import com.mutbook.week4_mission.app.domain.member.dto.LoginDto; | ||
import com.mutbook.week4_mission.app.domain.member.entity.Member; | ||
import com.mutbook.week4_mission.app.domain.member.service.MemberService; | ||
import com.mutbook.week4_mission.util.Util; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
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; | ||
|
||
import javax.validation.Valid; | ||
|
||
@RestController | ||
@RequestMapping("api/v1/member") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Tag(name = "ApiV1MemberController", description = "로그인 기능과 로그인 된 회원의 정보를 제공 기능을 담당하는 컨트롤러") | ||
public class ApiV1MemberController { | ||
private final MemberService memberService; | ||
private final PasswordEncoder passwordEncoder; | ||
|
||
@PostMapping("/login") | ||
public ResponseEntity<RsData> login(@Valid @RequestBody LoginDto loginDto) { | ||
Member member = memberService.findByUsername(loginDto.getUsername()).orElse(null); | ||
|
||
if (member == null) { | ||
return Util.spring.responseEntityOf(RsData.of("F-2", "일치하는 회원이 존재하지 않습니다.")); | ||
} | ||
|
||
if (passwordEncoder.matches(loginDto.getPassword(), member.getPassword()) == false) { | ||
return Util.spring.responseEntityOf(RsData.of("F-3", "비밀번호가 일치하지 않습니다.")); | ||
} | ||
|
||
log.debug("Util.json.toStr(member.getAccessTokenClaims()) : " + Util.json.toStr(member.getAccessTokenClaims())); | ||
|
||
String accessToken = memberService.genAccessToken(member); | ||
|
||
return Util.spring.responseEntityOf( | ||
RsData.of( | ||
"S-1", | ||
"로그인 성공, Access Token을 발급합니다.", | ||
Util.mapOf( | ||
"accessToken", accessToken | ||
) | ||
), | ||
Util.spring.httpHeadersOf("Authentication", accessToken) | ||
); | ||
} | ||
} |