使用现有用户登录应用-Android手机验证Firebase-

问题描述 投票:0回答:2

我正在开发使用Firebase电话身份验证方法的应用程序。它适合初次使用的用户,他们获得短信代码并在Firebase控制台中注册。但是,如果他们退出或重新安装该应用程序,则他们将无法访问应用程序菜单,因为它们已经在Firebase控制台数据库中。

我不知道如何告诉应用程序用户是否已经注册,然后打算进行主要活动,如果尚未注册,则转到注册活动。我知道如何获取当前用户,但是如果该用户存在于Firebase数据库中,我不知道如何跳过验证步骤。

希望你能得到我。

这是我的代码。


    private static final String KEY = "userAuth";
    private static final String TAG = "PhoneAuthActivity";
    private static final String KEY_VERIFY_IN_PROGRESS = "key_verify_in_progress";

    // [START declare_auth]
    private FirebaseAuth mAuth;
    // [END declare_auth]

    private boolean mVerificationInProgress = false;
    private String mVerificationId;
    private PhoneAuthProvider.ForceResendingToken mResendToken;
    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;

    // Instance of Firebase
    private PhoneAuthProvider phoneAuthProvider;

    private String verificationId;
    // id user from DB
    private String idUser;

    private EditText activationCode;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_code);

        // Restore instance state
        if (savedInstanceState != null) {
            onRestoreInstanceState(savedInstanceState);
        }

        // [START initialize_auth]
        // Initialize Firebase Auth
        mAuth = FirebaseAuth.getInstance();
        // [END initialize_auth]

        // Initialize phone auth callbacks
        // [START phone_auth_callbacks]
        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

            @Override
            public void onVerificationCompleted(PhoneAuthCredential credential) {
                // This callback will be invoked in two situations:
                // 1 - Instant verification. In some cases the phone number can be instantly
                //     verified without needing to send or enter a verification code.
                // 2 - Auto-retrieval. On some devices Google Play services can automatically
                //     detect the incoming verification SMS and perform verification without
                //     user action.
                Log.d(TAG, "onVerificationCompleted:" + credential);
                // [START_EXCLUDE silent]
                mVerificationInProgress = false;
                // [END_EXCLUDE]

                signInWithPhoneAuthCredential(credential);
            }

            @Override
            public void onVerificationFailed(FirebaseException e) {
                // This callback is invoked in an invalid request for verification is made,
                // for instance if the the phone number format is not valid.
                Log.w(TAG, "onVerificationFailed", e);
                // [START_EXCLUDE silent]
                mVerificationInProgress = false;
                // [END_EXCLUDE]

                if (e instanceof FirebaseAuthInvalidCredentialsException) {

                } else if (e instanceof FirebaseTooManyRequestsException) {
                    // The SMS quota for the project has been exceeded
                    // [START_EXCLUDE]
                    Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.",
                            Snackbar.LENGTH_SHORT).show();
                    // [END_EXCLUDE]
                }
            }

            @Override
            public void onCodeSent(@NonNull String verificationId,
                                   @NonNull PhoneAuthProvider.ForceResendingToken token) {
                // The SMS verification code has been sent to the provided phone number, we
                // now need to ask the user to enter the code and then construct a credential
                // by combining the code with a verification ID.
                Log.d(TAG, "onCodeSent:" + verificationId);

                // Save verification ID and resending token so we can use them later
                mVerificationId = verificationId;
                mResendToken = token;
            }
        };
        // [END phone_auth_callbacks]

        activationCode = findViewById(R.id.eTextActivCode);
        progressBar = findViewById(R.id.progressBar);

        String phone = getIntent().getStringExtra("phone");
        idUser = getIntent().getStringExtra("user");
        sendVerificationCode(phone);
    }

    // [START on_start_check_user]
    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();
        if(currentUser != null){
            Intent intent = new Intent(VerifyCodeActivity.this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.putExtra("user", idUser);
            startActivity(intent);
        }

        // [START_EXCLUDE]
        if (mVerificationInProgress) {
            startPhoneNumberVerification(getIntent().getStringExtra("phone"));
        }
        // [END_EXCLUDE]
    }
    // [END on_start_check_user]

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(KEY_VERIFY_IN_PROGRESS, mVerificationInProgress);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mVerificationInProgress = savedInstanceState.getBoolean(KEY_VERIFY_IN_PROGRESS);
    }

    private void startPhoneNumberVerification(String phoneNumber) {
        // [START start_phone_auth]
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneNumber,        // Phone number to verify
                60,                 // Timeout duration
                TimeUnit.SECONDS,   // Unit of timeout
                this,               // Activity (for callback binding)
                mCallbacks);        // OnVerificationStateChangedCallbacks
        // [END start_phone_auth]

        mVerificationInProgress = true;
    }

    private void verifyPhoneNumberWithCode(String verificationId, String code) {
        // [START verify_with_code]
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
        // [END verify_with_code]
        signInWithPhoneAuthCredential(credential);
    }

    // [START resend_verification]
    private void resendVerificationCode(String phoneNumber,
                                        PhoneAuthProvider.ForceResendingToken token) {
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneNumber,        // Phone number to verify
                60,                 // Timeout duration
                TimeUnit.SECONDS,   // Unit of timeout
                this,               // Activity (for callback binding)
                mCallbacks,         // OnVerificationStateChangedCallbacks
                token);             // ForceResendingToken from callbacks
    }
    // [END resend_verification]

    // [START sign_in_with_phone]
    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            if (task.isSuccessful() || activationCode.equals("123456")) {
                                Intent intent = new Intent(VerifyCodeActivity.this, MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                intent.putExtra("user", idUser);
                                startActivity(intent);
                            } else {
                                Toast.makeText(VerifyCodeActivity.this, task.getException().getMessage(),
                                        Toast.LENGTH_LONG).show();
                            }
                        } else {
                            // Sign in failed, display a message and update the UI
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                // The verification code entered was invalid
                                // [START_EXCLUDE silent]
                                activationCode.setError("Ingresa un código válido.");
                                // [END_EXCLUDE]
                            }
                        }
                    }
                });
    }
    // [END sign_in_with_phone]


    public void verifyPhoneNumber(View view) {
        String code = activationCode.getText().toString();

        if (code.isEmpty() || code.length() < 6) {
            activationCode.setError("Ingresa un código correcto");
            activationCode.requestFocus();
            return;
        }
        verifyPhoneNumberWithCode(mVerificationId, code);
    }

    public void resendActivationCode(View view){
        resendVerificationCode(getIntent().getStringExtra("phone"), mResendToken);
    }

    private void sendVerificationCode(String number) {
        // Initialize Firebase Auth
        phoneAuthProvider = PhoneAuthProvider.getInstance();

        phoneAuthProvider.verifyPhoneNumber(number, 60, TimeUnit.SECONDS,
                TaskExecutors.MAIN_THREAD, mCallback);
    }

    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallback = new
            PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

                @Override
                public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken
                        forceResendingToken) {
                    super.onCodeSent(s, forceResendingToken);
                    verificationId = s;
                }

                @Override
                public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                    String code = phoneAuthCredential.getSmsCode();

                    if (code != null) {
                        progressBar.setVisibility(View.VISIBLE);
                        //verifyCode(code);
                    }
                }

                @Override
                public void onVerificationFailed(FirebaseException e) {
                    Toast.makeText(VerifyCodeActivity.this,
                            "Error al verificar el número de teléfono: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
            };
} ```
java android firebase authentication
2个回答
0
投票

Dependent关于您的问题和评论,您需要了解用户之前是否已经登录或者是第一次登录,并且您没有服务器数据库来保存用户数据

但是如果您使用其他服务器数据库,我会给您两个答案

NOTE:在您的问题中,当登录是未经验证但进入菜单活动的菜单时,您是第二次写入您想要的用户依靠是不可能发生的,因为这样任何人都可以使用任何电话号码并登录,因此电话验证码就像电子邮件验证码一样以确保身份验证成功

1。第一个解决方案:如果您有其他数据库服务器在第一次用户登录中,您会将用户数据如名称和电话,位置,uid ...等保存在数据库中,因此您可以通过uid]在Firebase Phone auth与数据库之间绑定用户bwt >或phone,现在,当用户第二次通过Firebase登录时,您将通过uid或电话来检查用户是否已经在数据库中(不是第一次)或不是(用户是第一次)。

  1. 第二个解决方案:在尝试在AdditionalUserInfo内的signInWithCredential之前,先使用onComplete检查用户是否为newUser或已登录。>

    喜欢这样

  2. OnCompleteListener<AuthResult> completeListener = new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    boolean isNew = task.getResult().getAdditionalUserInfo().isNewUser();
                    Log.d("MyTAG", "onComplete: " + (isNew ? "new user" : "old user"));
                }
            }
        };
    

希望对您有帮助

我发现这是使用Firebase身份验证的最简单方法;比你可以使用这样的东西;

private FirebaseAuth firebaseAuth;
if (firebaseAuth.getCurrentUser() != null) 
{
// go to home activity 
}
if (firebaseAuth.getCurrentUser() = null
{
//go to login activity
}

但是您需要注意的是,您还要在FirebaseAuth中注销用户。像这样;

FirebaseAuth.getInstance().signOut();

希望有所帮助!


0
投票

我发现这是使用Firebase身份验证的最简单方法;比你可以使用这样的东西;

© www.soinside.com 2019 - 2024. All rights reserved.