Skip to content

Commit bdcdb3c

Browse files
committed
Add sign out
1 parent 8e69564 commit bdcdb3c

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

auth/app/src/main/java/com/google/firebase/quickstart/auth/GoogleSignInActivity.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
import android.os.Bundle;
2222
import android.os.CancellationSignal;
2323
import android.util.Log;
24+
25+
import androidx.annotation.NonNull;
2426
import androidx.appcompat.app.AppCompatActivity;
27+
import androidx.credentials.ClearCredentialStateRequest;
2528
import androidx.credentials.Credential;
2629
import androidx.credentials.CredentialManager;
2730
import androidx.credentials.CredentialManagerCallback;
2831
import androidx.credentials.CustomCredential;
2932
import androidx.credentials.GetCredentialRequest;
3033
import androidx.credentials.GetCredentialResponse;
34+
import androidx.credentials.exceptions.ClearCredentialException;
3135
import androidx.credentials.exceptions.GetCredentialException;
3236
import com.google.android.libraries.identity.googleid.GetGoogleIdOption;
3337
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential;
@@ -126,7 +130,7 @@ private void createGoogleIdToken(Credential credential) {
126130
// Sign in to Firebase with using the token
127131
firebaseAuthWithGoogle(googleIdTokenCredential.getIdToken());
128132
} else {
129-
Log.d(TAG, "Credential is not of type Google ID!");
133+
Log.w(TAG, "Credential is not of type Google ID!");
130134
}
131135
}
132136
// [END create_google_id_token]
@@ -150,6 +154,31 @@ private void firebaseAuthWithGoogle(String idToken) {
150154
}
151155
// [END auth_with_google]
152156

157+
// [START sign_out]
158+
private void signOut() {
159+
// Firebase sign out
160+
mAuth.signOut();
161+
162+
// When a user signs out, clear the current user credential state from all credential providers.
163+
ClearCredentialStateRequest clearRequest = new ClearCredentialStateRequest();
164+
credentialManager.clearCredentialStateAsync(
165+
clearRequest,
166+
new CancellationSignal(),
167+
Executors.newSingleThreadExecutor(),
168+
new CredentialManagerCallback<>() {
169+
@Override
170+
public void onResult(@NonNull Void result) {
171+
updateUI(null);
172+
}
173+
174+
@Override
175+
public void onError(@NonNull ClearCredentialException e) {
176+
Log.e(TAG, "Couldn't clear user credentials: " + e.getLocalizedMessage());
177+
}
178+
});
179+
}
180+
// [END sign_out]
181+
153182
private void updateUI(FirebaseUser user) {
154183

155184
}

auth/app/src/main/java/com/google/firebase/quickstart/auth/kotlin/GoogleSignInActivity.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package com.google.firebase.quickstart.auth.kotlin
33
import android.os.Bundle
44
import android.util.Log
55
import androidx.appcompat.app.AppCompatActivity
6+
import androidx.credentials.ClearCredentialStateRequest
67
import androidx.credentials.Credential
78
import androidx.credentials.CredentialManager
89
import androidx.credentials.CustomCredential
910
import androidx.credentials.GetCredentialRequest
11+
import androidx.credentials.exceptions.ClearCredentialException
1012
import androidx.credentials.exceptions.GetCredentialException
1113
import androidx.lifecycle.lifecycleScope
1214
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
@@ -75,8 +77,8 @@ class GoogleSignInActivity : AppCompatActivity() {
7577
try {
7678
// Launch Credential Manager UI
7779
val result = credentialManager.getCredential(
78-
request = request,
79-
context = baseContext
80+
context = baseContext,
81+
request = request
8082
)
8183

8284
// Extract credential from the result returned by Credential Manager
@@ -98,7 +100,7 @@ class GoogleSignInActivity : AppCompatActivity() {
98100
// Sign in to Firebase with using the token
99101
firebaseAuthWithGoogle(googleIdTokenCredential.idToken)
100102
} else {
101-
Log.d(TAG, "Credential is not of type Google ID!")
103+
Log.w(TAG, "Credential is not of type Google ID!")
102104
}
103105
}
104106
// [END create_google_id_token]
@@ -122,6 +124,24 @@ class GoogleSignInActivity : AppCompatActivity() {
122124
}
123125
// [END auth_with_google]
124126

127+
// [START sign_out]
128+
private fun signOut() {
129+
// Firebase sign out
130+
auth.signOut()
131+
132+
// When a user signs out, clear the current user credential state from all credential providers.
133+
lifecycleScope.launch {
134+
try {
135+
val clearRequest = ClearCredentialStateRequest()
136+
credentialManager.clearCredentialState(clearRequest)
137+
updateUI(null)
138+
} catch (e: ClearCredentialException) {
139+
Log.e(TAG, "Couldn't clear user credentials: ${e.localizedMessage}")
140+
}
141+
}
142+
}
143+
// [END sign_out]
144+
125145
private fun updateUI(user: FirebaseUser?) {
126146
}
127147

0 commit comments

Comments
 (0)