Skip to content

Commit

Permalink
feat/#326 로그아웃 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
hooni0918 committed Aug 25, 2024
1 parent b70ac46 commit 68b4e74
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
22 changes: 13 additions & 9 deletions KkuMulKum/Network/Service/UserService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ final class MyPageUserService {
}
}
}
extension MyPageUserService : MyPageUserServiceProtocol {

func getUserInfo() async throws -> LoginUserModel {
return try await performRequest(.getUserInfo)
}

func unsubscribe(authCode: String) async throws {
let _: EmptyModel = try await performRequest(.unsubscribe(authCode: authCode))
}

extension MyPageUserService : MyPageUserServiceProtocol {
func getUserInfo() async throws -> LoginUserModel {
return try await performRequest(.getUserInfo)
}

func unsubscribe(authCode: String) async throws {
let _: EmptyModel = try await performRequest(.unsubscribe(authCode: authCode))
}

func logout() async throws {
let _: EmptyModel = try await performRequest(.logout)
}
}

8 changes: 8 additions & 0 deletions KkuMulKum/Network/TargetType/MypageTargetType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Moya
enum UserTargetType {
case getUserInfo
case unsubscribe(authCode : String)
case logout

}

extension UserTargetType: TargetType {
Expand All @@ -30,6 +32,8 @@ extension UserTargetType: TargetType {
return "/api/v1/users/me"
case .unsubscribe:
return "/api/v1/auth/withdrawal"
case .logout:
return "/api/v1/auth/signout"
}
}

Expand All @@ -39,6 +43,8 @@ extension UserTargetType: TargetType {
return .get
case .unsubscribe:
return .delete
case .logout:
return .post
}
}

Expand All @@ -48,6 +54,8 @@ extension UserTargetType: TargetType {
return .requestPlain
case .unsubscribe(let authCode):
return .requestPlain
case .logout:
return .requestPlain
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ protocol MyPageUserServiceProtocol {
func getUserInfo() async throws -> LoginUserModel
func performRequest<T: ResponseModelType>(_ target: UserTargetType) async throws -> T
func unsubscribe(authCode: String) async throws -> Void
func logout() async throws -> Void
}
15 changes: 15 additions & 0 deletions KkuMulKum/Source/MyPage/ViewController/MyPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ class MyPageViewController: BaseViewController, CustomActionSheetDelegate {
})
.disposed(by: disposeBag)

viewModel.logoutResult
.observe(on: MainScheduler.instance)
.subscribe(onNext: { [weak self] result in
switch result {
case .success:
print("Logout successful")
self?.navigateToLoginScreen()
case .failure(let error):
print("Logout failed: \(error)")
// 에러 처리 (예: 알림 표시)
}
})
.disposed(by: disposeBag)


viewModel.userInfo
.observe(on: MainScheduler.instance)
.subscribe(onNext: { [weak self] userInfo in
Expand Down
15 changes: 13 additions & 2 deletions KkuMulKum/Source/MyPage/ViewModel/MyPageViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class MyPageViewModel: NSObject {
let performUnsubscribe: Signal<Void>
let userInfo: BehaviorRelay<LoginUserModel?>
let unsubscribeResult = PublishRelay<Result<Void, Error>>()
let logoutResult = PublishRelay<Result<Void, Error>>()


init(userService: MyPageUserServiceProtocol = MyPageUserService()) {
self.userService = userService
Expand Down Expand Up @@ -63,8 +65,17 @@ class MyPageViewModel: NSObject {
}

func logout() {
print("로그아웃 눌름 ㅂㅂ")
}
Task {
do {
try await userService.logout()
print("Logout successful")
logoutResult.accept(.success(()))
} catch {
print("Logout failed: \(error)")
logoutResult.accept(.failure(error))
}
}
}

func unsubscribe() {
let appleIDProvider = ASAuthorizationAppleIDProvider()
Expand Down

0 comments on commit 68b4e74

Please sign in to comment.