简体   繁体   English

可以使用 Firebase Auth.auth() 函数检索 GoogleSignIn 用户吗?

[英]Can Firebase Auth.auth() functions be used to retrieve a GoogleSignIn user?

Within my app, I have 2 kinds of authentication:在我的应用程序中,我有两种身份验证:

  1. Email address Email 地址
  2. Google Sign In谷歌登录

This is the relevant code for creating a user when they authenticate with their email address:这是在用户使用其 email 地址进行身份验证时创建用户的相关代码:

// Create the user
Auth.auth().createUser(withEmail: email, password: password) { (result, err) in
    // Check for errors
    if err != nil {
        
        // There was an error creating the user
        self.showError("Error creating user")
    }
    else {
        
        // User was created successfully, now store the first name and last name
        UserService.createProfile(userId: Auth.auth().currentUser!.uid, firstName: firstName, lastName: lastName, email: email) { (user) in
        }
    }
}

For users who authenticate with their email address, I'm already able to successfully call several functions for them all over my app.对于使用 email 地址进行身份验证的用户,我已经能够在我的应用程序中为他们成功调用多个函数。 For example:例如:

  • To get the user's unique ID: let currentUser = Auth.auth().currentUser?.uid获取用户的唯一 ID: let currentUser = Auth.auth().currentUser?.uid
  • To check is a user is signed in when opening the app: Auth.auth().addStateDidChangeListener { (auth, user) in }打开应用程序时检查用户是否登录: Auth.auth().addStateDidChangeListener { (auth, user) in }

This is what I have in my AppDelegate to sign in a user with Google: func sign(_ signIn: GIDSignIn,: didSignInFor user, GIDGoogleUser:, withError error: Error!) {}这是我的AppDelegate中用于使用 Google 登录用户的内容: func sign(_ signIn: GIDSignIn,: didSignInFor user, GIDGoogleUser:, withError error: Error!) {}

Is there a way to use those existing Auth.auth() functions for Google-authenticated users?有没有办法为谷歌认证的用户使用那些现有的Auth.auth()函数? Or would I need to add new code all over my app to account for these users?或者我是否需要在我的整个应用程序中添加新代码来说明这些用户?

I'm not sure I understand, but will try to answer the most likely questions.我不确定我是否理解,但会尝试回答最有可能的问题。

If you want to sign the Google-authenticated user in to Firebase, you can do so by calling Auth.auth().signIn(with: credential) as shown in the documentation on authenticating with Firebase after signing in with Google .如果您想将经过 Google 身份验证的用户登录到 Firebase,您可以通过调用Auth.auth().signIn(with: credential)来完成此操作,如在使用 Google 登录后使用 Firebase 进行身份验证的文档中所示。

Once a user signs in with Firebase Authentication (no matter with what provider) the auth.currentUser will bet set for them (and cleared when they sign out of Firebase), and addStateDidChangeListener will fire events for them.一旦用户使用 Firebase 身份验证登录(无论使用什么提供商), auth.currentUser将为他们设置(并在他们退出 Firebase 时清除), addStateDidChangeListener将为他们触发事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM