Firebase Authorization

Firebase Authorization

A77456b262557e22986345f6d0555c58?s=128

nakajijapan

July 25, 2018
Tweet

Transcript

  1. 5.
  2. 6.

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

    4XJGUGPSNBU 'JSFCBTF "OBMZUJDT "VUIFOUJDBUJPO 1PMDB 5FTUT 1VTI Continuous Delivery
  3. 8.
  4. 11.

  5. 14.
  6. 15.
  7. 17.
  8. 18.

    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 () } } }) }
  9. 19.

    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() }) }
  10. 20.
  11. 21.
  12. 22.
  13. 24.

    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() }) }
  14. 25.
  15. 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() }) }
  16. 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() }) }
  17. 29.

    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
  18. 30.

    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() }) }
  19. 31.
  20. 32.
  21. 34.