📜  使用 google popup firebase 登录 (1)

📅  最后修改于: 2023-12-03 15:36:28.262000             🧑  作者: Mango

使用 Google Popup Firebase 登录

Firebase 是 Google 推出的一项云服务,其中包括了实时数据库、身份验证、云存储、云函数等多种功能。其中,身份验证模块支持使用 Google、Facebook、Twitter、Github、Apple 等第三方身份验证方式,方便开发者实现用户身份鉴别和授权管理。

在这里,我们将介绍如何使用 Google 登录模块实现 Firebase 的身份验证功能,其中采用了 Firebase 提供的弹出窗口界面,用户可以在界面中输入 Google 账号和密码完成登录操作。

前置条件

在使用 Firebase 身份验证功能前,需要完成以下几个步骤:

  1. 创建 Firebase 项目并启用身份验证功能
  2. 在 Firebase 控制台中注册 app 并下载配置文件(GoogleService-Info.plist 或 google-services.json)
  3. 安装 Firebase SDK 并引入项目中
  4. 在项目中集成 Google SDK 以支持 Google 登录
准备工作
下载 Google SDK

在项目中集成 Google 登录前,需要先下载 Google SDK。Google SDK 支持 CocoaPods 安装,打开终端并执行以下命令进行安装:

pod 'GoogleSignIn'

注意,由于 Google SDK 最新版已禁止了 UI 相关功能,因此如果需要使用弹出窗口登录界面,需要使用 Google 单独提供的 SDK,可以通过以下命令进行安装:

pod 'GoogleSignIn/GoogleSignIn'
集成 Google SDK

完成 Google SDK 的下载后,需要在项目中添加相关引用。首先打开应用程序的 AppDelegate.swift 文件,添加以下代码进行引用:

import GoogleSignIn

在 AppDelegate 类中添加以下代码以初始化 Google SDK:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    GIDSignIn.sharedInstance()?.clientID = FirebaseApp.app()?.options.clientID
    return true
}

在 Firebase 控制台中启用 Google 登录功能后,Firebase 将自动填充在此处使用的 FirebaseApp 选项。此代码将在应用程序启动后初始化 Google SDK,并将 Firebase 身份验证模块与 Google 登录相关联。

实现界面

在 AppDelegate 中初始化过程之后,下一步是实现用户界面。对于 Google 登录功能,Firebase 提供了内置的弹出窗口界面。在项目的 ViewController.swift 文件中添加以下代码以显示弹出窗口:

import Firebase
import GoogleSignIn

class ViewController: UIViewController, GIDSignInDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        GIDSignIn.sharedInstance()?.presentingViewController = self
        GIDSignIn.sharedInstance()?.delegate = self

        let signInButton = GIDSignInButton()
        signInButton.center = view.center
        view.addSubview(signInButton)
    }

    // ...
}

代码中主要完成了以下几个功能:

  • 设置 GIDSignInDelegate 以处理 Google 登录结果
  • 将显示窗口设置为当前视图控制器
  • 添加 Google 登录按钮到当前视图中

这里的 signInButton 是 Google SignIn SDK 提供的登录控件,从视觉上来看,这就是一个 Google 登录按钮,我们把它添加到界面中,会自动弹出一个界面,提示用户输入 Google 账号和密码完成鉴定操作。

然后,需要实现 GIDSignInDelegate 接口中的两个方法以处理 Google 登录的结果。

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
    // 登录成功
    if let authentication = user.authentication {
        let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
        Auth.auth().signIn(with: credential) { (authResult, error) in
            if let error = error {
                // 登录失败
                print("Authentication error:", error.localizedDescription)
            } else {
                // 登录成功
                print("Authentication success!")
            }
        }
    } else {
        // 登录失败
        print("Authentication error:", error?.localizedDescription ?? "Unknown error")
    }
}

func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
    // 断开连接
}

代码中将用户通过 Google 身份验证之后得到的身份令牌 ID 和访问令牌 Token 传递给 Firebase 身份验证模块,实现登录功能。

至此,我们已经完成了使用 Google Popup Firebase 登录的全部过程。在此过程中我们主要涉及到了 Google SDK、Firebase SDK 和 Google SignIn SDK 的集成使用。弹出窗口界面提供了一种友好的用户界面,方便用户进行操作,增加了应用程序的易用性。