From fce9b33d23b617986b27168cfc6af40b7e8af3f7 Mon Sep 17 00:00:00 2001 From: hhhello Date: Sun, 6 Oct 2024 20:24:54 +0900 Subject: [PATCH] Fix signUp & signIn --- .../api/auth/AuthController.kt | 5 ++-- .../graduatingserver/api/auth/AuthService.kt | 8 +++---- .../api/auth/req/SignInReq.kt | 5 ---- .../api/auth/req/SignUpReq.kt | 4 ++++ Graduating-iOS/Data/Network/AuthService.swift | 20 ++++++++-------- .../Feature/EditProfile/EditProfileView.swift | 2 +- .../Onboarding/OnboardingFirstView.swift | 7 ++++++ .../Onboarding/OnboardingViewModel.swift | 24 +++++++++++++------ .../Feature/ViewModel/AppState.swift | 18 +++++++++----- Graduating-iOS/Model/Auth/AuthDTO.swift | 4 ++-- 10 files changed, 59 insertions(+), 38 deletions(-) diff --git a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthController.kt b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthController.kt index 4197355..a8b57f0 100644 --- a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthController.kt +++ b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthController.kt @@ -1,10 +1,9 @@ package com.bestswlkh0310.graduating.graduatingserver.api.auth -import com.bestswlkh0310.graduating.graduatingserver.api.auth.req.SignInReq -import com.bestswlkh0310.graduating.graduatingserver.api.auth.req.RefreshReq import com.bestswlkh0310.graduating.graduatingserver.api.auth.req.SignUpReq +import com.bestswlkh0310.graduating.graduatingserver.api.auth.req.RefreshReq +import com.bestswlkh0310.graduating.graduatingserver.api.auth.req.SignInReq import jakarta.validation.Valid -import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping diff --git a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthService.kt b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthService.kt index 7e56a09..086d2ef 100644 --- a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthService.kt +++ b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/AuthService.kt @@ -1,8 +1,8 @@ package com.bestswlkh0310.graduating.graduatingserver.api.auth -import com.bestswlkh0310.graduating.graduatingserver.api.auth.req.SignInReq -import com.bestswlkh0310.graduating.graduatingserver.api.auth.req.RefreshReq import com.bestswlkh0310.graduating.graduatingserver.api.auth.req.SignUpReq +import com.bestswlkh0310.graduating.graduatingserver.api.auth.req.RefreshReq +import com.bestswlkh0310.graduating.graduatingserver.api.auth.req.SignInReq import com.bestswlkh0310.graduating.graduatingserver.api.auth.res.TokenRes import com.bestswlkh0310.graduating.graduatingserver.core.school.SchoolRepository import com.bestswlkh0310.graduating.graduatingserver.core.school.getBy @@ -31,7 +31,7 @@ class AuthService( private val googleOAuth2Helper: GoogleOAuth2Helper, private val jwtClient: JwtClient, ) { - fun signUp(req: SignUpReq): TokenRes { + fun signIn(req: SignInReq): TokenRes { val email = this.getEmail( code = req.code, platformType = req.platformType @@ -41,7 +41,7 @@ class AuthService( return jwtClient.generate(user) } - fun signIn(req: SignInReq): TokenRes { + fun signUp(req: SignUpReq): TokenRes { val email = this.getEmail( code = req.code, platformType = req.platformType diff --git a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/req/SignInReq.kt b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/req/SignInReq.kt index f649479..445ff2e 100644 --- a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/req/SignInReq.kt +++ b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/req/SignInReq.kt @@ -1,13 +1,8 @@ package com.bestswlkh0310.graduating.graduatingserver.api.auth.req import com.bestswlkh0310.graduating.graduatingserver.core.user.PlatformType -import jakarta.validation.constraints.Size data class SignInReq( val platformType: PlatformType, val code: String, - @Size(min = 1, max = 24) - val nickname: String, - val graduatingYear: Int, - val schoolId: Long, ) \ No newline at end of file diff --git a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/req/SignUpReq.kt b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/req/SignUpReq.kt index 1bc3743..f48475f 100644 --- a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/req/SignUpReq.kt +++ b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/auth/req/SignUpReq.kt @@ -6,4 +6,8 @@ import jakarta.validation.constraints.Size data class SignUpReq( val platformType: PlatformType, val code: String, + @Size(min = 1, max = 24) + val nickname: String, + val graduatingYear: Int, + val schoolId: Long, ) \ No newline at end of file diff --git a/Graduating-iOS/Data/Network/AuthService.swift b/Graduating-iOS/Data/Network/AuthService.swift index 76e6a83..142f39b 100644 --- a/Graduating-iOS/Data/Network/AuthService.swift +++ b/Graduating-iOS/Data/Network/AuthService.swift @@ -4,8 +4,8 @@ import Moya import MyMoya enum AuthEndpoint { - case signUp(SignUpReq) case signIn(SignInReq) + case signUp(SignUpReq) case refresh(RefreshReq) } @@ -13,12 +13,12 @@ extension AuthEndpoint: MyTarget { var host: String { "auth" } var route: Route { switch self { - case .signUp(let req): - .post("sign-up") - .task(req.toJSONParameters()) case .signIn(let req): .post("sign-in") .task(req.toJSONParameters()) + case .signUp(let req): + .post("sign-up") + .task(req.toJSONParameters()) case .refresh(let req): .post("refresh") .task(req.toJSONParameters()) @@ -27,10 +27,10 @@ extension AuthEndpoint: MyTarget { var authorization: Authorization { switch self { - case .signUp: - .none case .signIn: .none + case .signUp: + .none case .refresh: .none } @@ -40,14 +40,14 @@ extension AuthEndpoint: MyTarget { public class AuthService { public static let shared = AuthService() - public func signUp(_ req: SignUpReq) -> AnyPublisher { - runner.deepDive(AuthEndpoint.signUp(req), res: Token.self) - } - public func signIn(_ req: SignInReq) -> AnyPublisher { runner.deepDive(AuthEndpoint.signIn(req), res: Token.self) } + public func signUp(_ req: SignUpReq) -> AnyPublisher { + runner.deepDive(AuthEndpoint.signUp(req), res: Token.self) + } + public func refresh(_ req: RefreshReq) -> AnyPublisher { runner.deepDive(AuthEndpoint.refresh(req), res: Token.self) } diff --git a/Graduating-iOS/Graduating/Feature/EditProfile/EditProfileView.swift b/Graduating-iOS/Graduating/Feature/EditProfile/EditProfileView.swift index 78b8bba..3682bbf 100644 --- a/Graduating-iOS/Graduating/Feature/EditProfile/EditProfileView.swift +++ b/Graduating-iOS/Graduating/Feature/EditProfile/EditProfileView.swift @@ -42,7 +42,7 @@ extension EditProfileView { .init(title: "프로필 수정 성공") .primaryButton("닫기") { router.pop() - appState.fetchCurrentUser() +// appState.fetchCurrentUser() // TODO: Handle } ) case .failure: diff --git a/Graduating-iOS/Graduating/Feature/Onboarding/OnboardingFirstView.swift b/Graduating-iOS/Graduating/Feature/Onboarding/OnboardingFirstView.swift index 71f8e35..5dc6453 100644 --- a/Graduating-iOS/Graduating/Feature/Onboarding/OnboardingFirstView.swift +++ b/Graduating-iOS/Graduating/Feature/Onboarding/OnboardingFirstView.swift @@ -15,6 +15,7 @@ struct OnboardingFirstView { @EnvironmentObject private var viewModel: OnboardingViewModel @EnvironmentObject private var dialog: DialogProvider @EnvironmentObject private var router: Router + @EnvironmentObject private var appState: AppState @StateObject private var oauth2ViewModel = OAuth2ViewModel() @@ -47,6 +48,11 @@ extension OnboardingFirstView: View { .onReceive(oauth2ViewModel.$googleSignInFlow) { receiveSignInFlow($0, platformType: .google) } + .onReceive(viewModel.$signInFlow) { + if case .success = $0 { + + } + } } } @@ -56,6 +62,7 @@ extension OnboardingFirstView { case .success(let code): viewModel.code = code viewModel.platformType = platformType + viewModel.signIn() router.push(OnboardingSecondView.Path()) case .failure: dialog.present( diff --git a/Graduating-iOS/Graduating/Feature/Onboarding/OnboardingViewModel.swift b/Graduating-iOS/Graduating/Feature/Onboarding/OnboardingViewModel.swift index 6a530ca..573ad3c 100644 --- a/Graduating-iOS/Graduating/Feature/Onboarding/OnboardingViewModel.swift +++ b/Graduating-iOS/Graduating/Feature/Onboarding/OnboardingViewModel.swift @@ -15,7 +15,8 @@ final class OnboardingViewModel: ObservableObject { @Published var school: School? @Published var graduatingYear = Date.now[.year] ?? 1900 @Published var nickname: String = "" - @Published var signInFlow = Flow.idle + @Published var signUpFlow: Resource = .idle + @Published var signInFlow: Resource = .idle var subscriptions = Set() @@ -25,11 +26,11 @@ final class OnboardingViewModel: ObservableObject { } extension OnboardingViewModel { - func signIn() { + func signUp() { guard let platformType, let code, let school, isValidInput else { return } - AuthService.shared.signIn( + AuthService.shared.signUp( .init( platformType: platformType, code: code, @@ -38,11 +39,20 @@ extension OnboardingViewModel { schoolId: school.id ) ) - .flow(\.signInFlow, on: self) - .ignoreError() - .sink { - Sign.me.login(id: "", password: "", accessToken: $0.accessToken, refreshToken: $0.refreshToken) + .resource(\.signInFlow, on: self) + .silentSink() + .store(in: &subscriptions) + } + + func signIn() { + guard let platformType, let code else { + return } + AuthService.shared.signIn( + .init(platformType: platformType, code: code) + ) + .resource(\.signUpFlow, on: self) + .silentSink() .store(in: &subscriptions) } } diff --git a/Graduating-iOS/Graduating/Feature/ViewModel/AppState.swift b/Graduating-iOS/Graduating/Feature/ViewModel/AppState.swift index 11b5711..6967cda 100644 --- a/Graduating-iOS/Graduating/Feature/ViewModel/AppState.swift +++ b/Graduating-iOS/Graduating/Feature/ViewModel/AppState.swift @@ -15,16 +15,22 @@ final class AppState: ObservableObject { init() { fetchCurrentUser() } - +} + +extension AppState { func logout() { Sign.me.logout() currentUser = .idle - isLoggedIn = false + isLoggedIn = Sign.me.isLoggedIn } -} - -extension AppState { - func fetchCurrentUser() { + + func signIn(token: Token) { + Sign.me.login(id: "", password: "", accessToken: token.accessToken, refreshToken: token.refreshToken) + fetchCurrentUser() + isLoggedIn = Sign.me.isLoggedIn + } + + private func fetchCurrentUser() { UserService.shared.getMe() .resource(\.currentUser, on: self) .sink { diff --git a/Graduating-iOS/Model/Auth/AuthDTO.swift b/Graduating-iOS/Model/Auth/AuthDTO.swift index c18e407..2a410e6 100644 --- a/Graduating-iOS/Model/Auth/AuthDTO.swift +++ b/Graduating-iOS/Model/Auth/AuthDTO.swift @@ -1,4 +1,4 @@ -public struct SignUpReq: ReqProtocol { +public struct SignInReq: ReqProtocol { public let platformType: PlatformType public let code: String @@ -11,7 +11,7 @@ public struct SignUpReq: ReqProtocol { } } -public struct SignInReq: ReqProtocol { +public struct SignUpReq: ReqProtocol { public let platformType: PlatformType public let code: String public let nickname: String