Skip to content

Commit b75ae82

Browse files
author
avishalom
authored
Merge pull request #23 from firebase/align2
Align the comments in the check revoked (verify id token)
2 parents a50e8f1 + 6ba99d9 commit b75ae82

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

auth/src/main/java/com/google/firebase/quickstart/AuthSnippets.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import com.google.firebase.FirebaseOptions;
66
import com.google.firebase.auth.ExportedUserRecord;
77
import com.google.firebase.auth.FirebaseAuth;
8+
import com.google.firebase.auth.FirebaseAuthException;
89
import com.google.firebase.auth.FirebaseToken;
910
import com.google.firebase.auth.ListUsersPage;
1011
import com.google.firebase.auth.UserRecord;
1112
import com.google.firebase.auth.UserRecord.CreateRequest;
1213
import com.google.firebase.auth.UserRecord.UpdateRequest;
14+
import com.google.firebase.database.*;
1315
import java.io.FileInputStream;
1416
import java.io.IOException;
1517
import java.util.HashMap;
@@ -216,13 +218,14 @@ public static void verifyIdTokenCheckRevoked(String idToken) throws InterruptedE
216218
FirebaseToken decodedToken = FirebaseAuth.getInstance().verifyIdTokenAsync(idToken, checkRevoked).get();
217219
// Token is valid and not revoked.
218220
String uid = decodedToken.getUid();
219-
}
220-
catch (FirebaseAuthException e) {
221-
if ("id-token-revoked".equals(e.getErrorCode())) {
222-
// Token is valid but has been revoked.
223-
// When this occurs, inform the user to reauthenticate or signOut() the user.
224-
} else {
225-
// Token is invalid.
221+
} catch (ExecutionException e) {
222+
if (e.getCause() instanceof FirebaseAuthException) {
223+
FirebaseAuthException authError = (FirebaseAuthException) e.getCause();
224+
if (authError.getErrorCode().equals("id-token-revoked")) {
225+
// Token has been revoked. Inform the user to reauthenticate or signOut() the user.
226+
} else {
227+
// Token is invalid.
228+
}
226229
}
227230
}
228231
// [END verify_id_token_check_revoked]
@@ -231,7 +234,7 @@ public static void verifyIdTokenCheckRevoked(String idToken) throws InterruptedE
231234
public static void revokeIdTokens(String idToken) throws InterruptedException, ExecutionException {
232235
String uid="someUid";
233236
// [START revoke_tokens]
234-
FirebaseToken decodedToken = FirebaseAuth.getInstance().revokeRefreshTokens(uid).get();
237+
FirebaseAuth.getInstance().revokeRefreshTokensAsync(uid).get();
235238
UserRecord user = FirebaseAuth.getInstance().getUserAsync(uid).get();
236239
// Convert to seconds as the auth_time in the token claims is in seconds too.
237240
long revocationSecond = user.getTokensValidAfterTimestamp() / 1000;
@@ -240,7 +243,9 @@ public static void revokeIdTokens(String idToken) throws InterruptedException, E
240243

241244
// [START save_revocation_in_db]
242245
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("metadata/" + uid);
243-
ref.setValueAsync(MapBuilder.of("revokeTime", revocationSecond)).get();
246+
Map<String, Object> userData = new HashMap<>();
247+
userData.put("revokeTime", revocationSecond);
248+
ref.setValueAsync(userData).get();
244249
// [END save_revocation_in_db]
245250

246251
}

0 commit comments

Comments
 (0)