Firebase检查用户身份验证状态Swift

时间:2019-04-13 07:09:09

标签: swift firebase firebase-authentication

如果用户已登录,我将如何使用他们提供的代码片段转到主页,否则请在介绍页面中启动他们

我已经设置了所有的firebase框架,但是我不确定如何强制页面跳转并进而限制应用程序页面。人们可以当前登录,并且需要所有凭据字段才能继续,但是即使用户已登录,他们每次也都从进入页面开始。

这是提供的auth侦听器语句

handle = Auth.auth().addStateDidChangeListener { (auth, user) in
  // ...
}

这是指向其余文档的链接 https://firebase.google.com/docs/auth/ios/start

1 个答案:

答案 0 :(得分:0)

determine the current user in Firebase authentication有两种方法。主动的,被动的。

活动代码是最简单的。根据我链接的文档中的代码:

if Auth.auth().currentUser != nil {
  print("User is signed in.")
} else {
  print("No user is signed in.")
}

但这仅在运行用户时确定用户的状态。由于用户登录是异步进行的,因此只要您只要用户登录(或退出)就可以响应,有时会更容易。为此,您可以使用文档中的反应式代码段:

handle = Auth.auth().addStateDidChangeListener { (auth, user) in
  if user != nil {
    print("User signed in.")
  } else {
    print("No user signed in.")
  }
}
相关问题