FSTCredentialsProvider.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #include "Firestore/core/src/firebase/firestore/auth/token.h"
  18. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. @class FIRApp;
  21. @class FSTDispatchQueue;
  22. #pragma mark - Typedefs
  23. /**
  24. * `FSTVoidTokenErrorBlock` is a block that gets a token or an error.
  25. *
  26. * @param token An auth token, either valid or invalid when error occurred.
  27. * @param error The error if one occurred, or else `nil`.
  28. */
  29. typedef void (^FSTVoidGetTokenResultBlock)(firebase::firestore::auth::Token token,
  30. NSError *_Nullable error);
  31. /** Listener block notified with a User. */
  32. typedef void (^FSTVoidUserBlock)(firebase::firestore::auth::User user);
  33. #pragma mark - FSTCredentialsProvider
  34. /** Provides methods for getting the uid and token for the current user and listen for changes. */
  35. @protocol FSTCredentialsProvider <NSObject>
  36. /** Requests token for the current user, optionally forcing a refreshed token to be fetched. */
  37. - (void)getTokenForcingRefresh:(BOOL)forceRefresh completion:(FSTVoidGetTokenResultBlock)completion;
  38. /**
  39. * A listener to be notified of user changes (sign-in / sign-out). It is immediately called once
  40. * with the initial user.
  41. *
  42. * Note that this block will be called back on an arbitrary thread that is not the normal Firestore
  43. * worker thread.
  44. */
  45. @property(nonatomic, copy, nullable, readwrite) FSTVoidUserBlock userChangeListener;
  46. @end
  47. #pragma mark - FSTFirebaseCredentialsProvider
  48. /**
  49. * `FSTFirebaseCredentialsProvider` uses Firebase Auth via `FIRApp` to get an auth token.
  50. *
  51. * NOTE: To simplify the implementation, it requires that you set `userChangeListener` with a
  52. * non-`nil` value no more than once and don't call `getTokenForcingRefresh:` after setting
  53. * it to `nil`.
  54. *
  55. * This class must be implemented in a thread-safe manner since it is accessed from the thread
  56. * backing our internal worker queue and the callbacks from FIRAuth will be executed on an
  57. * arbitrary different thread.
  58. */
  59. @interface FSTFirebaseCredentialsProvider : NSObject <FSTCredentialsProvider>
  60. /**
  61. * Initializes a new FSTFirebaseCredentialsProvider.
  62. *
  63. * @param app The Firebase app from which to get credentials.
  64. *
  65. * @return A new instance of FSTFirebaseCredentialsProvider.
  66. */
  67. - (instancetype)initWithApp:(FIRApp *)app NS_DESIGNATED_INITIALIZER;
  68. - (id)init NS_UNAVAILABLE;
  69. @end
  70. NS_ASSUME_NONNULL_END