44import com .google .firebase .FirebaseApp ;
55import com .google .firebase .FirebaseOptions ;
66import com .google .firebase .auth .FirebaseAuth ;
7+ import com .google .firebase .auth .FirebaseToken ;
78import com .google .firebase .auth .UserRecord ;
89import com .google .firebase .auth .UserRecord .CreateRequest ;
910import com .google .firebase .auth .UserRecord .UpdateRequest ;
1011import java .io .FileInputStream ;
1112import java .io .IOException ;
13+ import java .util .HashMap ;
14+ import java .util .Map ;
1215import java .util .concurrent .ExecutionException ;
1316
1417/**
@@ -95,6 +98,38 @@ public static void deleteUser(String uid) throws InterruptedException, Execution
9598 // [END delete_user]
9699 }
97100
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+
98133 public static void main (String [] args ) throws InterruptedException , ExecutionException {
99134 System .out .println ("Hello, AuthSnippets!" );
100135
@@ -121,6 +156,8 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc
121156 getUserByPhoneNumber ("+11234567890" );
122157 updateUser ("some-uid" );
123158 deleteUser ("some-uid" );
159+ createCustomToken ();
160+ createCustomTokenWithClaims ();
124161 System .out .println ("Done!" );
125162 }
126163
0 commit comments