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. QPUBUPUJQT
    Firebase
    Authorization
    Ͱେมͩͬͨ͜ͱ

    View full-size slide

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

    View full-size slide

  3. NKJMultiMovieCaptureView
    NKJMovieComposer
    NKJPagerViewController
    PhotoSlider
    Teiten
    GitHub
    Sengiri
    Shari
    Ajimi
    Kazaguruma
    iOSDC 2017
    Izumo

    View full-size slide

  4. Izumo
    macOS
    iPhone / iPad
    Markdown Note Tool

    View full-size slide

  5. DJSDMFDJ GBTUMBOF
    DPEBDZ
    J5VOFT$POOFDU
    'BCSJD
    #FUB
    $SBTIMZUJDT
    "QQ4UPSF
    $PEF$PWFSBHF
    4XJGUMJOU
    4XJGUGPSNBU
    'JSFCBTF
    "OBMZUJDT
    "VUIFOUJDBUJPO
    1PMDB
    5FTUT
    1VTI
    Continuous Delivery

    View full-size slide

  6. Firebase
    Authentication

    View full-size slide

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

    View full-size slide

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

    View full-size slide


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

    View full-size slide


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

    View full-size slide


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

    View full-size slide

  12. func retryWithAuthIfNeeded() -> Observable {
    let firebaseAuthProvider = FirebaseAuthProvier()
    return self.retryWhen({ (errors: Observable) in
    errors.enumerated().flatMap { retryCount, error -> Observable 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 () }
    }
    })
    }

    View full-size slide

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

    View full-size slide

  14. DispatchSemaphore

    View full-size slide

  15. func auth() -> Observable {
    let semaphore = DispatchSemaphore(value: 1)
    var authError: Error?
    return Observable.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()
    })
    }

    View full-size slide

  16. DispatchSemaphore

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  20. func auth() -> Observable {
    var authError: Error?
    return Observable.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()
    })
    }

    View full-size slide

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

    View full-size slide