如何在电话验证期间检查用户是否已存在于firebase中

时间:2018-05-31 01:12:17

标签: java android firebase firebase-authentication

我正在尝试创建一个仅使用来自firebase的电话授权的应用。由于登录/注册是通过相同的过程完成的,即验证发送的代码。如何检查用户是否已存在于firebase中?我需要这个来向他们展示合适的用户界面。

2 个答案:

答案 0 :(得分:1)

目前,唯一的方法是通过Firebase Admin SDK。有lookup a user by phone number的API。

<!DOCTYPE html>
<head>
    <link rel="stylesheet" type="text/css" href="photo_gallery.css"/>
    <meta charset="UTF-8"/>
    <script src="photo_gallery.js"></script>
</head>
<body>
...
</body>
</html>

答案 1 :(得分:0)

您可以通过比较用户元数据来检查用户是否已存在于Firebase中。参见代码示例:

PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, smsCode);
            FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(PhoneLoginEnterCodeActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task){
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        FirebaseUser user = task.getResult().getUser();
                        long creationTimestamp = user.getMetadata().getCreationTimestamp();
                        long lastSignInTimestamp = user.getMetadata().getLastSignInTimestamp();
                        if (creationTimestamp == lastSignInTimestamp) {
                            //do create new user
                        } else {
                           //user is exists, just do login
                        }
                    } else {
                        // Sign in failed, display a message and update the UI
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid
                        }
                    }
                }
            });
相关问题