From 4fb756b8ba8494f8f2b87d08866b35ee2f31ff53 Mon Sep 17 00:00:00 2001 From: adorableco Date: Fri, 11 Oct 2024 11:20:17 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EC=9E=84=EC=8B=9C=20=EC=9E=90=EC=B2=B4?= =?UTF-8?q?=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../phote/controller/AuthController.kt | 14 ++++++ .../com/swm_standard/phote/dto/AuthDtos.kt | 5 ++ .../phote/service/NativeAuthService.kt | 48 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/main/kotlin/com/swm_standard/phote/service/NativeAuthService.kt diff --git a/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt b/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt index 617d8b2..2e72be0 100644 --- a/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt +++ b/src/main/kotlin/com/swm_standard/phote/controller/AuthController.kt @@ -3,10 +3,12 @@ package com.swm_standard.phote.controller import com.swm_standard.phote.common.responsebody.BaseResponse import com.swm_standard.phote.dto.LoginRequest import com.swm_standard.phote.dto.MemberInfoResponse +import com.swm_standard.phote.dto.NativeLoginRequest import com.swm_standard.phote.dto.RenewAccessTokenResponse import com.swm_standard.phote.service.AppleAuthService import com.swm_standard.phote.service.GoogleAuthService import com.swm_standard.phote.service.KaKaoAuthService +import com.swm_standard.phote.service.NativeAuthService import com.swm_standard.phote.service.TokenService import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.tags.Tag @@ -25,6 +27,7 @@ class AuthController( private val kaKaoAuthService: KaKaoAuthService, private val appleAuthService: AppleAuthService, private val tokenService: TokenService, + private val nativeAuthService: NativeAuthService, ) { @Operation(summary = "google-login", description = "구글 로그인/회원가입") @PostMapping("/google-login") @@ -62,6 +65,17 @@ class AuthController( return BaseResponse(msg = message, data = memberInfo) } + @Operation(summary = "native-login", description = "자체 로그인") + @PostMapping("/native-login") + fun nativeLogin( + @RequestBody request: NativeLoginRequest, + ): BaseResponse { + val memberInfo = nativeAuthService.getMemberInfoFromNative(request) + + val message = if (memberInfo.isMember == false) "회원가입 성공" else "로그인 성공" + return BaseResponse(msg = message, data = memberInfo) + } + @Operation(summary = "renewAccessToken", description = "refreshToken으로 accessToken 갱신") @PostMapping("/refreshtoken") fun renewAccessToken( diff --git a/src/main/kotlin/com/swm_standard/phote/dto/AuthDtos.kt b/src/main/kotlin/com/swm_standard/phote/dto/AuthDtos.kt index 4155968..12710c8 100644 --- a/src/main/kotlin/com/swm_standard/phote/dto/AuthDtos.kt +++ b/src/main/kotlin/com/swm_standard/phote/dto/AuthDtos.kt @@ -35,3 +35,8 @@ data class LoginRequest( val code: String, val redirectUri: String, ) + +data class NativeLoginRequest( + val email: String, + val password: String, +) diff --git a/src/main/kotlin/com/swm_standard/phote/service/NativeAuthService.kt b/src/main/kotlin/com/swm_standard/phote/service/NativeAuthService.kt new file mode 100644 index 0000000..b0118c8 --- /dev/null +++ b/src/main/kotlin/com/swm_standard/phote/service/NativeAuthService.kt @@ -0,0 +1,48 @@ +package com.swm_standard.phote.service + +import com.swm_standard.phote.common.authority.JwtTokenProvider +import com.swm_standard.phote.common.exception.BadRequestException +import com.swm_standard.phote.common.exception.NotFoundException +import com.swm_standard.phote.dto.MemberInfoResponse +import com.swm_standard.phote.dto.NativeLoginRequest +import com.swm_standard.phote.repository.MemberRepository +import org.springframework.beans.factory.annotation.Value +import org.springframework.stereotype.Service + +@Service +class NativeAuthService( + private val memberRepository: MemberRepository, + private val jwtTokenProvider: JwtTokenProvider, + private val tokenService: TokenService, + @Value("\${native-login.password}") + private val password: String, +) { + fun getMemberInfoFromNative(request: NativeLoginRequest): MemberInfoResponse { + validatePassword(request) + + var member = memberRepository.findByEmail(request.email) ?: throw NotFoundException(fieldName = "member") + + val dto = + MemberInfoResponse( + accessToken = null, + name = member.name, + email = member.email, + picture = member.image, + isMember = true, + memberId = member.id, + ) + + dto.accessToken = jwtTokenProvider.createToken(member.id) + dto.refreshToken = tokenService.generateRefreshToken(member.id) + dto.memberId = member.id + dto.name = member.name + + return dto + } + + private fun validatePassword(request: NativeLoginRequest) { + if (request.password != password) { + throw BadRequestException(fieldName = "password", message = "비밀번호가 일치하지 않습니다.") + } + } +}