如何区分第一次谷歌登录和连续谷歌登录在Android应用程序? [英] How to differentiate between first time google Sign-In and successive google sign-In in an android application?

查看:138
本文介绍了如何区分第一次谷歌登录和连续谷歌登录在Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,用户必须先注册(谷歌或电子邮件)。第一次用户将被引导到他/她将输入他们的个人细节的活动,并且返回的用户将被引导到配置文件活动。我正在使用Firebase身份验证和数据库。

I am building an app where the user has to register first (google or Email). First time users will be directed to an activity where he/she will have enter their personal-details and Returning users will be directed to profile activity. I am using firebase authentication and Database for this.

如何检查用户是否已经注册并使用他的Google ID?现在,只要用户使用谷歌登录选项,他们总是被重定向到个人细节活动,即使他们之前已经注册。

How do I check if the user has already registered and direct him to his profile activity when he logs-in using his google ID? Right Now whenever the user uses the google sign-in option they are always redirected to the personal-details activity even if they have registered before.

这是我的主要活动Google登录

This is my main activity with google Sign-In

public class MainActivity extends AppCompatActivity implements View.OnClickListener,GoogleApiClient.OnConnectionFailedListener{
private Button mainRegister;
private TextView mainsignIn;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private String TAG="MAIN_ACTIVITY";
private static int RC_SIGN_IN=0;
private GoogleApiClient mGoogleApiClient;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAuth = FirebaseAuth.getInstance();
    mainRegister=(Button)findViewById(R.id.mainregister);
    mainsignIn=(TextView)findViewById(R.id.mainSignIN);

   // googlebutton=(SignInButton)findViewById(R.id.mainGoogleSignin);


    //Listener
    mAuthListener=new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if(user != null){
                Log.d("AUTH", "User logged in: " + user.getEmail());

            }else{
                Log.d("AUTH", "User Logged out");
                }
        }
    };

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

    mGoogleApiClient= new GoogleApiClient.Builder(this)
            .enableAutoManage(this,this)
            .addApi(Auth.GOOGLE_SIGN_IN_API,gso)
            .build();

    mainRegister.setOnClickListener(this);
    mainsignIn.setOnClickListener(this);
    findViewById(R.id.mainGoogleSignin).setOnClickListener(this);
}

//googleSignIn Method
private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            // Google Sign In was successful, authenticate with Firebase
            //uncomment this app crashes
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);


       } else {
           // Google Sign In failed, update UI appropriately
            Log.d(TAG,"Google login Failed");
        }
   }
}



//use this if app dosent work
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    //Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d("AUTH", "signInWithCredential:onComplete:" + task.isSuccessful());
                    finish();
                    startActivity(new Intent(getApplicationContext(),PersonalDetailsActivity.class));

                }
            });
}

@Override
public void onStart() {
    super.onStart();
   mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

@Override
public void onClick(View v) {
        if(v==findViewById(R.id.mainGoogleSignin)){
            signIn();
            //finish();
            //startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
        }
        if(v==mainRegister){
            finish();
            startActivity(new Intent(getApplicationContext(),EmailRegisterActivity.class));
        }
        if(v==mainsignIn){
            finish();
            startActivity(new Intent(getApplicationContext(),SignInActivity.class));
        }

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(TAG,"Connection Failed");
}

}

}

推荐答案

为什么不把google登录令牌保存在本地数据库中,每当用户打开应用程序时,都要检查用户令牌(可能在你的飞溅中)是否存在,这意味着已经有一个现有的用户对该配置文件活动有意向。其他个人信息活动。

Why don't you save the google sign-in token in your local database and every time the user opens the application make a check for the user token (maybe in your splash) and if it is present it means there is already an existing user make an intent to the profile activity. Else personal-details activity.

这篇关于如何区分第一次谷歌登录和连续谷歌登录在Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆