Firebase电话认证iOS

时间:2018-07-13 09:11:47

标签: ios swift firebase firebase-authentication

我目前正在使用Firebase电话身份验证来对用户进行身份验证,并且可以正常工作,但是当我关闭应用程序并再次打开它时,我将重定向到“身份验证”表单,并且每次都收到身份验证代码。我不要这种行为。有没有一种方法可以检查当前用户或电话号码是否已通过身份验证,而无需将用户保存到数据库中

1 个答案:

答案 0 :(得分:0)

您可以像在firebase中检查currentUser一样执行此操作,如果currentUser的电话号码与textField中提供的电话号码相同,则可以绕过身份验证并将用户带到主屏幕。这是可以完成的方法。

if Auth.auth().currentUser != nil {
        // USER IS SIGNED IN, BUT STILL WE HAVE TO CHECK, IF THE SAME USER IS SIGNING IN OR DIFFERENT.
        if let user = Auth.auth().currentUser {
            let phone = user.phone
            if phone == YOUR TEXTFIELD VALUE OF PHONE {
                // YOU HAVE THE LOGGED IN USER NOW, YOU CAN TAKE USER TO HOME SCREEN
            } else {
                // SIGNOUT THE CURRENT USER AND DO THE AUTHENTICATION FOR NEW USER
                let firebaseAuth = Auth.auth()
                do {
                    try firebaseAuth.signOut()
                } catch let signOutError as NSError {
                    print ("Error signing out: %@", signOutError)
                }

                // No USER IS SIGNED IN, SO GET CREDENTIAL FROM YOUR SERVER AND SEND IT TO AUTH
                Auth.auth().signIn(withCustomToken: customToken ?? "") { (user, error) in
                    // YOU HAVE THE LOGGED IN USER NOW, YOU CAN TAKE USER TO HOME SCREEN
                }
            }
        }
    } else {
        // No USER IS SIGNED IN, SO GET CREDENTIAL FROM YOUR SERVER AND SEND IT TO AUTH
        Auth.auth().signIn(withCustomToken: customToken ?? "") { (user, error) in
            // YOU HAVE THE LOGGED IN USER NOW, YOU CAN TAKE USER TO HOME SCREEN
        }
    }

如果这是您想要实现的,请检查该过程。