4
4
import com .google .firebase .FirebaseApp ;
5
5
import com .google .firebase .FirebaseOptions ;
6
6
import com .google .firebase .auth .FirebaseAuth ;
7
+ import com .google .firebase .auth .FirebaseToken ;
7
8
import com .google .firebase .auth .UserRecord ;
8
9
import com .google .firebase .auth .UserRecord .CreateRequest ;
9
10
import com .google .firebase .auth .UserRecord .UpdateRequest ;
10
11
import java .io .FileInputStream ;
11
12
import java .io .IOException ;
13
+ import java .util .HashMap ;
14
+ import java .util .Map ;
12
15
import java .util .concurrent .ExecutionException ;
13
16
14
17
/**
@@ -95,6 +98,38 @@ public static void deleteUser(String uid) throws InterruptedException, Execution
95
98
// [END delete_user]
96
99
}
97
100
101
+ public static void createCustomToken () throws InterruptedException , ExecutionException {
102
+ // [START custom_token]
103
+ String uid = "some-uid" ;
104
+
105
+ String customToken = FirebaseAuth .getInstance ().createCustomTokenAsync (uid ).get ();
106
+ // Send token back to client
107
+ // [END custom_token]
108
+ System .out .println ("Created custom token: " + customToken );
109
+ }
110
+
111
+ public static void createCustomTokenWithClaims () throws InterruptedException , ExecutionException {
112
+ // [START custom_token_with_claims]
113
+ String uid = "some-uid" ;
114
+ Map <String , Object > additionalClaims = new HashMap <String , Object >();
115
+ additionalClaims .put ("premiumAccount" , true );
116
+
117
+ String customToken = FirebaseAuth .getInstance ()
118
+ .createCustomTokenAsync (uid , additionalClaims ).get ();
119
+ // Send token back to client
120
+ // [END custom_token_with_claims]
121
+ System .out .println ("Created custom token: " + customToken );
122
+ }
123
+
124
+ public static void verifyIdToken (String idToken ) throws InterruptedException , ExecutionException {
125
+ // [START verify_id_token]
126
+ // idToken comes from the client app (shown above)
127
+ FirebaseToken decodedToken = FirebaseAuth .getInstance ().verifyIdTokenAsync (idToken ).get ();
128
+ String uid = decodedToken .getUid ();
129
+ // [END verify_id_token]
130
+ System .out .println ("Decoded ID token from user: " + uid );
131
+ }
132
+
98
133
public static void main (String [] args ) throws InterruptedException , ExecutionException {
99
134
System .out .println ("Hello, AuthSnippets!" );
100
135
@@ -121,6 +156,8 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc
121
156
getUserByPhoneNumber ("+11234567890" );
122
157
updateUser ("some-uid" );
123
158
deleteUser ("some-uid" );
159
+ createCustomToken ();
160
+ createCustomTokenWithClaims ();
124
161
System .out .println ("Done!" );
125
162
}
126
163
0 commit comments