Skip to content

Commit

Permalink
Feat: #108 - Apple 로그인 연동 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongsuu committed Aug 27, 2022
1 parent d598b05 commit b6b9991
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2022 Tidify. All rights reserved.
//

import TidifyCore
import TidifyDomain

import ReactorKit
Expand All @@ -29,6 +30,7 @@ final class SignInReactor: Reactor {

enum Action {
case trySignIn(type: SocialLoginType)
case appleSignIn(token: String)
}

struct State {
Expand All @@ -40,6 +42,8 @@ final class SignInReactor: Reactor {
case .trySignIn(let type):
return usecase.trySignIn(type: type)
.map { .trySignIn(type: type) }
case .appleSignIn(let token):
return .just(.appleSignIn(token: token))
}
}

Expand All @@ -50,6 +54,10 @@ final class SignInReactor: Reactor {
case .trySignIn(let type):
newState.successSignIn = true
coordinator.didSuccessSignIn(type: type)
case .appleSignIn(let token):
AppProperties.authorization = token
newState.successSignIn = true
coordinator.didSuccessSignIn(type: .apple)
}

return newState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import AuthenticationServices
import TidifyCore
import UIKit

import RxCocoa
Expand Down Expand Up @@ -66,6 +67,13 @@ final class SignInViewController: UIViewController, View {
$0.cornerRadius = 14
}

private let appleSignInSubject: PublishSubject<String> = .init()
private var signInWithAppleBinder: Binder<Void> {
return .init(self, binding: { vc, _ in
vc.signInWithApple()
})
}

var disposeBag: DisposeBag = .init()

override func viewDidLoad() {
Expand All @@ -80,8 +88,49 @@ final class SignInViewController: UIViewController, View {
}
}

extension SignInViewController: ASAuthorizationControllerDelegate {
func authorizationController(
controller: ASAuthorizationController,
didCompleteWithAuthorization authorization: ASAuthorization
) {
// Apple ID 연동 성공시
switch authorization.credential {
case let appleIDCredential as ASAuthorizationAppleIDCredential:
let token: String = String(data: appleIDCredential.identityToken!, encoding: .utf8)!
appleSignInSubject.onNext(token)

default: break
}
}

func authorizationController(
controller: ASAuthorizationController,
didCompleteWithError error: Error
) {
// Apple ID 연동 실패시
Beaver.error("Fail Apple Authentication")
}
}

extension SignInViewController: ASAuthorizationControllerPresentationContextProviding {
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return self.view.window!
}
}

// MARK: - Private
private extension SignInViewController {
func signInWithApple() {
let appleIDProvider: ASAuthorizationAppleIDProvider = .init()
let request: ASAuthorizationAppleIDRequest = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]

let authorizationController: ASAuthorizationController = .init(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}

func setupUI() {
view.backgroundColor = .white

Expand Down Expand Up @@ -134,14 +183,19 @@ private extension SignInViewController {
.disposed(by: disposeBag)

appleSignInButton.rx.controlEvent(.touchUpInside)
.map { Action.trySignIn(type: .apple) }
.bind(to: signInWithAppleBinder)
.disposed(by: disposeBag)

appleSignInSubject
.map { Action.appleSignIn(token: $0) }
.bind(to: reactor.action)
.disposed(by: disposeBag)
}

func bindState(reactor: SignInReactor) {
reactor.state
.map { $0.successSignIn }
.distinctUntilChanged()
.subscribe()
.disposed(by: disposeBag)
}
Expand Down
10 changes: 10 additions & 0 deletions Tidify/Tidify.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.applesignin</key>
<array>
<string>Default</string>
</array>
</dict>
</plist>

0 comments on commit b6b9991

Please sign in to comment.