firebase 谷歌登录

This article is originally published at https://swiftsenpai.com on June 15, 2020.

本文最初于 2020年6月15日 发布在 https://swiftsenpai.com 上。

When it comes to third party backend authentication services for iOS apps, the first that comes to mind for sure is Firebase Authentication. By using Firebase Authentication, iOS developers can skip the hassle of setting up a secure backend server and only focus on the mobile side implementation.

当涉及针对iOS应用的第三方后端身份验证服务时,首先想到的肯定是Firebase身份验证。 通过使用Firebase身份验证,iOS开发人员可以跳过设置安全后端服务器的麻烦,而只专注于移动端实现。

Even though Firebase Authentication and Google Sign-in for iOS are both products of Google, they do not automatically integrate with each other.

尽管Firebase身份验证和iOS的Google登录都是Google的产品,但它们不会自动相互集成。

In this article, I will show you, step-by-step on how to set up Firebase for your iOS project and integrate it with Firebase Authentication.

在本文中,我将逐步向您展示如何为iOS项目设置Firebase并将其与Firebase身份验证集成。

先决条件 (Prerequisite)

This article is the continuation of my previous article Google Sign-In Integration in iOS. If you do not have an iOS app that is already integrated with Google Sign-in, I highly recommend you to go through the previous article before proceeding with this one.

本文是我上一篇文章iOS中的Google登录集成的继续。 如果您还没有与Google登录集成的iOS应用,我强烈建议您先阅读上一篇文章,然后再继续进行本文。

As a quick recap, here’s where we left off:

快速回顾一下,这是我们离开的地方:

Google Sign-In Integration in iOS
Google Sign-in integration in iOS
iOS中的Google登录集成

We have successfully integrated Google Sign-in into our sample app, which means users can sign in to the sample app using their Gmail account without any problem.

我们已成功将Google登录功能集成到示例应用程序中,这意味着用户可以使用其Gmail帐户登录该示例应用程序而不会出现任何问题。

With that being said, let’s get right into Firebase Authentication integration.

话虽如此,让我们直接进行Firebase身份验证集成。

创建Firebase项目 (Create Firebase Project)

Head over to Firebase Console and create a Firebase project for your sample app.

转到Firebase控制台,并为您的示例应用程序创建一个Firebase项目。

Create a Firebase project
Create a Firebase project
创建一个Firebase项目

After clicking on “Add Project”, you will be presented with a list of Google Cloud Platform projects. Go ahead and select the project that you created in Google APIs Console during the Google Sign-in integration.

单击“添加项目”后,将显示Google Cloud Platform项目列表。 继续并选择您在Google登录集成期间在Google API控制台中创建的项目。

Select a Google Cloud Platform project to add to Firebase
Select a Google Cloud Platform project to add to Firebase
选择一个Google Cloud Platform项目以添加到Firebase

After clicking “Continue”, you will be asked to enable Google Analytics for your project. You can disable it for now because Google Analytics is not required for Firebase Authentication integration.

单击“继续”后,将要求您为项目启用Google Analytics(分析)。 您现在可以将其禁用,因为Firebase身份验证集成不需要Google Analytics。

Disable Google Analytisc for Firebase Authentication
Disable Google Analytics
停用Google Analytics(分析)

After disabling Google Analytics, click “Create Project” to create your Firebase project.

停用Google Analytics(分析)后,点击“创建项目”以创建Firebase项目。

将Firebase添加到App (Add Firebase to App)

In order to add Firebase to your sample app, click on the “iOS” icon right below the project name to kick start the process.

为了将Firebase添加到示例应用程序,请单击项目名称正下方的“ iOS”图标以启动该过程。

Add iOS app to Firebase project
Add iOS app to Firebase project
将iOS应用添加到Firebase项目

注册应用 (Register App)

The first step is to register your sample app to the Firebase project.

第一步是将您的示例应用程序注册到Firebase项目。

Fill in your sample app’s bundle ID and app nickname then click “Register app” to proceed. You can leave the App Store ID blank for now.

填写示例应用程序的捆绑软件ID和应用程序昵称,然后单击“注册应用程序”继续。 您可以暂时将App Store ID留空。

Register iOS app for Firebase project
Register iOS app for Firebase project
为Firebase项目注册iOS应用

下载配置文件 (Download Config File)

In this step, you need to download the GoogleService-Info.plist which contains information on the Firebase project you just created and add it to your Xcode project.

在此步骤中,您需要下载GoogleService-Info.plist ,其中包含有关刚创建的Firebase项目的信息,并将其添加到Xcode项目中。

After that, make sure that the plist file has been added to the app target.

之后,请确保已将plist文件添加到应用程序目标中。

Ensure GoogleService-Info.plist added to the app target
GoogleService-Info.plist added to the app target GoogleService-Info.plist添加到应用目标

Once everything is done, click “Next” to proceed to the next step.

完成所有操作后,单击“下一步”继续进行下一步。

添加Firebase SDK (Add Firebase SDK)

In this step, we will use CocoaPods to install the Firebase Authentication SDK. Go ahead and open the Podfile that we created during the Google Sign-in integration and add in the following:

在此步骤中,我们将使用CocoaPods安装Firebase身份验证SDK。 继续并打开我们在Google登录集成过程中创建的Podfile,然后添加以下内容:

pod 'Firebase/Auth'

Here’s how your Podfile should look like:

这是您的Podfile的外观:

Add Firebase Authentication SDK to Podfile
Add Firebase Authentication SDK to Podfile
将Firebase身份验证SDK添加到Podfile

After that, close your Xcode project, fire up the Terminal app, navigate to your sample app project location and run pod install to install the Firebase Authentication SDK.

之后,关闭您的Xcode项目,启动终端应用程序,导航到示例应用程序项目位置,然后运行pod install安装Firebase Authentication SDK。

添加初始化代码 (Add Initialization Code)

The SDK installation might take a while to finish. Once the installation is completed, open your Xcode project using the workspace file (.xcworkspace) and navigate to AppDelegate.swift.

SDK安装可能需要一段时间才能完成。 安装完成后,使用工作空间文件( .xcworkspace )打开Xcode项目,并导航至AppDelegate.swift

Add the following line of code at the top of the file to import the Firebase SDK.

在文件顶部添加以下代码行以导入Firebase SDK。

import Firebase

After that, add the following line of code in application(_:didFinishLaunchingWithOptions:) to initialize Firebase during app launch.

之后,在application(_:didFinishLaunchingWithOptions:)添加以下代码行,以在应用启动期间初始化Firebase。

FirebaseApp.configure()
Configure Firebase in AppDelegate.swift
AppDelegate.swift AppDelegate.swift配置Firebase

With all that in place, build and run your sample app to make sure it can be executed without any problem.

一切就绪后,构建并运行示例应用程序,以确保可以毫无问题地执行该应用程序。

阅读iOS入门指南 (Read the Get Started Guide for iOS)

This is the last step of the process and you can basically skip this step and just click “Continue to console” to finish up the entire process.

这是该过程的最后一步,您基本上可以跳过此步骤,只需单击“继续控制台”即可完成整个过程。

Finishing up the add app to Firebase process
Finishing up the add app to Firebase process
完成将应用添加到Firebase流程

启用Google登录以进行Firebase身份验证 (Enable Google Sign-In for Firebase Authentication)

Up until this point, the last bit of configuration left is to enable Google Sign-in for Firebase Authentication.

到目前为止,剩下的最后配置是启用Google登录以进行Firebase身份验证。

You can follow the flow as shown in the image below to make Google as one of the sign-in providers of Firebase Authentication.

您可以按照下图所示的流程操作,使Google成为Firebase身份验证的登录提供商之一。

Enable Google Sign-in for Firebase Authentication
Enable Google Sign-in for Firebase Authentication
为Firebase身份验证启用Google登录

With that, you have completed the set up needed for Firebase. It is time to head over to Xcode and write some code.

这样,您就完成了Firebase的设置。 现在该到Xcode并编写一些代码了。

使用Firebase进行身份验证 (Authenticate with Firebase)

Let’s get started by replacing the hardcoded OAuth client ID with the client ID value contained in GoogleService-Info.plist. You can retrieve the client ID value from the plist file using the following line of code.

首先,将硬编码的OAuth客户端ID替换为GoogleService-Info.plist包含的客户端ID值。 您可以使用以下代码行从plist文件中检索客户端ID值。

FirebaseApp.app()?.options.clientID
Retrieve client ID from GoogleService-Info.plist using Swift
GoogleService-Info.plist GoogleService-Info.plist检索客户端ID

After that, we will need to modify the sign(_:didSignInFor:withError:) delegate method so that the sample app will authenticate with Firebase after a user finishes signing in with Google.

之后,我们将需要修改sign(_:didSignInFor:withError:)委托方法,以便在用户完成Google登录后,示例应用将通过Firebase进行身份验证。

As can be seen from the code snippet above, once a user successfully signed in with Google, we will retrieve the credential object using the Google ID token and access token. After getting the credential object, we will use it to authenticate with Firebase.

从上面的代码片段可以看出,用户成功登录Google后,我们将使用Google ID令牌和访问令牌检索凭证对象。 获取凭据对象后,我们将使用它来向Firebase进行身份验证。

Lastly, let’s head over to the view controller and import the Firebase SDK.

最后,让我们转到视图控制器并导入Firebase SDK。

import Firebase

After that, modify the signOutButtonTapped(_ sender: UIButton) method to perform Firebase sign out once a user successfully signed out from Google.

此后,请修改signOutButtonTapped(_ sender: UIButton)方法,以在用户成功从Google注销后执行Firebase注销。

With that, we have completed all the steps and code changes required for Firebase Authentication integration. You can run your sample app to test out the integration.

至此,我们完成了Firebase身份验证集成所需的所有步骤和代码更改。 您可以运行示例应用程序以测试集成。

Go ahead and perform a sign-in action using your sample app. Once you successfully signed in, head over to Firebase Console and verify that a new user has been created.

继续并使用示例应用程序执行登录操作。 成功登录后,转到Firebase控制台并验证是否已创建新用户。

New user created in Firebase console
New user created in Firebase console
在Firebase控制台中创建的新用户

Congratulations! After all this hard work, you have successfully integrated Google Sign-In with Firebase Authentication. 🥳

恭喜你! 经过所有这些努力,您已经成功将Google登录与Firebase身份验证集成。 🥳

结语 (Wrapping Up)

I hope this article can give you a good idea on what should be done when it comes to integrating Google Sign-in with Firebase Authentication.

希望本文能使您对将Google登录与Firebase身份验证集成时应该采取的措施有所了解。

I will be covering some other topics related to Google Sign-in and Firebase in the near future. If you would like to get notified when a new article comes out, feel free to follow me on Twitter.

我将在不久的将来讨论与Google登录和Firebase相关的其他一些主题。 如果您想在新文章发表时得到通知,请随时在Twitter上关注我。

Thanks for reading and happy coding! 👨🏼‍💻

感谢您的阅读和愉快的编码! ‍💻

翻译自: https://medium.com/@kahseng.lee123/integrate-google-sign-in-with-firebase-authentication-ff0b4ede7f07

firebase 谷歌登录

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐