Firebase Authorization

Firebase Authorization

A77456b262557e22986345f6d0555c58?s=128

nakajijapan

July 25, 2018
Tweet

Transcript

  1. QPUBUPUJQT Firebase Authorization Ͱେมͩͬͨ͜ͱ

  2. @nakajijapan Software Engineer CAMPFIRE / iOSDC / NKJ Internet iOS

    / Android / Web / macOS
  3. NKJMultiMovieCaptureView NKJMovieComposer NKJPagerViewController PhotoSlider Teiten GitHub Sengiri Shari Ajimi Kazaguruma

    iOSDC 2017 Izumo
  4. Izumo macOS iPhone / iPad Markdown Note Tool

  5. None
  6. DJSDMFDJ GBTUMBOF DPEBDZ J5VOFT$POOFDU 'BCSJD #FUB $SBTIMZUJDT "QQ4UPSF $PEF$PWFSBHF 4XJGUMJOU

    4XJGUGPSNBU 'JSFCBTF "OBMZUJDT "VUIFOUJDBUJPO 1PMDB 5FTUT 1VTI Continuous Delivery
  7. Firebase Authentication

  8. None
  9. 'JSFCBTF"VUIFOUJDBUJPOʹ͸ɺόοΫΤϯ υαʔϏεɺ࢖͍΍͍͢4%,ɺΞϓϦͰͷϢʔ βʔೝূʹ࢖༻Ͱ͖Δ6*ϥΠϒϥϦ͕༻ҙ͞Ε ͍ͯ·͢ɻ'JSFCBTF"VUIFOUJDBUJPOͰ͸ɺύ εϫʔυɺి࿩൪߸ɺҰൠతͳϑΣσϨʔγϣϯ *%ϓϩόΠμʢ(PPHMFɺ'BDFCPPLɺ5XJUUFSʣ ͳͲΛ࢖༻ͨ͠ೝূΛߦ͏͜ͱ͕Ͱ͖·͢ɻ

  10. Firebase Authentication • ϩάΠϯΛ׬ྃ͢Δ • tokenΛϔομʹՃ͑ͯϦΫΤετ • αʔόʔଆͰݕূ

  11. ⚠ • Tokenͷ༗ޮظݶ͸̍࣌ؒ

  12. ⚠ • Tokenͷ༗ޮظݶ͸̍࣌ؒ • getIDTokenForcingRefresh()ͷར༻ • ແ͚Ε͹ɺ৽͍͠tokenΛൃߦ

  13. Retry

  14. None
  15. ⚠ • ࣌ʑϦΫΤετʹࣦഊ͢Δ • ͦͷޙɺ௨৴͸࠶։͞ΕΔ

  16. RxSwift

  17. func retryWithAuthIfNeeded() -> Observable<E> { let firebaseAuthProvider = FirebaseAuthProvier() return

    self.retryWhen({ (errors: Observable<Swift.Error>) in errors.enumerated().flatMap { retryCount, error -> Observable<Void> in guard case let Moya.MoyaError.statusCode(response) = error else { return Observable.error(error) } if response.statusCode != 401 { return Observable.error(error) } if response.statusCode == 401 && retryCount >= 2 { return Observable.error(error) } return firebaseAuthProvider.auth().do(onError: { error in throw error }).map { _ in () } } }) }
  18. func auth() -> Observable<Bool> { return Observable<Bool>.create({ observe -> Disposable

    in self.firebaseAuth { success in observe.on(.next(success)) observe.on(.completed) } return Disposables.create() }) }
  19. None
  20. None
  21. ඇಉظ

  22. DispatchSemaphore

  23. func auth() -> Observable<String> { let semaphore = DispatchSemaphore(value: 1)

    var authError: Error? return Observable<String>.create({ observer in Auth.auth().addStateDidChangeListener({ _, user in guard let user = user else { authError = FirebaseAuthError.unknownUser semaphore.signal() return } user.getIDTokenForcingRefresh(true, completion: { token, error in if error != nil { authError = FirebaseAuthError.noAuthToken semaphore.signal() return } UserCredentials.shared.accessToken = token semaphore.signal() }) }) semaphore.wait() if authError != nil { observer.onError(authError!) } observer.onNext(UserCredentials.shared.accessToken ?? "") observer.onCompleted() return Disposables.create() }) }
  24. None
  25. DispatchSemaphore

  26. func auth() -> Observable<Bool> { return Observable<Bool>.create({ observe -> Disposable

    in self.firebaseAuth { success in observe.on(.next(success)) observe.on(.completed) } return Disposables.create() }) }
  27. func auth() -> Observable<Bool> { return Observable<Bool>.create({ observe -> Disposable

    in self.firebaseAuth { success in observe.on(.next(success)) observe.on(.completed) } return Disposables.create() }) }
  28. func auth() -> Observable<Bool> { return Observable<Bool>.create({ observe -> Disposable

    in self.firebaseAuth { success in observe.on(.next(success)) observe.on(.completed) } return Disposables.create() }) } ద੾ͳΤϥʔॲཧ͕ߦΘΕ͍ͯͳ͔ͬͨ ੒ޭͯ͠΋ࣦഊͯ͠΋OFYUͯ͠DPNQMFUFE
  29. func auth() -> Observable<String> { var authError: Error? return Observable<String>.create({

    observer in Auth.auth().addStateDidChangeListener({ _, user in guard let user = user else { observer.onError(FirebaseAuthError.unknownUser!) return } user.getIDTokenForcingRefresh(true, completion: { token, error in if error != nil { observer.onError(FirebaseAuthError.noAuthToken!) return } observer.onNext(token) observer.onCompleted() }) }) return Disposables.create() }) }
  30. None
  31. Wrap Up

  32. Wrap Up • Token͕੾Εͨͱ͖ͷରॲΛߟྀ͢Δ • retryWhenͰTokenΛߋ৽ͯ͠ϩάΠϯঢ় ଶΛҡ࣋͢Δ • RxSwiftؤுΔ

  33. Thanks.