Skip to content

Add Credential Manager to Auth samples #610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 14, 2025
Prev Previous commit
Next Next commit
Update method name to match Credential Manager docs, merge Firebase s…
…ign out with clearing credential state
  • Loading branch information
marinacoelho committed Feb 14, 2025
commit 35d2acc3abf943bb2351f2e5357f6f5bf35602a3
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private void launchCredentialManager() {
// Instantiate a Google sign-in request
GetGoogleIdOption googleIdOption = new GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(true)
.setServerClientId(getBaseContext().getString(R.string.default_web_client_id))
.setServerClientId(getString(R.string.default_web_client_id))
.build();

// Create the Credential Manager request
Expand All @@ -107,7 +107,7 @@ private void launchCredentialManager() {
@Override
public void onResult(GetCredentialResponse result) {
// Extract credential from the result returned by Credential Manager
createGoogleIdToken(result.getCredential());
handleSignIn(result.getCredential());
}

@Override
Expand All @@ -118,8 +118,8 @@ public void onError(GetCredentialException e) {
);
}

// [START create_google_id_token]
private void createGoogleIdToken(Credential credential) {
// [START handle_sign_in]
private void handleSignIn(Credential credential) {
// Check if credential is of type Google ID
if (credential instanceof CustomCredential customCredential
&& credential.getType().equals(TYPE_GOOGLE_ID_TOKEN_CREDENTIAL)) {
Expand All @@ -133,7 +133,7 @@ private void createGoogleIdToken(Credential credential) {
Log.w(TAG, "Credential is not of type Google ID!");
}
}
// [END create_google_id_token]
// [END handle_sign_in]

// [START auth_with_google]
private void firebaseAuthWithGoogle(String idToken) {
Expand All @@ -154,8 +154,11 @@ private void firebaseAuthWithGoogle(String idToken) {
}
// [END auth_with_google]

// [START clear_credential_stage]
private void clearCredentialState() {
// [START sign_out]
private void signOut() {
// Firebase sign out
mAuth.signOut();

// When a user signs out, clear the current user credential state from all credential providers.
ClearCredentialStateRequest clearRequest = new ClearCredentialStateRequest();
credentialManager.clearCredentialStateAsync(
Expand All @@ -174,7 +177,7 @@ public void onError(@NonNull ClearCredentialException e) {
}
});
}
// [END clear_credential_stage]
// [END sign_out]

private void updateUI(FirebaseUser user) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ class GoogleSignInActivity : AppCompatActivity() {
)

// Extract credential from the result returned by Credential Manager
createGoogleIdToken(result.credential)
handleSignIn(result.credential)
} catch (e: GetCredentialException) {
Log.e(TAG, "Couldn't retrieve user's credentials: ${e.localizedMessage}")
}
}
}

// [START create_google_id_token]
private fun createGoogleIdToken(credential: Credential) {
// [START handle_sign_in]
private fun handleSignIn(credential: Credential) {
// Check if credential is of type Google ID
if (credential is CustomCredential && credential.type == TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
// Create Google ID Token
Expand All @@ -106,7 +106,7 @@ class GoogleSignInActivity : AppCompatActivity() {
Log.w(TAG, "Credential is not of type Google ID!")
}
}
// [END create_google_id_token]
// [END handle_sign_in]

// [START auth_with_google]
private fun firebaseAuthWithGoogle(idToken: String) {
Expand All @@ -127,8 +127,11 @@ class GoogleSignInActivity : AppCompatActivity() {
}
// [END auth_with_google]

// [START clear_credential_stage]
private fun clearCredentialState() {
// [START sign_out]
private fun signOut() {
// Firebase sign out
auth.signOut()

// When a user signs out, clear the current user credential state from all credential providers.
lifecycleScope.launch {
try {
Expand All @@ -140,7 +143,7 @@ class GoogleSignInActivity : AppCompatActivity() {
}
}
}
// [END clear_credential_stage]
// [END sign_out]

private fun updateUI(user: FirebaseUser?) {
}
Expand Down
Loading