Google Firebase sign out and forget user in Android app - android

When I call mFirebaseAuth.signOut() or mFirebaseUser.delete() my FirebaseAuth.AuthStateListener() works correctly and returns null as FirebaseUser instance in onAuthStateChanged, I refresh UI and show "Sign in with Google" button.
But when I want to log in again, I don't see the dialog with users (I have 2 users on my device, attached the image). The app shows this dialog only in first sign in, after that it uses the same user. If I clear app's data on the settings screen I will be able to see this dialog again.
My question is how to show this dialog after every sign out.
I run this code when press Sign In button:
// in onCreate()
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 /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
// in OnClickListener
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, FirebaseActivity.REQUEST_SIGN_IN);
In onActivityResult(), I get an instance of GoogleSignInResult so everything I need happens after I call startActivityForResult().

In the Firebase Auth Quickstart sample code, the sign-out for Google provider includes these steps. Are you calling GoogleSignInClient.signOut() when you sign-out?
private void signOut() {
// Firebase sign out
mAuth.signOut();
// Google sign out
mGoogleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
updateUI(null);
}
});
}

Another option is to use the FirebaseUI library. It simplifies sign in and sign out operations in a sense that it will do all the heavy lifting for you.
Kotlin
AuthUI.getInstance().signOut(this).addOnCompleteListener {
// do something here
}
Java
AuthUI.getInstance()
.signOut(ActivityMainOld.this)
.addOnCompleteListener(new OnCompleteListener<Void>(){
#Override
public void onComplete(#NonNull Task<Void> task) {
// do something here
}
});
Hope this helps

I was confused since all of the solutions required having a reference to the GoogleSignInClient, but it is actually not required for you to hold a reference to it, you can simply create a new instance and call signOut() on it.
GoogleSignIn.getClient(
getContext(),
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
).signOut();

For anyone else who wants this result (as in getting the google account options back) on a different activity.
public static void signOut() {
// Firebase sign out
mAuth.signOut();
// Google sign out
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
}
Add this on the sign in page, and before you pass to the next activity, just call SignOut().
// everything ok...
signOut();
startActivity(new Intent(SignIn.this,NextOne.class));
and then, in your other class you can call
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(NextClass.this, SignIn.class));
It's easy, and it will work. Cheers!

You can also define something like this:
private void signOut() {
mAuth.signOut();
mGoogleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Intent intent = new Intent(YourActivity.this, NextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
}

None of the above did fix the issue for me,
accepted answer requires access to mGoogleApiClient defined in login view (normally log out button is in settings view)
another answer was suggesting to logout in the login view (after successful login and just before launching to the main view). This was addressing the mGoogleApiClient accessibility issue, but the issue with this approach is every time user opens the app it ends up in login view and requires to log in every time which is not ideal.
So here is what I ended up doing and it is kind of all-round fix that support all the third-party log out options (in my case Facebook and Google)
logOutButton.setOnSingleClickListener {
FirebaseAuth.getInstance().currentUser?.getIdToken(false)?.addOnSuccessListener {
result ->
when (result.signInProvider){
"facebook.com" -> {
LoginManager.getInstance().logOut()
signOutFromApp()
}
"google.com" -> {
GoogleSignIn.getClient(
this,
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
).signOut()
signOutFromApp()
}
else -> {
signOutFromApp()
}
}
}
}
private fun signOutFromApp() {
FirebaseAuth.getInstance().signOut()
LauncherActivity.start(this) //starts login view
finish() //finish settigs view
}

private void sendToLogin() { //funtion
GoogleSignInClient mGoogleSignInClient ;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(getBaseContext(), gso);
mGoogleSignInClient.signOut().addOnCompleteListener(/*CURRENT CLASS */.this,
new OnCompleteListener<Void>() { //signout Google
#Override
public void onComplete(#NonNull Task<Void> task) {
FirebaseAuth.getInstance().signOut(); //signout firebase
Intent setupIntent = new Intent(getBaseContext(), /*To ur activity calss*/);
Toast.makeText(getBaseContext(), "Logged Out", Toast.LENGTH_LONG).show(); //if u want to show some text
setupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(setupIntent);
finish();
}
});
}
this code is written to work as copy past just read COMMENTS in code to customize it to ur needs, i prefer to send user to login

I did mGoogleSignInClient.signOut()
this is working as expected.

you can set a custom parameter for the GoogleAuthProvider to force user to re authenticate via google.
var provider = new Firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({
prompt: 'select_account'
});

Use same instances of Firebase Auth and GoogleSignInClient,
for example, If we declared and instantiated a Firebase Auth called mAuth in LoginActivity,
Then if we declare a new mAuth in other activity and try to call mAuth.login, it will not work properly.
Make Firebase Auth and GoogleSignInClient variables public and static and use the same from the other activities

For flutter use,
await GoogleSignIn().signOut();
await _auth.signOut();

Related

how I can login with facebook or something else, after that i integrate with google account to get the service of google [duplicate]

When I call mFirebaseAuth.signOut() or mFirebaseUser.delete() my FirebaseAuth.AuthStateListener() works correctly and returns null as FirebaseUser instance in onAuthStateChanged, I refresh UI and show "Sign in with Google" button.
But when I want to log in again, I don't see the dialog with users (I have 2 users on my device, attached the image). The app shows this dialog only in first sign in, after that it uses the same user. If I clear app's data on the settings screen I will be able to see this dialog again.
My question is how to show this dialog after every sign out.
I run this code when press Sign In button:
// in onCreate()
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 /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
// in OnClickListener
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, FirebaseActivity.REQUEST_SIGN_IN);
In onActivityResult(), I get an instance of GoogleSignInResult so everything I need happens after I call startActivityForResult().
In the Firebase Auth Quickstart sample code, the sign-out for Google provider includes these steps. Are you calling GoogleSignInClient.signOut() when you sign-out?
private void signOut() {
// Firebase sign out
mAuth.signOut();
// Google sign out
mGoogleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
updateUI(null);
}
});
}
Another option is to use the FirebaseUI library. It simplifies sign in and sign out operations in a sense that it will do all the heavy lifting for you.
Kotlin
AuthUI.getInstance().signOut(this).addOnCompleteListener {
// do something here
}
Java
AuthUI.getInstance()
.signOut(ActivityMainOld.this)
.addOnCompleteListener(new OnCompleteListener<Void>(){
#Override
public void onComplete(#NonNull Task<Void> task) {
// do something here
}
});
Hope this helps
I was confused since all of the solutions required having a reference to the GoogleSignInClient, but it is actually not required for you to hold a reference to it, you can simply create a new instance and call signOut() on it.
GoogleSignIn.getClient(
getContext(),
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
).signOut();
For anyone else who wants this result (as in getting the google account options back) on a different activity.
public static void signOut() {
// Firebase sign out
mAuth.signOut();
// Google sign out
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
}
Add this on the sign in page, and before you pass to the next activity, just call SignOut().
// everything ok...
signOut();
startActivity(new Intent(SignIn.this,NextOne.class));
and then, in your other class you can call
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(NextClass.this, SignIn.class));
It's easy, and it will work. Cheers!
You can also define something like this:
private void signOut() {
mAuth.signOut();
mGoogleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Intent intent = new Intent(YourActivity.this, NextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
}
None of the above did fix the issue for me,
accepted answer requires access to mGoogleApiClient defined in login view (normally log out button is in settings view)
another answer was suggesting to logout in the login view (after successful login and just before launching to the main view). This was addressing the mGoogleApiClient accessibility issue, but the issue with this approach is every time user opens the app it ends up in login view and requires to log in every time which is not ideal.
So here is what I ended up doing and it is kind of all-round fix that support all the third-party log out options (in my case Facebook and Google)
logOutButton.setOnSingleClickListener {
FirebaseAuth.getInstance().currentUser?.getIdToken(false)?.addOnSuccessListener {
result ->
when (result.signInProvider){
"facebook.com" -> {
LoginManager.getInstance().logOut()
signOutFromApp()
}
"google.com" -> {
GoogleSignIn.getClient(
this,
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
).signOut()
signOutFromApp()
}
else -> {
signOutFromApp()
}
}
}
}
private fun signOutFromApp() {
FirebaseAuth.getInstance().signOut()
LauncherActivity.start(this) //starts login view
finish() //finish settigs view
}
private void sendToLogin() { //funtion
GoogleSignInClient mGoogleSignInClient ;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(getBaseContext(), gso);
mGoogleSignInClient.signOut().addOnCompleteListener(/*CURRENT CLASS */.this,
new OnCompleteListener<Void>() { //signout Google
#Override
public void onComplete(#NonNull Task<Void> task) {
FirebaseAuth.getInstance().signOut(); //signout firebase
Intent setupIntent = new Intent(getBaseContext(), /*To ur activity calss*/);
Toast.makeText(getBaseContext(), "Logged Out", Toast.LENGTH_LONG).show(); //if u want to show some text
setupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(setupIntent);
finish();
}
});
}
this code is written to work as copy past just read COMMENTS in code to customize it to ur needs, i prefer to send user to login
I did mGoogleSignInClient.signOut()
this is working as expected.
you can set a custom parameter for the GoogleAuthProvider to force user to re authenticate via google.
var provider = new Firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({
prompt: 'select_account'
});
Use same instances of Firebase Auth and GoogleSignInClient,
for example, If we declared and instantiated a Firebase Auth called mAuth in LoginActivity,
Then if we declare a new mAuth in other activity and try to call mAuth.login, it will not work properly.
Make Firebase Auth and GoogleSignInClient variables public and static and use the same from the other activities
For flutter use,
await GoogleSignIn().signOut();
await _auth.signOut();

How to logout of Google Sign-In?

How do I logout out of Google Sign In?
I know that I would be able to call mGoogleSignInClient.signOut(), but I create the mGoogleSignInClient in my login activity. How could I access it in my settings activity (where the logout happens)?
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("id_token")
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(this, gso)
In my settings activity, where I have the logout button, I want to be able to call some static method relating to the Google Sign-In SDK and logout.
Do I really need to repeat the steps above (which I implemented in my login activity) into my settings activity (where the logout button is)?
You can use this for logout click
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(new ResultCallback<Status>() {
#Override
public void onResult(#NonNull Status status) {
//set your action after log out
}
});
According to the docs, using the signInClient:
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
// ...
}
});
As pointed out in this answer, in your login activity you can pass to GoogleSignIn.getClient() the application context instead of the activity context.
So in your settings activity you can simply do the following:
GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(getApplicationContext(), GoogleSignInOptions.DEFAULT_SIGN_IN);
signOut(googleSignInClient);
revokeAccess(googleSignInClient);
The definitions of signOut() and revokeAccess() can be found here.
I guess these answers (#arjun, #Till) don't really answer the question
Both of them assume the instance of GoogleSignInClient is present
But #rgoncalv asked: "Do I really need to repeat the steps above?"
This means: does he/she need to create the same instance of GoogleSignInClient (with the same options) to sign out as it was created in the login activity?
BTW I'm interested the same
Its a bit tricky,
I used sharedpreferences to create a flag if user is logged in or
not.
When I set the flag to logout, In my code I navigate back to login
activity
On login activity -> onStart(), if sharedpreference flag is logged
out but GoogleSignInAccount is not null , do the signout and show the
login UI.
Note, for new Login the logout wont work since user is not logged in yet.

How to prevent Firebase from remembering Google Account on Android [duplicate]

When I call mFirebaseAuth.signOut() or mFirebaseUser.delete() my FirebaseAuth.AuthStateListener() works correctly and returns null as FirebaseUser instance in onAuthStateChanged, I refresh UI and show "Sign in with Google" button.
But when I want to log in again, I don't see the dialog with users (I have 2 users on my device, attached the image). The app shows this dialog only in first sign in, after that it uses the same user. If I clear app's data on the settings screen I will be able to see this dialog again.
My question is how to show this dialog after every sign out.
I run this code when press Sign In button:
// in onCreate()
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 /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
// in OnClickListener
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, FirebaseActivity.REQUEST_SIGN_IN);
In onActivityResult(), I get an instance of GoogleSignInResult so everything I need happens after I call startActivityForResult().
In the Firebase Auth Quickstart sample code, the sign-out for Google provider includes these steps. Are you calling GoogleSignInClient.signOut() when you sign-out?
private void signOut() {
// Firebase sign out
mAuth.signOut();
// Google sign out
mGoogleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
updateUI(null);
}
});
}
Another option is to use the FirebaseUI library. It simplifies sign in and sign out operations in a sense that it will do all the heavy lifting for you.
Kotlin
AuthUI.getInstance().signOut(this).addOnCompleteListener {
// do something here
}
Java
AuthUI.getInstance()
.signOut(ActivityMainOld.this)
.addOnCompleteListener(new OnCompleteListener<Void>(){
#Override
public void onComplete(#NonNull Task<Void> task) {
// do something here
}
});
Hope this helps
I was confused since all of the solutions required having a reference to the GoogleSignInClient, but it is actually not required for you to hold a reference to it, you can simply create a new instance and call signOut() on it.
GoogleSignIn.getClient(
getContext(),
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
).signOut();
For anyone else who wants this result (as in getting the google account options back) on a different activity.
public static void signOut() {
// Firebase sign out
mAuth.signOut();
// Google sign out
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
}
Add this on the sign in page, and before you pass to the next activity, just call SignOut().
// everything ok...
signOut();
startActivity(new Intent(SignIn.this,NextOne.class));
and then, in your other class you can call
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(NextClass.this, SignIn.class));
It's easy, and it will work. Cheers!
You can also define something like this:
private void signOut() {
mAuth.signOut();
mGoogleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Intent intent = new Intent(YourActivity.this, NextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
}
None of the above did fix the issue for me,
accepted answer requires access to mGoogleApiClient defined in login view (normally log out button is in settings view)
another answer was suggesting to logout in the login view (after successful login and just before launching to the main view). This was addressing the mGoogleApiClient accessibility issue, but the issue with this approach is every time user opens the app it ends up in login view and requires to log in every time which is not ideal.
So here is what I ended up doing and it is kind of all-round fix that support all the third-party log out options (in my case Facebook and Google)
logOutButton.setOnSingleClickListener {
FirebaseAuth.getInstance().currentUser?.getIdToken(false)?.addOnSuccessListener {
result ->
when (result.signInProvider){
"facebook.com" -> {
LoginManager.getInstance().logOut()
signOutFromApp()
}
"google.com" -> {
GoogleSignIn.getClient(
this,
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
).signOut()
signOutFromApp()
}
else -> {
signOutFromApp()
}
}
}
}
private fun signOutFromApp() {
FirebaseAuth.getInstance().signOut()
LauncherActivity.start(this) //starts login view
finish() //finish settigs view
}
private void sendToLogin() { //funtion
GoogleSignInClient mGoogleSignInClient ;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(getBaseContext(), gso);
mGoogleSignInClient.signOut().addOnCompleteListener(/*CURRENT CLASS */.this,
new OnCompleteListener<Void>() { //signout Google
#Override
public void onComplete(#NonNull Task<Void> task) {
FirebaseAuth.getInstance().signOut(); //signout firebase
Intent setupIntent = new Intent(getBaseContext(), /*To ur activity calss*/);
Toast.makeText(getBaseContext(), "Logged Out", Toast.LENGTH_LONG).show(); //if u want to show some text
setupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(setupIntent);
finish();
}
});
}
this code is written to work as copy past just read COMMENTS in code to customize it to ur needs, i prefer to send user to login
I did mGoogleSignInClient.signOut()
this is working as expected.
you can set a custom parameter for the GoogleAuthProvider to force user to re authenticate via google.
var provider = new Firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({
prompt: 'select_account'
});
Use same instances of Firebase Auth and GoogleSignInClient,
for example, If we declared and instantiated a Firebase Auth called mAuth in LoginActivity,
Then if we declare a new mAuth in other activity and try to call mAuth.login, it will not work properly.
Make Firebase Auth and GoogleSignInClient variables public and static and use the same from the other activities
For flutter use,
await GoogleSignIn().signOut();
await _auth.signOut();

How to disable the Google auto-account detection on Sign In button? [duplicate]

When I call mFirebaseAuth.signOut() or mFirebaseUser.delete() my FirebaseAuth.AuthStateListener() works correctly and returns null as FirebaseUser instance in onAuthStateChanged, I refresh UI and show "Sign in with Google" button.
But when I want to log in again, I don't see the dialog with users (I have 2 users on my device, attached the image). The app shows this dialog only in first sign in, after that it uses the same user. If I clear app's data on the settings screen I will be able to see this dialog again.
My question is how to show this dialog after every sign out.
I run this code when press Sign In button:
// in onCreate()
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 /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
// in OnClickListener
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, FirebaseActivity.REQUEST_SIGN_IN);
In onActivityResult(), I get an instance of GoogleSignInResult so everything I need happens after I call startActivityForResult().
In the Firebase Auth Quickstart sample code, the sign-out for Google provider includes these steps. Are you calling GoogleSignInClient.signOut() when you sign-out?
private void signOut() {
// Firebase sign out
mAuth.signOut();
// Google sign out
mGoogleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
updateUI(null);
}
});
}
Another option is to use the FirebaseUI library. It simplifies sign in and sign out operations in a sense that it will do all the heavy lifting for you.
Kotlin
AuthUI.getInstance().signOut(this).addOnCompleteListener {
// do something here
}
Java
AuthUI.getInstance()
.signOut(ActivityMainOld.this)
.addOnCompleteListener(new OnCompleteListener<Void>(){
#Override
public void onComplete(#NonNull Task<Void> task) {
// do something here
}
});
Hope this helps
I was confused since all of the solutions required having a reference to the GoogleSignInClient, but it is actually not required for you to hold a reference to it, you can simply create a new instance and call signOut() on it.
GoogleSignIn.getClient(
getContext(),
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
).signOut();
For anyone else who wants this result (as in getting the google account options back) on a different activity.
public static void signOut() {
// Firebase sign out
mAuth.signOut();
// Google sign out
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
}
Add this on the sign in page, and before you pass to the next activity, just call SignOut().
// everything ok...
signOut();
startActivity(new Intent(SignIn.this,NextOne.class));
and then, in your other class you can call
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(NextClass.this, SignIn.class));
It's easy, and it will work. Cheers!
You can also define something like this:
private void signOut() {
mAuth.signOut();
mGoogleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Intent intent = new Intent(YourActivity.this, NextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
}
None of the above did fix the issue for me,
accepted answer requires access to mGoogleApiClient defined in login view (normally log out button is in settings view)
another answer was suggesting to logout in the login view (after successful login and just before launching to the main view). This was addressing the mGoogleApiClient accessibility issue, but the issue with this approach is every time user opens the app it ends up in login view and requires to log in every time which is not ideal.
So here is what I ended up doing and it is kind of all-round fix that support all the third-party log out options (in my case Facebook and Google)
logOutButton.setOnSingleClickListener {
FirebaseAuth.getInstance().currentUser?.getIdToken(false)?.addOnSuccessListener {
result ->
when (result.signInProvider){
"facebook.com" -> {
LoginManager.getInstance().logOut()
signOutFromApp()
}
"google.com" -> {
GoogleSignIn.getClient(
this,
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
).signOut()
signOutFromApp()
}
else -> {
signOutFromApp()
}
}
}
}
private fun signOutFromApp() {
FirebaseAuth.getInstance().signOut()
LauncherActivity.start(this) //starts login view
finish() //finish settigs view
}
private void sendToLogin() { //funtion
GoogleSignInClient mGoogleSignInClient ;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(getBaseContext(), gso);
mGoogleSignInClient.signOut().addOnCompleteListener(/*CURRENT CLASS */.this,
new OnCompleteListener<Void>() { //signout Google
#Override
public void onComplete(#NonNull Task<Void> task) {
FirebaseAuth.getInstance().signOut(); //signout firebase
Intent setupIntent = new Intent(getBaseContext(), /*To ur activity calss*/);
Toast.makeText(getBaseContext(), "Logged Out", Toast.LENGTH_LONG).show(); //if u want to show some text
setupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(setupIntent);
finish();
}
});
}
this code is written to work as copy past just read COMMENTS in code to customize it to ur needs, i prefer to send user to login
I did mGoogleSignInClient.signOut()
this is working as expected.
you can set a custom parameter for the GoogleAuthProvider to force user to re authenticate via google.
var provider = new Firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({
prompt: 'select_account'
});
Use same instances of Firebase Auth and GoogleSignInClient,
for example, If we declared and instantiated a Firebase Auth called mAuth in LoginActivity,
Then if we declare a new mAuth in other activity and try to call mAuth.login, it will not work properly.
Make Firebase Auth and GoogleSignInClient variables public and static and use the same from the other activities
For flutter use,
await GoogleSignIn().signOut();
await _auth.signOut();

Properly log out a user from android app

I'm developing a small android app, and basically so far it just has login and logout functionality. I'm using Firebase to store user data and also for authentication.
So I have login working and it authenticates users as it should and I have logging out working in the sense that it unauthenticates users. But is there anything I have to do from within the app to kill the session?
if (id == R.id.action_log_out) {
ref.unauth(); //End user session
startActivity(new Intent(MainActivity.this, LoginActivity.class)); //Go back to home page
finish();
}
Will this work as I think it should? Obviously if someone logs out they shouldn't be able to hit th back button and magically go back to the last page without re-logging in.
From Firebase docs
https://firebase.google.com/docs/auth/android/custom-auth
call this FirebaseAuth.getInstance().signOut();
When Firebase authenticates the user (or you authenticate the user with Firebase), it stores the token for that user in local storage on your device. This happens when you call one of the authWith... methods (of course only if it successfully authenticates the user).
Calling ref.unauth(); immediately deletes that token from local storage.
A properly implemented flow would not automatically re-authenticate them when the user presses the back button, but that depends on the flow you implement (which is missing from your question and would likely be too much code anyway).
I see 2 options for the issue we have with the back-Button after Logout:
In your LoginActivity, wich should be you launcher activity, Override onBackPressed Method and leave it empty:
#Override
public void onBackPressed() {
// empty so nothing happens
}
Or/and you can add the LoginActivityIntent in your LogoutActivty if user == null.
This way, whenever a not authenticated user lands on the activity, it will redirect to the LoginActivity instantly, although this looks kinda weird.
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG,"onAuthStateChanged:signed_out");
startActivity(new Intent(LogoutActivity.this, LoginActivity.class));
}
// ...
}
};
First Option is easier, but I guess if you apply both your on the save side ^^ Im coding for 2 weeks now so correct me if im wrong.
You can replace finish() with finishAffinity();
Delete tokens and Instance IDs
String authorizedEntity = PROJECT_ID;
String scope = "GCM";
FirebaseInstanceID.getInstance(context).deleteToken(authorizedEntity,scope);
You can also delete the Instance ID itself, including all associated tokens. The next time you call getInstance() you will get a new Instance ID:
FirebaseInstanceID.getInstance(context).deleteInstanceID();
String newIID = InstanceID.getInstance(context).getId();
private void sendToLogin() { //funtion
GoogleSignInClient mGoogleSignInClient ;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(getBaseContext(), gso);
mGoogleSignInClient.signOut().addOnCompleteListener(/*CURRENT CLASS */.this,
new OnCompleteListener<Void>() { //signout Google
#Override
public void onComplete(#NonNull Task<Void> task) {
FirebaseAuth.getInstance().signOut(); //signout firebase
Intent setupIntent = new Intent(getBaseContext(), /*To ur activity calss*/);
Toast.makeText(getBaseContext(), "Logged Out", Toast.LENGTH_LONG).show(); //if u want to show some text
setupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(setupIntent);
finish();
}
});
}
this code is written to work as copy past just read COMMENTS in code to customize it to ur needs, i prefer to send user to login

Categories

Resources