I use OAuth to let users sign in to the android app via Google account. When the user taps the Google login button for the first time, it produces a dialog to choose the account. Again, when the user logs out and decides to login with different Google account, it doesn't prompt to choose the account, it logs in the account the user had chose the previous time
'
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
initialize();
Firebase.setAndroidContext(this);
ref=new Firebase("https://texter10c.firebaseio.com");
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog.setMessage("Logging in !");
progressDialog.setTitle("Hang on!");
progressDialog.show();
ref.authWithPassword(emailField.getText().toString(), passwordField.getText().toString(), new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
Log.e("Authenticated","Authenticated");
getUserIdandLogin();
}
@Override
public void onAuthenticationError(FirebaseError firebaseError) {
progressDialog.dismiss();
Toast.makeText(LoginActivity.this, firebaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, SignupActivity.class);
startActivity(intent);
}
});
googleSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog.setTitle("Contacting Google");
progressDialog.setMessage("Logging you in");
progressDialog.show();
if(!mGoogleApiClient.isConnected())
mGoogleApiClient.connect();
}
});
}
private void getGoogleToken(){
AsyncTask<Void,Void,String> task=new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
final String scopes="oauth2:"+"https://www.googleapis.com/auth/plus.login"+" "+"https://www.googleapis.com/auth/plus.me";
try {
if(!mGoogleApiClient.isConnected())
{
mGoogleApiClient.connect();
}
googleAccessToken= GoogleAuthUtil.getToken(LoginActivity.this,Plus.AccountApi.getAccountName(mGoogleApiClient),scopes);
Log.e("AccessToken",googleAccessToken+"");
}
catch (IOException e)
{
e.printStackTrace();
}
catch (GoogleAuthException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
ref.authWithOAuthToken("google", googleAccessToken, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(final AuthData authData) {
try {
Log.e("Firebase", "Google Authentication Success");
Log.e("Username", authData.getProviderData().get("displayName").toString());
Log.e("Id", authData.getProviderData().get("id").toString());
Firebase googleUserRef = ref.child("UserDetails/names/" + authData.getProviderData().get("id").toString());
Map<String, String> googleUserMap = new HashMap<String, String>();
googleUserMap.put("Username", authData.getProviderData().get("displayName").toString());
final String UserID = "GoogleUser" + authData.getProviderData().get("displayName") + authData.getProviderData().get("id");
googleUserMap.put("UserId", UserID);
googleUserRef.setValue(googleUserMap, new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
progressDialog.dismiss();
dataStore.setCurrentUserName(authData.getProviderData().get("displayName").toString());
dataStore.setCurrentUserID(UserID);
storeDatainSharedPreferences();
Intent intent = new Intent(LoginActivity.this, DialogActivity.class);
startActivity(intent);
}
});
}
catch (NullPointerException e)
{
e.printStackTrace();
}
}
@Override
public void onAuthenticationError(FirebaseError firebaseError) {
Log.e("GogoleAuthFailed", firebaseError.getMessage());
}
});
}
catch (NullPointerException e)
{
Log.e("Accesstoken problem",e.getMessage());
}
}
};
task.execute();
}
public void getUserIdandLogin()
{
dataStore.userDialogMap=new ArrayList<Map<String,String>>();
dataStore.generatedChatIds=new ArrayList<>();
Firebase refUser=ref.child("UserDetails/names");
refUser.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String, String> map = new HashMap<String, String>();
map = dataSnapshot.getValue(Map.class);
try{
if (map.get("Email").equals(emailField.getText().toString()))
{
Toast.makeText(LoginActivity.this, "Successfilly Logged in", Toast.LENGTH_SHORT).show();
dataStore.setCurrentUserName(map.get("Username"));
dataStore.setCurrentUserID(map.get("UserId"));
intent=new Intent(LoginActivity.this,DialogActivity.class);
startActivity(intent);
storeDatainSharedPreferences();
progressDialog.dismiss();
}
}
catch (NullPointerException e)
{
Log.e("NullPointerGUser",e.getMessage());
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
private void storeDatainSharedPreferences() {
try {
SharedPreferences.Editor editor = getSharedPreferences(NEW_PREFS, MODE_PRIVATE).edit();
editor.putString("CurrentUsername", dataStore.getCurrentUserName());
editor.putString("CurrentUserId", dataStore.getCurrentUserID());
editor.commit();
}
catch (NoSuchElementException e)
{
new AlertDialog.Builder(LoginActivity.this).setMessage("There was an error whil logging in")
.setTitle("Little Problem here!").setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(LoginActivity.this,LoginActivity.class);
removeDatainSharedPreferences();
mGoogleApiClient.disconnect();
startActivity(intent);
}
}).show();
}
}
private void removeDatainSharedPreferences() {
SharedPreferences.Editor editor = getSharedPreferences(NEW_PREFS, MODE_PRIVATE).edit();
editor.remove("CurrentUsername");
editor.remove("CurrentUserId");
editor.commit();
}
private void initialize() {
emailInputLayout=(TextInputLayout)findViewById(R.id.emailInputLayout);
emailField=(EditText)findViewById(R.id.emailField);
passwordField=(EditText)findViewById(R.id.passwordField);
passwordInputLayout=(TextInputLayout)findViewById(R.id.passwordInputLayout);
loginButton=(Button)findViewById(R.id.loginButton);
emailInputLayout.setHint("Email");
passwordInputLayout.setHint("Password");
progressDialog=new ProgressDialog(LoginActivity.this);
signupButton=(TextView)findViewById(R.id.signupButton);
forgotPasswordButton=(TextView)findViewById(R.id.forgotPasswordField);
googleSignInButton=(SignInButton)findViewById(R.id.googleSignInButton);
googleSignInButton.setColorScheme(SignInButton.COLOR_AUTO);
forgotPasswordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(LoginActivity.this,ForgotPasswordActivity.class);
startActivity(intent);
}
});
SharedPreferences sharePreferences=getSharedPreferences(NEW_PREFS,MODE_PRIVATE);
if(!sharePreferences.getString("CurrentUsername","null").equals("null")) {
Log.e("SharedPreferences", sharePreferences.getString("CurrentUsername", "null"));
Log.e("SharedPreferences",sharePreferences.getString("CurrentUserId",null));
Intent intent=new Intent(LoginActivity.this,DialogActivity.class);
startActivity(intent);
}
mGoogleApiClient=new GoogleApiClient.Builder(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
if(!isNetworkAvailable())
{
snackbar=Snackbar.make(findViewById(android.R.id.content),"You are offline",Snackbar.LENGTH_INDEFINITE)
.setAction("Retry", new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isNetworkAvailable())
dismissSnackBar();
else
snackbar.show();
}
});
snackbar.show();
}
}
private void dismissSnackBar() {
snackbar.dismiss();
}
private boolean isNetworkAvailable()
{
ConnectivityManager manager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkIngo=manager.getActiveNetworkInfo();
return networkIngo!=null&& networkIngo.isConnected();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.e("GoogleApi","Connected");
getGoogleToken();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e("GoogleApi",connectionResult.toString());
if(!connectionResult.hasResolution())
{
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(),LoginActivity.this,0).show();
}
if(connectionResult.isSuccess())
{
getGoogleToken();
}
try
{
connectionResult.startResolutionForResult(this,100);
}
catch (IntentSender.SendIntentException e)
{
e.printStackTrace();
}
}
}
`
You needed to log out the user from google client.
public void signOut() {
mAuth.signOut();
mGoogleSignInClient.signOut();
LoginManager.getInstance().logOut();
}
Where
private FirebaseAuth mAuth;
private GoogleSignInClient mGoogleSignInClient;
and
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
mAuth = FirebaseAuth.getInstance();
Just add this signIn()
method after getting data from the intent.
Intent signInIntent=Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
mGoogleApiClient.clearDefaultAccountAndReconnect();
You have to just clear pre-signed in accounts. To do so, add the following as first line under signIn() function: mGoogleSignInClient.signOut();
private void signIn() {
// clearing previous signin caches
mGoogleSignInClient.signOut();
//getting the google signin intent
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
//starting the activity for result
startActivityForResult(signInIntent, RC_SIGN_IN);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With