Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firebase Authorization

Firebase Authorization

nakajijapan

July 25, 2018
Tweet

More Decks by nakajijapan

Other Decks in Technology

Transcript

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

    4XJGUGPSNBU 'JSFCBTF "OBMZUJDT "VUIFOUJDBUJPO 1PMDB 5FTUT 1VTI Continuous Delivery
  2. 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 () } } }) }
  3. 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() }) }
  4. 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() }) }
  5. 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() }) }
  6. 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() }) }
  7. 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
  8. 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() }) }