|
|
1 jaar geleden | |
|---|---|---|
| .. | ||
| AuthenticationExample | 1 jaar geleden | |
| AuthenticationExample.xcodeproj | 1 jaar geleden | |
| AuthenticationExampleUITests | 1 jaar geleden | |
| ObjCApiTests | 1 jaar geleden | |
| SwiftApiTests | 1 jaar geleden | |
| media | 2 jaren geleden | |
| Podfile | 1 jaar geleden | |
| README.md | 1 jaar geleden | |
| generate_refresh_token.sh | 2 jaren geleden | |
This Sample App is used for manual and automated Firebase Auth integration testing.
It's implementation is based on the quickstart that can be found here
Firebase Auth offers multiple ways to authenticate users. In this sample, we demonstrate how you can use Firebase Auth to authenticate users by providing implementations for the various authentication flows. Since each Firebase Auth flow is different, each may require a few extra steps to set everything up. Feel free to follow along and configure as many authentication flows as you would like to demo!
Ready? Let's get started! 🏎💨
Open the AuthenticationExample.xcodeproj project.
We will need to connect our sample with the Firebase Console. For an in depth explanation, you can read more about adding Firebase to your iOS Project.
Bundle Identifier you set for this iOS App matches that of the one in this sample.GoogleService-Info.plist when prompted.GoogleService-Info.plist into the opened app. In Xcode, you can also add this file to the project by going to File-> Add Files to 'AuthenticationExample' and selecting the downloaded .plist file. Be sure to add the .plist file to the app's main target.To enable sign in with each of the following identity providers, there are a few configuration steps required to make sure everything works properly.
When it comes to configuring most of the below identity providers, you may have to add a custom URL scheme in your Xcode project so Firebase Auth can correctly work with the corresponding Identity Provider. This is done by selecting the app's target in Xcode and navigating to the Info tab. For each login flow that requires adding a custom URL scheme, be sure to add a new URL Scheme for each respective identity provider rather than replace existing schemes you have created previously.
We have already included the GoogleSignIn cocoapod in the Podfile. This cocoapod is required for Google Sign In.
Select the Auth panel and then click the Sign In Method tab.
Click Google and turn on the Enable switch, then click Save.
In Xcode, add a custom URL scheme for your reversed client ID.
GoogleService-Info.plist. This is the value associated with the REVERSED_CLIENT_ID key in the GoogleService-Info.plist file.URL Type's Identifier, something like "Firebase Auth" adds some context for what the reversed link is related to.Info tab. Look for the URL Types section. Expand the section and add a 'URL Type' and by pasting in the URL and, optionally, adding an identifier.Run the app on your device or simulator.
Choose Google under Identity Providers to launch the Google Sign In flow
See the Getting Started with Google Sign In guide for more details.
As outlined in the docs, Sign in with Apple requires enabling the Sign In with Apple Capability in the Xcode project.
GoogleService-Info.plistChoose Twitter under Identity Providers to launch the Twitter Sign In flow
See the Getting Started with Twitter Sign In guide for more details.
GoogleService-Info.plistSee the Getting Started with Microsoft Sign In guide for more details.
GoogleService-Info.plistSee the Getting Started with GitHub Sign In guide for more details.
GoogleService-Info.plistSee the Getting Started with Yahoo Sign In guide for more details.
We have already included the FBSDKLoginKit cocoapod in the Podfile. This cocoapod is required for Sign In with Facebook.
com.google.firebase.quickstart.AuthenticationExample. This is the default bundle identifier for this quickstart. If you change it, be sure that the bundle identifier entered on the Facebook developer console matches that of the bundle identifier for the quickstart.kFacebookAppID at the top of AuthViewController.swift with your Facebook App Id
Info.plist'fb' + the Facebook App Idfb1234567890See the Getting Started with Password-based Sign In guide for more details.
Email Link authentication, which is also referred to as Passwordless authentication, works by sending a verification email to a user requesting to sign in. This verification email contains a special Dynamic Link that links the user back to your app, completing authentication in the process. In order to configure this method of authentication, we will use Firebase Dynamic Links, which we will need to set up.
As we mentioned above, we will need to configure dynamic links for this auth flow.
applinks:[insert the domain you copied] into the Associated Domains section on either Xcode or Apple developer console (depending on how you set up Associated Domains in the previous step)
applinks: authenticationexample.page.linkNow let's create the dynamic link that will be used in the Passwordless login flow. Return to the Dynamic Links tab on the Firebase Console. Click New Dynamic Link, then:
[the app's name].firebaseapp.com. Paste this entire domain into the Deep Link we are creating above. You can also instead allowlist the dynamic links URL prefix and use that here as well.Configure the following steps as you please and then hit Create!
Dynamic links use your app's bundle identifier as a url scheme by default. In Xcode, add a custom URL scheme for your bundle identifier.
Last todo! Navigate to sendSignInLink() in PasswordlessViewController.swift. Within the method, there is astringURLconstant. Paste in the long deeplink you created from the steps above for theauthroizedDomain` property above the method. It should look something like:
let stringURL = "https://\(authorizedDomain)/login"
Run the app on your device or simulator.
See the Getting Started with Email Link/Passwordless Sign In guide for more details.
We will start by taking a look at PasswordlessViewController.swift. If you are currently running the app, select the "Email Link/Passwordless" authentication option.
The user is prompted for an email to be used in the verification process. When the Send Sign In Link button is tapped, we configure our verification link by adding the user's email to the dynamic link we created earlier. Then we send a send the link to the user's email. You can edit the format of these verification emails on the Firebase Console.
When the user receives the verification email, they can open the link contained in the email to be redirected back to the app (using the power of Dynamic Links 😎. On apps using the SceneDelegate API, opening the incoming dynamic link will be handled in UIWindowSceneDelegate's func scene(_ scene: UIScene, continue userActivity: NSUserActivity) method. This method can be implemented in SceneDelegate.swift. Since the sample uses the SceneDelegate API, you can check out the implementation in SceneDelegate.swift. We basically pass the incoming link to a helper method that will do a few things:
// SceneDelegate.swift
private func handleIncomingDynamicLink(_ incomingURL: URL) {
let link = incomingURL.absoluteString
// Here, we check if our dynamic link is a sign-link (the one we emailed our user!)
if Auth.auth().isSignIn(withEmailLink: link) {
// Save the link as it will be used in the next step to complete login
UserDefaults.standard.set(link, forKey: "Link")
// Post a notification to the PasswordlessViewController to resume authentication
NotificationCenter.default.post(Notification(name: Notification.Name("PasswordlessEmailNotificationSuccess")))
}
}
If the incoming dynamic link is a sign-in link, then we post a notification that pretty much says: "Hey! A user just opened a verification dynamic link that we emailed them and we need to complete the authentication!"
This takes us back to our PasswordlessViewController.swift, where we registered for this exact notification. When the notification is posted, we will receive it here and call the passwordlessSignIn() method to complete the authentication. In this method, we used Firebase Auth's Auth.auth().signIn(withEmail: String, link: String) which, behind the scenes, checks that this link was the link we originally sent to the associated email and if so, signs in the user! 🥳
When Firebase Auth uses Phone Number authentication, Auth will attempt to send a silent Apple Push Notification (APN) to the device to confirm that the phone number being used is associated with the device. If APNs (which, like Sign In with Apple, are a capability you can enable in Xcode or on the Apple Developer Console) are not enabled or configured correctly, Auth will instead present a web view with a reCAPTCHA verification flow.
Firebase Auth can manage authentication for use cases that utilize a custom auth system. Ensure you have an authentication server capable of producing custom signed tokens. When a user signs in, make a request for a signed token from your authentication server.
After your server returns the token, pass that into Firebase Auth's signIn(withCustomtoken: String) method to complete the authentication process. In the sample, you can demo signing in with tokens you generate. See CustomAuthViewController.swift for more info.
If you wish to setup a custom auth system. The below steps can help in its configuration.
X service accounts link. This will take you to the Google Cloud Console.web/auth.html in your computer's web browser. The auth.html file can now be found in the current directory's LegacyAuthQuickstart subdirectory.