Skip to content

Documentation Updates for 5.4.0 Release #14

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 2 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ repositories {

dependencies {
// Firebase Java SDK
compile 'com.google.firebase:firebase-admin:5.1.0'
compile 'com.google.firebase:firebase-admin:5.3.1'
}
154 changes: 72 additions & 82 deletions auth/src/main/java/com/google/firebase/quickstart/AuthSnippets.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.google.firebase.quickstart;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.auth.FirebaseToken;
import com.google.firebase.auth.UserRecord;
import com.google.firebase.auth.UserRecord.CreateRequest;
import com.google.firebase.auth.UserRecord.UpdateRequest;
import com.google.firebase.tasks.Task;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
* Auth snippets for documentation.
Expand All @@ -19,52 +22,32 @@
*/
public class AuthSnippets {

public static Task<UserRecord> getUserById(String uid) {
public static void getUserById(String uid) throws InterruptedException, ExecutionException {
// [START get_user_by_id]
Task<UserRecord> task = FirebaseAuth.getInstance().getUser(uid)
.addOnSuccessListener(userRecord -> {
// See the UserRecord reference doc for the contents of userRecord.
System.out.println("Successfully fetched user data: " + userRecord.getUid());
})
.addOnFailureListener(e -> {
System.err.println("Error fetching user data: " + e.getMessage());
});
UserRecord userRecord = FirebaseAuth.getInstance().getUserAsync(uid).get();
// See the UserRecord reference doc for the contents of userRecord.
System.out.println("Successfully fetched user data: " + userRecord.getUid());
// [END get_user_by_id]

return task;
}

public static Task<UserRecord> getUserByEmail(String email) {
public static void getUserByEmail(String email) throws InterruptedException, ExecutionException {
// [START get_user_by_email]
Task<UserRecord> task = FirebaseAuth.getInstance().getUserByEmail(email)
.addOnSuccessListener(userRecord -> {
// See the UserRecord reference doc for the contents of userRecord.
System.out.println("Successfully fetched user data: " + userRecord.getEmail());
})
.addOnFailureListener(e -> {
System.err.println("Error fetching user data: " + e.getMessage());
});
UserRecord userRecord = FirebaseAuth.getInstance().getUserByEmailAsync(email).get();
// See the UserRecord reference doc for the contents of userRecord.
System.out.println("Successfully fetched user data: " + userRecord.getEmail());
// [END get_user_by_email]

return task;
}

public static Task<UserRecord> getUserByPhoneNumber(String phoneNumber) {
public static void getUserByPhoneNumber(
String phoneNumber) throws InterruptedException, ExecutionException {
// [START get_user_by_phone]
Task<UserRecord> task = FirebaseAuth.getInstance().getUserByPhoneNumber(phoneNumber)
.addOnSuccessListener(userRecord -> {
// See the UserRecord reference doc for the contents of userRecord.
System.out.println("Successfully fetched user data: " + userRecord.getPhoneNumber());
})
.addOnFailureListener(e -> {
System.err.println("Error fetching user data: " + e.getMessage());
});
UserRecord userRecord = FirebaseAuth.getInstance().getUserByPhoneNumberAsync(phoneNumber).get();
// See the UserRecord reference doc for the contents of userRecord.
System.out.println("Successfully fetched user data: " + userRecord.getPhoneNumber());
// [END get_user_by_phone]

return task;
}

public static Task<UserRecord> createUser() {
public static void createUser() throws InterruptedException, ExecutionException {
// [START create_user]
CreateRequest request = new CreateRequest()
.setEmail("[email protected]")
Expand All @@ -75,40 +58,24 @@ public static Task<UserRecord> createUser() {
.setPhotoUrl("http://www.example.com/12345678/photo.png")
.setDisabled(false);

Task<UserRecord> task = FirebaseAuth.getInstance().createUser(request)
.addOnSuccessListener(userRecord -> {
// See the UserRecord reference doc for the contents of userRecord.
System.out.println("Successfully created new user: " + userRecord.getUid());
})
.addOnFailureListener(e -> {
System.err.println("Error creating new user: " + e.getMessage());
});
UserRecord userRecord = FirebaseAuth.getInstance().createUserAsync(request).get();
System.out.println("Successfully created new user: " + userRecord.getUid());
// [END create_user]

return task;
}

public static Task<UserRecord> createUserWithUid() {
public static void createUserWithUid() throws InterruptedException, ExecutionException {
// [START create_user_with_uid]
CreateRequest request = new CreateRequest()
.setUid("some-uid")
.setEmail("[email protected]")
.setPhoneNumber("+11234567890");

Task<UserRecord> task = FirebaseAuth.getInstance().createUser(request)
.addOnSuccessListener(userRecord -> {
// See the UserRecord reference doc for the contents of userRecord.
System.out.println("Successfully created new user: " + userRecord.getUid());
})
.addOnFailureListener(e -> {
System.err.println("Error creating new user: " + e.getMessage());
});
UserRecord userRecord = FirebaseAuth.getInstance().createUserAsync(request).get();
System.out.println("Successfully created new user: " + userRecord.getUid());
// [END create_user_with_uid]

return task;
}

public static Task<UserRecord> updateUser(String uid) {
public static void updateUser(String uid) throws InterruptedException, ExecutionException {
// [START update_user]
UpdateRequest request = new UpdateRequest(uid)
.setEmail("[email protected]")
Expand All @@ -119,38 +86,59 @@ public static Task<UserRecord> updateUser(String uid) {
.setPhotoUrl("http://www.example.com/12345678/photo.png")
.setDisabled(true);

Task<UserRecord> task = FirebaseAuth.getInstance().updateUser(request)
.addOnSuccessListener(userRecord -> {
// See the UserRecord reference doc for the contents of userRecord.
System.out.println("Successfully updated user: " + userRecord.getUid());
})
.addOnFailureListener(e -> {
System.err.println("Error updating user: " + e.getMessage());
});
UserRecord userRecord = FirebaseAuth.getInstance().updateUserAsync(request).get();
System.out.println("Successfully updated user: " + userRecord.getUid());
// [END update_user]

return task;
}

public static Task<Void> deleteUser(String uid) {
public static void deleteUser(String uid) throws InterruptedException, ExecutionException {
// [START delete_user]
Task<Void> task = FirebaseAuth.getInstance().deleteUser(uid)
.addOnSuccessListener(aVoid -> System.out.println("Successfully deleted user."))
.addOnFailureListener(e -> System.err.println("Error updating user: " + e.getMessage()));
FirebaseAuth.getInstance().deleteUserAsync(uid).get();
System.out.println("Successfully deleted user.");
// [END delete_user]
}

public static void createCustomToken() throws InterruptedException, ExecutionException {
// [START custom_token]
String uid = "some-uid";

String customToken = FirebaseAuth.getInstance().createCustomTokenAsync(uid).get();
// Send token back to client
// [END custom_token]
System.out.println("Created custom token: " + customToken);
}

public static void createCustomTokenWithClaims() throws InterruptedException, ExecutionException {
// [START custom_token_with_claims]
String uid = "some-uid";
Map<String, Object> additionalClaims = new HashMap<String, Object>();
additionalClaims.put("premiumAccount", true);

String customToken = FirebaseAuth.getInstance()
.createCustomTokenAsync(uid, additionalClaims).get();
// Send token back to client
// [END custom_token_with_claims]
System.out.println("Created custom token: " + customToken);
}

return task;
public static void verifyIdToken(String idToken) throws InterruptedException, ExecutionException {
// [START verify_id_token]
// idToken comes from the client app (shown above)
FirebaseToken decodedToken = FirebaseAuth.getInstance().verifyIdTokenAsync(idToken).get();
String uid = decodedToken.getUid();
// [END verify_id_token]
System.out.println("Decoded ID token from user: " + uid);
}

public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println("Hello, AuthSnippets!");

// Initialize Firebase
try {
// [START initialize]
FileInputStream serviceAccount = new FileInputStream("service-account.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();
FirebaseApp.initializeApp(options);
// [END initialize]
Expand All @@ -162,13 +150,15 @@ public static void main(String[] args) {
}

// Smoke test
createUserWithUid()
.continueWithTask(task -> getUserById("some-uid"))
.continueWithTask(task -> getUserByEmail("[email protected]"))
.continueWithTask(task -> getUserByPhoneNumber("+11234567890"))
.continueWithTask(task -> updateUser("some-uid"))
.continueWithTask(task -> deleteUser("some-uid"))
.addOnCompleteListener(task -> System.out.println("Done! Success: " + task.isSuccessful()));
createUserWithUid();
getUserById("some-uid");
getUserByEmail("[email protected]");
getUserByPhoneNumber("+11234567890");
updateUser("some-uid");
deleteUser("some-uid");
createCustomToken();
createCustomTokenWithClaims();
System.out.println("Done!");
}

}
2 changes: 1 addition & 1 deletion database/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repositories {

dependencies {
// Firebase Java SDK
compile 'com.google.firebase:firebase-admin:5.1.0'
compile 'com.google.firebase:firebase-admin:5.3.1'

// Sundial Job Scheduler
compile 'org.knowm:sundial:2.1.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/
package com.google.firebase.quickstart;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.database.*;
import com.google.firebase.quickstart.email.MyEmailer;
import com.google.firebase.quickstart.model.Post;
import com.google.firebase.quickstart.model.User;
import java.io.IOException;
import org.knowm.sundial.SundialJobScheduler;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
* Firebase Database quickstart sample for the Java Admin SDK.
Expand Down Expand Up @@ -184,12 +184,12 @@ public static void main(String[] args) {
// [START initialize]
FileInputStream serviceAccount = new FileInputStream("service-account.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl(DATABASE_URL)
.build();
FirebaseApp.initializeApp(options);
// [END initialize]
} catch (FileNotFoundException e) {
} catch (IOException e) {
System.out.println("ERROR: invalid service account credentials. See README.");
System.out.println(e.getMessage());

Expand Down