简体   繁体   English

如何配置 Firebase Auth 对话框的超链接

[英]How to configue hyperlink of Firebase Auth's Dialog

While using the Google sign-in method of Firebase Authentication, the privacy policy and term of service's hyperlink not working.使用 Firebase 身份验证的 Google 登录方法时,隐私政策和服务条款的超链接不起作用。 Note on some devices there is no hyperlink.请注意,在某些设备上没有超链接。 I followed doc: https://firebase.google.com/docs/auth/android/google-signin我关注了文档: https://firebase.google.com/docs/auth/android/google-signin

Source Code: https://drive.google.com/file/d/1xTy078Wk9ttL5pcgwNv3urSjNHDqqrwN/view?usp=sharing源代码: https://drive.google.com/file/d/1xTy078Wk9ttL5pcgwNv3urSjNHDqqrwN/view?usp=sharing

Activity:活动:

public class MainActivity extends AppCompatActivity {

    GoogleSignInClient mGoogleSignInClient;
    private int RC_SIGN_IN =10996;
    private FirebaseAuth mAuth;

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

        createRequest();
        // Initialize Firebase Auth
        mAuth = FirebaseAuth.getInstance();

        findViewById(R.id.loginId).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openSomeActivityForResult();
            }
        });
    }

    private void createRequest() {

        GoogleSignInOptions gso = new GoogleSignInOptions
                .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

         mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
    }
    

    ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        Intent data = result.getData();
                        //Do that you need to do with the data

                        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
                        try {
                            // Google Sign In was successful, authenticate with Firebase
                            GoogleSignInAccount account = task.getResult(ApiException.class);
                            Log.d("onActivityResult", "firebaseAuthWithGoogle:" + account.getId());
                            firebaseAuthWithGoogle(account.getIdToken());
                        }catch (ApiException e) {
                            // Google Sign In failed, update UI appropriately
                            Log.d("onActivityResult", "Google sign in failed", e);
                        }
                    }
                }
            });

    public void openSomeActivityForResult() {
//        Intent intent = new Intent(this, SomeActivity.class);
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();

        someActivityResultLauncher.launch(signInIntent);
    }
    @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){

            startActivity(new Intent(MainActivity.this,SignOut.class));
        }
    }

    private void firebaseAuthWithGoogle(String idToken) {
        AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(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
                            Log.d("firebaseAuthWithGoogle", "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            if(user!=null){

                                startActivity(new Intent(MainActivity.this,SignOut.class));
                            }
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.d("firebaseAuthWithGoogle", "signInWithCredential:failure", task.getException());

                        }
                    }
                });
    }
}

在此处输入图像描述

在此处输入图像描述

Please note that onActivityResult is deprecated.请注意, onActivityResult已弃用。 What you should do instead, is to use registerForActivityResult .您应该做的是使用registerForActivityResult In Java, it should look like this:在 Java 中,它应该是这样的:

ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    Intent data = result.getData();
                    //Do that you need to do with the data
                }
            }
        });

public void openSomeActivityForResult() {
    Intent intent = new Intent(this, SomeActivity.class);
    someActivityResultLauncher.launch(intent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM