Skip to content

Commit

Permalink
Fix signUp & signIn
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Oct 6, 2024
1 parent 9e53552 commit fce9b33
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
20 changes: 10 additions & 10 deletions Graduating-iOS/Data/Network/AuthService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import Moya
import MyMoya

enum AuthEndpoint {
case signUp(SignUpReq)
case signIn(SignInReq)
case signUp(SignUpReq)
case refresh(RefreshReq)
}

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())
Expand All @@ -27,10 +27,10 @@ extension AuthEndpoint: MyTarget {

var authorization: Authorization {
switch self {
case .signUp:
.none
case .signIn:
.none
case .signUp:
.none
case .refresh:
.none
}
Expand All @@ -40,14 +40,14 @@ extension AuthEndpoint: MyTarget {
public class AuthService {
public static let shared = AuthService()

public func signUp(_ req: SignUpReq) -> AnyPublisher<Token, APIError> {
runner.deepDive(AuthEndpoint.signUp(req), res: Token.self)
}

public func signIn(_ req: SignInReq) -> AnyPublisher<Token, APIError> {
runner.deepDive(AuthEndpoint.signIn(req), res: Token.self)
}

public func signUp(_ req: SignUpReq) -> AnyPublisher<Token, APIError> {
runner.deepDive(AuthEndpoint.signUp(req), res: Token.self)
}

public func refresh(_ req: RefreshReq) -> AnyPublisher<Token, APIError> {
runner.deepDive(AuthEndpoint.refresh(req), res: Token.self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension EditProfileView {
.init(title: "프로필 수정 성공")
.primaryButton("닫기") {
router.pop()
appState.fetchCurrentUser()
// appState.fetchCurrentUser() // TODO: Handle
}
)
case .failure:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -47,6 +48,11 @@ extension OnboardingFirstView: View {
.onReceive(oauth2ViewModel.$googleSignInFlow) {
receiveSignInFlow($0, platformType: .google)
}
.onReceive(viewModel.$signInFlow) {
if case .success = $0 {

}
}
}
}

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Token> = .idle
@Published var signInFlow: Resource<Token> = .idle

var subscriptions = Set<AnyCancellable>()

Expand All @@ -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,
Expand All @@ -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)
}
}
18 changes: 12 additions & 6 deletions Graduating-iOS/Graduating/Feature/ViewModel/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions Graduating-iOS/Model/Auth/AuthDTO.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public struct SignUpReq: ReqProtocol {
public struct SignInReq: ReqProtocol {
public let platformType: PlatformType
public let code: String

Expand All @@ -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
Expand Down

0 comments on commit fce9b33

Please sign in to comment.