11package com .google .firebase .quickstart ;
22
3+ import com .google .auth .oauth2 .GoogleCredentials ;
34import com .google .firebase .FirebaseApp ;
45import com .google .firebase .FirebaseOptions ;
56import com .google .firebase .auth .FirebaseAuth ;
6- import com .google .firebase .auth .FirebaseCredentials ;
77import com .google .firebase .auth .UserRecord ;
88import com .google .firebase .auth .UserRecord .CreateRequest ;
99import com .google .firebase .auth .UserRecord .UpdateRequest ;
10- import com .google .firebase .tasks .Task ;
1110import java .io .FileInputStream ;
1211import java .io .IOException ;
12+ import java .util .concurrent .ExecutionException ;
1313
1414/**
1515 * Auth snippets for documentation.
1919 */
2020public class AuthSnippets {
2121
22- public static Task < UserRecord > getUserById (String uid ) {
22+ public static void getUserById (String uid ) throws InterruptedException , ExecutionException {
2323 // [START get_user_by_id]
24- Task <UserRecord > task = FirebaseAuth .getInstance ().getUser (uid )
25- .addOnSuccessListener (userRecord -> {
26- // See the UserRecord reference doc for the contents of userRecord.
27- System .out .println ("Successfully fetched user data: " + userRecord .getUid ());
28- })
29- .addOnFailureListener (e -> {
30- System .err .println ("Error fetching user data: " + e .getMessage ());
31- });
24+ UserRecord userRecord = FirebaseAuth .getInstance ().getUserAsync (uid ).get ();
25+ // See the UserRecord reference doc for the contents of userRecord.
26+ System .out .println ("Successfully fetched user data: " + userRecord .getUid ());
3227 // [END get_user_by_id]
33-
34- return task ;
3528 }
3629
37- public static Task < UserRecord > getUserByEmail (String email ) {
30+ public static void getUserByEmail (String email ) throws InterruptedException , ExecutionException {
3831 // [START get_user_by_email]
39- Task <UserRecord > task = FirebaseAuth .getInstance ().getUserByEmail (email )
40- .addOnSuccessListener (userRecord -> {
41- // See the UserRecord reference doc for the contents of userRecord.
42- System .out .println ("Successfully fetched user data: " + userRecord .getEmail ());
43- })
44- .addOnFailureListener (e -> {
45- System .err .println ("Error fetching user data: " + e .getMessage ());
46- });
32+ UserRecord userRecord = FirebaseAuth .getInstance ().getUserByEmailAsync (email ).get ();
33+ // See the UserRecord reference doc for the contents of userRecord.
34+ System .out .println ("Successfully fetched user data: " + userRecord .getEmail ());
4735 // [END get_user_by_email]
48-
49- return task ;
5036 }
5137
52- public static Task <UserRecord > getUserByPhoneNumber (String phoneNumber ) {
38+ public static void getUserByPhoneNumber (
39+ String phoneNumber ) throws InterruptedException , ExecutionException {
5340 // [START get_user_by_phone]
54- Task <UserRecord > task = FirebaseAuth .getInstance ().getUserByPhoneNumber (phoneNumber )
55- .addOnSuccessListener (userRecord -> {
56- // See the UserRecord reference doc for the contents of userRecord.
57- System .out .println ("Successfully fetched user data: " + userRecord .getPhoneNumber ());
58- })
59- .addOnFailureListener (e -> {
60- System .err .println ("Error fetching user data: " + e .getMessage ());
61- });
41+ UserRecord userRecord = FirebaseAuth .getInstance ().getUserByPhoneNumberAsync (phoneNumber ).get ();
42+ // See the UserRecord reference doc for the contents of userRecord.
43+ System .out .println ("Successfully fetched user data: " + userRecord .getPhoneNumber ());
6244 // [END get_user_by_phone]
63-
64- return task ;
6545 }
6646
67- public static Task < UserRecord > createUser () {
47+ public static void createUser () throws InterruptedException , ExecutionException {
6848 // [START create_user]
6949 CreateRequest request = new CreateRequest ()
7050 .setEmail ("user@example.com" )
@@ -75,40 +55,24 @@ public static Task<UserRecord> createUser() {
7555 .setPhotoUrl ("http://www.example.com/12345678/photo.png" )
7656 .setDisabled (false );
7757
78- Task <UserRecord > task = FirebaseAuth .getInstance ().createUser (request )
79- .addOnSuccessListener (userRecord -> {
80- // See the UserRecord reference doc for the contents of userRecord.
81- System .out .println ("Successfully created new user: " + userRecord .getUid ());
82- })
83- .addOnFailureListener (e -> {
84- System .err .println ("Error creating new user: " + e .getMessage ());
85- });
58+ UserRecord userRecord = FirebaseAuth .getInstance ().createUserAsync (request ).get ();
59+ System .out .println ("Successfully created new user: " + userRecord .getUid ());
8660 // [END create_user]
87-
88- return task ;
8961 }
9062
91- public static Task < UserRecord > createUserWithUid () {
63+ public static void createUserWithUid () throws InterruptedException , ExecutionException {
9264 // [START create_user_with_uid]
9365 CreateRequest request = new CreateRequest ()
9466 .setUid ("some-uid" )
9567 .setEmail ("user@example.com" )
9668 .setPhoneNumber ("+11234567890" );
9769
98- Task <UserRecord > task = FirebaseAuth .getInstance ().createUser (request )
99- .addOnSuccessListener (userRecord -> {
100- // See the UserRecord reference doc for the contents of userRecord.
101- System .out .println ("Successfully created new user: " + userRecord .getUid ());
102- })
103- .addOnFailureListener (e -> {
104- System .err .println ("Error creating new user: " + e .getMessage ());
105- });
70+ UserRecord userRecord = FirebaseAuth .getInstance ().createUserAsync (request ).get ();
71+ System .out .println ("Successfully created new user: " + userRecord .getUid ());
10672 // [END create_user_with_uid]
107-
108- return task ;
10973 }
11074
111- public static Task < UserRecord > updateUser (String uid ) {
75+ public static void updateUser (String uid ) throws InterruptedException , ExecutionException {
11276 // [START update_user]
11377 UpdateRequest request = new UpdateRequest (uid )
11478 .setEmail ("user@example.com" )
@@ -119,38 +83,27 @@ public static Task<UserRecord> updateUser(String uid) {
11983 .setPhotoUrl ("http://www.example.com/12345678/photo.png" )
12084 .setDisabled (true );
12185
122- Task <UserRecord > task = FirebaseAuth .getInstance ().updateUser (request )
123- .addOnSuccessListener (userRecord -> {
124- // See the UserRecord reference doc for the contents of userRecord.
125- System .out .println ("Successfully updated user: " + userRecord .getUid ());
126- })
127- .addOnFailureListener (e -> {
128- System .err .println ("Error updating user: " + e .getMessage ());
129- });
86+ UserRecord userRecord = FirebaseAuth .getInstance ().updateUserAsync (request ).get ();
87+ System .out .println ("Successfully updated user: " + userRecord .getUid ());
13088 // [END update_user]
131-
132- return task ;
13389 }
13490
135- public static Task < Void > deleteUser (String uid ) {
91+ public static void deleteUser (String uid ) throws InterruptedException , ExecutionException {
13692 // [START delete_user]
137- Task <Void > task = FirebaseAuth .getInstance ().deleteUser (uid )
138- .addOnSuccessListener (aVoid -> System .out .println ("Successfully deleted user." ))
139- .addOnFailureListener (e -> System .err .println ("Error updating user: " + e .getMessage ()));
93+ FirebaseAuth .getInstance ().deleteUserAsync (uid ).get ();
94+ System .out .println ("Successfully deleted user." );
14095 // [END delete_user]
141-
142- return task ;
14396 }
14497
145- public static void main (String [] args ) {
98+ public static void main (String [] args ) throws InterruptedException , ExecutionException {
14699 System .out .println ("Hello, AuthSnippets!" );
147100
148101 // Initialize Firebase
149102 try {
150103 // [START initialize]
151104 FileInputStream serviceAccount = new FileInputStream ("service-account.json" );
152105 FirebaseOptions options = new FirebaseOptions .Builder ()
153- .setCredential ( FirebaseCredentials . fromCertificate (serviceAccount ))
106+ .setCredentials ( GoogleCredentials . fromStream (serviceAccount ))
154107 .build ();
155108 FirebaseApp .initializeApp (options );
156109 // [END initialize]
@@ -162,13 +115,13 @@ public static void main(String[] args) {
162115 }
163116
164117 // Smoke test
165- createUserWithUid ()
166- . continueWithTask ( task -> getUserById ("some-uid" ))
167- . continueWithTask ( task -> getUserByEmail ("user@example.com" ))
168- . continueWithTask ( task -> getUserByPhoneNumber ("+11234567890" ))
169- . continueWithTask ( task -> updateUser ("some-uid" ))
170- . continueWithTask ( task -> deleteUser ("some-uid" ))
171- . addOnCompleteListener ( task -> System .out .println ("Done! Success: " + task . isSuccessful ()) );
118+ createUserWithUid ();
119+ getUserById ("some-uid" );
120+ getUserByEmail ("user@example.com" );
121+ getUserByPhoneNumber ("+11234567890" );
122+ updateUser ("some-uid" );
123+ deleteUser ("some-uid" );
124+ System .out .println ("Done!" );
172125 }
173126
174127}
0 commit comments