|
| 1 | +# FirebaseUI for iOS — Auth |
| 2 | + |
| 3 | +FirebaseUI is an open-source library for iOS that provides simple, customizable UI |
| 4 | +bindings on top of [Firebase](https://firebase.google.com) SDKs to eliminate |
| 5 | +boilerplate code and promote best practices. |
| 6 | + |
| 7 | +FirebaseUI provides a drop-in auth solution that handles the UI flows for |
| 8 | +signing in users with email addresses and passwords, Google Sign-In, and |
| 9 | +Facebook Login. It is built on top of [Firebase Auth](https://firebase.google.com/docs/auth). |
| 10 | + |
| 11 | +The FirebaseUI Auth component implement best practices for authentication on |
| 12 | +mobile devices and websites, which can maximize sign-in and sign-up conversion |
| 13 | +for your app. It also handles edge cases like account recovery and account |
| 14 | +linking that can be security sensitive and error-prone to handle correctly. |
| 15 | + |
| 16 | +FirebaseUI can be easily customized to fit in with the rest of your app's visual |
| 17 | + style, and it is open source, so you aren't constrained in realizing the user |
| 18 | + experience you want. |
| 19 | + |
| 20 | +Compatible FirebaseUI clients are also available for [Android](https://github.com/firebase/firebaseui-android/tree/master/auth) |
| 21 | +and [Web](https://github.com/firebase/firebaseui-web/tree/master/auth). |
| 22 | + |
| 23 | +## Table of Contents |
| 24 | + |
| 25 | +1. [Installation](#installation) |
| 26 | +2. [Usage instructions](#using-firebaseui-for-authentication) |
| 27 | +3. [Customization](#customizing-firebaseui-for-authentication) |
| 28 | + |
| 29 | +## Installation |
| 30 | +### Importing FirebaseUI components for auth |
| 31 | +Add the following line to your `Podfile`: |
| 32 | +```ruby |
| 33 | +pod 'FirebaseUI/Auth' |
| 34 | +``` |
| 35 | + |
| 36 | +### Configuring sign-in providers |
| 37 | +To use FirebaseUI to authenticate users you first need to configure each provider you want to use in |
| 38 | +their own developer app settings. Please read the *Before you begin* section of the Firebase |
| 39 | +Auth guides at the following links: |
| 40 | + |
| 41 | +- [Email and password](https://firebase.google.com/docs/auth/ios/password-auth#before_you_begin) |
| 42 | +- [Google](https://firebase.google.com/docs/auth/ios/google-signin#before_you_begin) |
| 43 | +- [Facebook](https://firebase.google.com/docs/auth/ios/facebook-login#before_you_begin) |
| 44 | + |
| 45 | + |
| 46 | +## Using FirebaseUI for Authentication |
| 47 | + |
| 48 | +### Configuration |
| 49 | + |
| 50 | +All operations, callbacks, UI customizations are done through an `FIRAuthUI` |
| 51 | +instance. The `FIRAuthUI` instance associated with the default `FIRAuth` |
| 52 | +instance can be accessed as follows: |
| 53 | + |
| 54 | +```swift |
| 55 | +// swift |
| 56 | +import Firebase |
| 57 | +import FirebaseAuthUI |
| 58 | + |
| 59 | +/* ... */ |
| 60 | + |
| 61 | +FIRApp.configure() |
| 62 | +let authUI = FIRAuthUI.authUI() |
| 63 | +authUI?.delegate = self |
| 64 | +``` |
| 65 | + |
| 66 | +```objective-c |
| 67 | +// objc |
| 68 | +@import Firebase |
| 69 | +@import FirebaseAuthUI |
| 70 | +... |
| 71 | +[FIRApp configure]; |
| 72 | +FIRAuthUI *authUI = [FIRAuthUI authUI]; |
| 73 | +authUI.delegate = self; // Set the delegate to receive callback. |
| 74 | +``` |
| 75 | + |
| 76 | +This instance can then be configured with the providers you wish to support: |
| 77 | + |
| 78 | +```swift |
| 79 | +// swift |
| 80 | +import FirebaseGoogleAuthUI |
| 81 | +import FirebaseFacebookAuthUI |
| 82 | + |
| 83 | +let googleAuthUI = FIRGoogleAuthUI(clientID: kGoogleClientID) |
| 84 | +let facebookAuthUI = FIRFacebookAuthUI(appID: kFacebookAppID) |
| 85 | + |
| 86 | +authUI?.signInProviders = [googleAuthUI, facebookAuthUI] |
| 87 | +``` |
| 88 | + |
| 89 | +```objective-c |
| 90 | +// objc |
| 91 | +@import FirebaseGoogleAuthUI |
| 92 | +@import FirebaseFacebookAuthUI |
| 93 | +... |
| 94 | +FIRGoogleAuthUI *googleAuthUI = |
| 95 | + [[FIRGoogleAuthUI alloc] initWithClientID:kGoogleClientID]; |
| 96 | +FIRFacebookAuthUI *facebookAuthUI = |
| 97 | + [[FIRFacebookAuthUI alloc] initWithAppID:kFacebookAppID]; |
| 98 | +authUI.signInProviders = @[ googleAuthUI, facebookAuthUI]; |
| 99 | +``` |
| 100 | +
|
| 101 | +For Google sign in support, add custom URL schemes to your Xcode project |
| 102 | +(step 1 of the [implement Google Sign-In documentation](https://developers.google.com/firebase/docs/auth/ios/google-signin#2_implement_google_sign-in)). |
| 103 | +
|
| 104 | +For Facebook sign in support, follow step 3 and 4 of |
| 105 | +[Facebook login documentation](https://developers.google.com/firebase/docs/auth/ios/facebook-login#before_you_begin) |
| 106 | +, and add custom URL schemes following step 5 of [Facebook SDK for iOS-Getting started documentation](https://developers.facebook.com/docs/ios/getting-started). |
| 107 | +
|
| 108 | +Finally add a call to handle the URL that your application receives at the end of the |
| 109 | +Google/Facebook authentication process. |
| 110 | +
|
| 111 | +```swift |
| 112 | +// swift |
| 113 | +func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool { |
| 114 | + let sourceApplication = options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String |
| 115 | + return FIRAuthUI.authUI()?.handleOpenURL(url, sourceApplication: sourceApplication ?? "") ?? false |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +```objective-c |
| 120 | +// objc |
| 121 | +- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options { |
| 122 | + NSString *sourceApplication = options[UIApplicationOpenURLOptionsSourceApplicationKey]; |
| 123 | + return [[FIRAuthUI authUI] handleOpenURL:url sourceApplication:sourceApplication]; |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### Sign In |
| 128 | + |
| 129 | +To start the authentication flow, obtain an `authViewController` instance from |
| 130 | +`FIRAuthUI`. In order to leverage FirebaseUI for iOS you must display the |
| 131 | +`authViewController`; you can present it as the first view controller of your |
| 132 | +app or present it from another view controller within your app. In order to |
| 133 | +present the `authViewController` obtain as instance as follows: |
| 134 | + |
| 135 | +```swift |
| 136 | +// swift |
| 137 | + |
| 138 | +// Present the auth view controller and then implement the sign in callback. |
| 139 | +let authViewController = authUI!.authViewController() |
| 140 | + |
| 141 | +func authUI(authUI: FIRAuthUI, didSignInWithUser user: FIRUser?, error: ErrorType?) { |
| 142 | + // handle user and error as necessary |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +```objective-c |
| 147 | +// objc |
| 148 | +UIViewController *authViewController = [authUI authViewController]; |
| 149 | +// Use authViewController as your root view controller, |
| 150 | +// or present it on top of an existing view controller. |
| 151 | + |
| 152 | +- (void)authUI:(FIRAuthUI *)authUI didSignInWithUser:(nullable FIRUser *)user error:(nullable NSError *)error { |
| 153 | + // Implement this method to handle signed in user or error if any. |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +## Customizing FirebaseUI for authentication |
| 158 | +### Terms of Service (ToS) URL customization: |
| 159 | + |
| 160 | +The Terms of Service URL for your application, which is displayed on the |
| 161 | +email/password account creation screen, can be specified as follows: |
| 162 | + |
| 163 | +```swift |
| 164 | +// swift |
| 165 | +authUI?.TOSURL = NSURL(string: "https://example.com/tos")! |
| 166 | +``` |
| 167 | + |
| 168 | +```objective-c |
| 169 | +// objc |
| 170 | +authUI.TOSURL = [NSURL URLWithString:@"https://example.com/tos"]; |
| 171 | +``` |
| 172 | +
|
| 173 | +### Custom strings |
| 174 | +
|
| 175 | +You can override the default messages and prompts shown to your users. This can |
| 176 | +be useful for things such as adding support for other languages besides English. |
| 177 | +
|
| 178 | +In order to do so: |
| 179 | +
|
| 180 | +```swift |
| 181 | +// swift |
| 182 | +authUI?.customStringsBundle = NSBundle.mainBundle() // Or any custom bundle. |
| 183 | +``` |
| 184 | + |
| 185 | +```objective-c |
| 186 | +// objc |
| 187 | +authUI.customStringsBundle = [NSBundle mainBundle]; // Or any custom bundle. |
| 188 | +``` |
| 189 | + |
| 190 | +The bundle should include [.strings](Auth/AuthUI/Strings/FirebaseAuthUI.strings) |
| 191 | +files that have the same names as the default files, namely `FirebaseAuthUI`, |
| 192 | +`FirebaseGoogleAuthUI`, and `FirebaseFacebookAuthUI`. Each string in these files |
| 193 | +should have the same key as its counterpart in the default `.strings` files. |
| 194 | + |
| 195 | +### Custom sign-in screen |
| 196 | + |
| 197 | +You can customize everything about the authentication method picker screen, |
| 198 | +except for the actual sign-in buttons. |
| 199 | + |
| 200 | +In order to do so, create a subclass of `FIRAuthPickerViewController` and |
| 201 | +customize it to your needs. Provide `FIRAuthUI` with an instance of your |
| 202 | +subclass by implementing the delegate method |
| 203 | +`authPickerViewControllerForAuthUI:` as follows: |
| 204 | + |
| 205 | +```swift |
| 206 | +// swift |
| 207 | +func authPickerViewControllerForAuthUI(authUI: FIRAuthUI) -> FIRAuthPickerViewController { |
| 208 | + return CustomAuthPickerViewController(authUI: authUI) |
| 209 | +} |
| 210 | +``` |
| 211 | + |
| 212 | +```objective-c |
| 213 | +// objc |
| 214 | +- (FIRAuthPickerViewController *)authPickerViewControllerForAuthUI:(FIRAuthUI *)authUI { |
| 215 | + return [[CustomAuthPickerViewController alloc] initWithAuthUI:authUI]; |
| 216 | +} |
| 217 | +``` |
0 commit comments