Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
firebase是第一次检查用户吗_Firebase_Login_Firebase Authentication - Fatal编程技术网

firebase是第一次检查用户吗

firebase是第一次检查用户吗,firebase,login,firebase-authentication,Firebase,Login,Firebase Authentication,在初始加载时,firebase告诉我,如果用户通过如下方式触发事件登录: firebase.auth().onAuthStateChanged(func...) 我想检查firebase是否还在检查。就像页面加载时显示微调器一样,等待firebase检查用户,然后显示应用程序或登录/注册表单,考虑是否找到用户 现在我只需显示页面,然后初始化firebase,然后,如果firebase找到用户,则重定向到app。传递到onAuthStateChanged的侦听器将使用null或用户实例的参数调用

在初始加载时,firebase告诉我,如果用户通过如下方式触发事件登录:

firebase.auth().onAuthStateChanged(func...)
我想检查firebase是否还在检查。就像页面加载时显示微调器一样,等待firebase检查用户,然后显示应用程序或登录/注册表单,考虑是否找到用户


现在我只需显示页面,然后初始化firebase,然后,如果firebase找到用户,则重定向到app。

传递到onAuthStateChanged的侦听器将使用
null
用户
实例的参数调用


因此,可以安全地假设Firebase正在检查调用
initializeApp
和正在调用的
onAuthStateChanged
的侦听器之间的身份验证状态。调用
initializeApp
时显示微调器,调用侦听器时将其隐藏。

Swift 4

方法1

检查用户的自动创建时间是否等于上次登录时间(如果确实是用户的首次登录,则为首次登录时间)

方法2

或者,您可以在应用程序委托中设置全局变量。如果用户已经存在,我开发的大多数应用程序都会使用自动Firebase登录;这意味着它不会更新lastSignInDate值,因此仍将用户显示为新用户

因此,首先在类上方的AppDelegate中创建一个变量,如下所示:

var newUser = false

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate{
然后,无论何时调用函数创建新的Firebase用户,请将newUser设置为true:

newUser = true
最后,做一个if语句,过滤主控制器正在接收的用户:

Override func viewDidLoad() {
   super.viewDidLoad()
      if newUser == true{
          print("welcome new user")

        showOnboarding()
      }
      else{
        print("Welcome back!")
      }
}
现在,只要现有用户登录,变量将保持为false。您可以使用成功登录中返回的对象,通过additionalUserInfo属性确定该用户是否是新用户:

Auth.auth().signIn(with: credential) { (authResult, error) in
  if let error = error {
     print(error.localizedDescription)
     return
  }
  // User is signed in
  // ...

  //Check if new user
  if let isNewUser: Bool = authResult?.additionalUserInfo?.isNewUser {
     if isNewUser {
        print("new user")
     }
  }
}

谢谢我没有注意到这个监听器也是用
null
调用的。我的错。对我来说,newUserRref?.creationDate?.TimeIntervalencesince1970和newUserRref?.lastSignInDate?.TimeIntervalencesince197几乎相同,但在小数点后第三位左右有所不同(一个是1553623223.055,另一个是1553623223.057)。我添加了一个方法来截断这些,然后方法1起作用了。
Auth.auth().signIn(with: credential) { (authResult, error) in
  if let error = error {
     print(error.localizedDescription)
     return
  }
  // User is signed in
  // ...

  //Check if new user
  if let isNewUser: Bool = authResult?.additionalUserInfo?.isNewUser {
     if isNewUser {
        print("new user")
     }
  }
}