X Tutup
Skip to content

feat(auth): implement generateVerifyAndChangeEmailLink API#1195

Open
siddharthmohan619-eng wants to merge 4 commits intofirebase:mainfrom
siddharthmohan619-eng:feature-change-email-link-1193
Open

feat(auth): implement generateVerifyAndChangeEmailLink API#1195
siddharthmohan619-eng wants to merge 4 commits intofirebase:mainfrom
siddharthmohan619-eng:feature-change-email-link-1193

Conversation

@siddharthmohan619-eng
Copy link

This PR adds support for generating out-of-band email action links for the Verify and Change Email flow. This allows developers to programmatically generate links for users to verify a new email address before it is updated on their profile.

Key Changes:

AbstractFirebaseAuth.java: Added generateVerifyAndChangeEmailLink() and the asynchronous generateVerifyAndChangeEmailLinkAsync() implementation using CallableOperation.

FirebaseAuth.java: Exposed the new methods to the public API.

FirebaseUserManager.java: Updated the backend manager to handle the VERIFY_AND_CHANGE_EMAIL request type.

FirebaseUserManagerTest.java: Added comprehensive unit tests to verify:

Correct request payload (email, newEmail, and requestType).

Proper handling of ActionCodeSettings.

Correct parsing of the returned OOB link.

Testing:
Verified via mvn test -Dtest=FirebaseUserManagerTest. All 147 tests passed, including the new test case.

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new feature to programmatically generate out-of-band email action links specifically for the "Verify and Change Email" flow. This allows developers to create links that users can use to verify a new email address before it is officially updated on their profile, enhancing the flexibility of email management within the authentication system.

Highlights

  • AbstractFirebaseAuth.java: Introduced new synchronous and asynchronous methods, generateVerifyAndChangeEmailLink, for creating out-of-band email action links.
  • FirebaseUserManager.java: Updated the backend manager to support the VERIFY_AND_CHANGE_EMAIL request type and handle the newEmail parameter.
  • FirebaseUserManagerTest.java: Added comprehensive unit tests to ensure the correct functionality and payload for the new email action link generation.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/main/java/com/google/firebase/auth/AbstractFirebaseAuth.java
    • Added generateVerifyAndChangeEmailLink method overloads.
    • Added generateVerifyAndChangeEmailLinkAsync method overloads.
  • src/main/java/com/google/firebase/auth/FirebaseUserManager.java
    • Overloaded getEmailActionLink to accept a newEmail parameter.
    • Added VERIFY_AND_CHANGE_EMAIL to the EmailLinkType enum.
  • src/test/java/com/google/firebase/auth/FirebaseUserManagerTest.java
    • Added import for CallableOperation.
    • Implemented testGenerateVerifyAndChangeEmailLink to validate the new functionality.
Activity
  • All existing and new unit tests were run via mvn test -Dtest=FirebaseUserManagerTest, with all 147 tests passing successfully.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new API for generating a 'verify and change email' link. The implementation is sound, but I've provided feedback to improve consistency with existing code patterns in AbstractFirebaseAuth.java by adding Javadoc, argument validation, and refactoring to a common helper method pattern. I've also suggested improvements to the test coverage to ensure the new API is fully tested, and pointed out a minor cleanup in the test file.

Comment on lines +1072 to +1097
public String generateVerifyAndChangeEmailLink(@NonNull String email, @NonNull String newEmail)
throws FirebaseAuthException {
return generateVerifyAndChangeEmailLink(email, newEmail, null);
}

public String generateVerifyAndChangeEmailLink(
@NonNull String email, @NonNull String newEmail, @Nullable ActionCodeSettings settings)
throws FirebaseAuthException {
return getUserManager().getEmailActionLink(
EmailLinkType.VERIFY_AND_CHANGE_EMAIL, email, newEmail, settings);
}

public ApiFuture<String> generateVerifyAndChangeEmailLinkAsync(
@NonNull String email, @NonNull String newEmail) {
return generateVerifyAndChangeEmailLinkAsync(email, newEmail, null);
}

public ApiFuture<String> generateVerifyAndChangeEmailLinkAsync(
@NonNull String email, @NonNull String newEmail, @Nullable ActionCodeSettings settings) {
return new CallableOperation<String, FirebaseAuthException>() {
@Override
protected String execute() throws FirebaseAuthException {
return generateVerifyAndChangeEmailLink(email, newEmail, settings);
}
}.callAsync(firebaseApp);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The new generateVerifyAndChangeEmailLink methods are a great addition. To align them with the existing patterns in this class and improve maintainability, I suggest adding Javadoc for the new public methods and refactoring to use a private ...Op helper method. This helper would centralize the logic and argument validation (e.g., using checkArgument for email and newEmail), which is a common pattern in this file.

  /**
   * Generates the out-of-band email action link for the verify and change email flow.
   *
   * @param email The user's current email.
   * @param newEmail The user's new email.
   * @return A verify and change email link.
   * @throws IllegalArgumentException If email or newEmail are null or empty.
   * @throws FirebaseAuthException If an error occurs while generating the link.
   */
  public String generateVerifyAndChangeEmailLink(@NonNull String email, @NonNull String newEmail)
      throws FirebaseAuthException {
    return generateVerifyAndChangeEmailLink(email, newEmail, null);
  }

  /**
   * Generates the out-of-band email action link for the verify and change email flow.
   *
   * @param email The user's current email.
   * @param newEmail The user's new email.
   * @param settings The action code settings.
   * @return A verify and change email link.
   * @throws IllegalArgumentException If email or newEmail are null or empty.
   * @throws FirebaseAuthException If an error occurs while generating the link.
   */
  public String generateVerifyAndChangeEmailLink(
      @NonNull String email, @NonNull String newEmail, @Nullable ActionCodeSettings settings)
      throws FirebaseAuthException {
    return generateVerifyAndChangeEmailLinkOp(email, newEmail, settings).call();
  }

  /**
   * Asynchronously generates the out-of-band email action link for the verify and change email
   * flow.
   *
   * @param email The user's current email.
   * @param newEmail The user's new email.
   * @return An {@code ApiFuture} which will complete with the generated link.
   * @throws IllegalArgumentException If email or newEmail are null or empty.
   */
  public ApiFuture<String> generateVerifyAndChangeEmailLinkAsync(
      @NonNull String email, @NonNull String newEmail) {
    return generateVerifyAndChangeEmailLinkAsync(email, newEmail, null);
  }

  /**
   * Asynchronously generates the out-of-band email action link for the verify and change email
   * flow.
   *
   * @param email The user's current email.
   * @param newEmail The user's new email.
   * @param settings The action code settings.
   * @return An {@code ApiFuture} which will complete with the generated link.
   * @throws IllegalArgumentException If email or newEmail are null or empty.
   */
  public ApiFuture<String> generateVerifyAndChangeEmailLinkAsync(
      @NonNull String email, @NonNull String newEmail, @Nullable ActionCodeSettings settings) {
    return generateVerifyAndChangeEmailLinkOp(email, newEmail, settings).callAsync(firebaseApp);
  }

  private CallableOperation<String, FirebaseAuthException> generateVerifyAndChangeEmailLinkOp(
      final String email, final String newEmail, final ActionCodeSettings settings) {
    checkArgument(!Strings.isNullOrEmpty(email), "email must not be null or empty");
    checkArgument(!Strings.isNullOrEmpty(newEmail), "newEmail must not be null or empty");
    final FirebaseUserManager userManager = getUserManager();
    return new CallableOperation<String, FirebaseAuthException>() {
      @Override
      protected String execute() throws FirebaseAuthException {
        return userManager.getEmailActionLink(
            EmailLinkType.VERIFY_AND_CHANGE_EMAIL, email, newEmail, settings);
      }
    };
  }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gemini-code-assist Thanks for the detailed review! I've updated the PR to fully align with the existing patterns in this class:

Refactored Logic: Centralized the logic into a private CallableOperation helper method (generateVerifyAndChangeEmailLinkOp).

Validation: Added checkArgument to validate that email and newEmail are not null or empty.

Documentation: Added the requested Javadoc for the new public and async methods.

The build is fully passing locally. Ready for the next stage of review lets goo

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good and align with the suggested improvements. Thank you for addressing the feedback.

import com.google.firebase.auth.multitenancy.TenantAwareFirebaseAuth;
import com.google.firebase.auth.multitenancy.TenantManager;
import com.google.firebase.internal.ApiClientUtils;
import com.google.firebase.internal.CallableOperation;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This import appears to be unused and can be removed to keep the code clean.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I saw this and I fixed it bro

Comment on lines +1515 to +1530
@Test
public void testGenerateVerifyAndChangeEmailLink() throws Exception {
TestResponseInterceptor interceptor = initializeAppForUserManagement(
TestUtils.loadResource("generateEmailLink.json"));
String link = FirebaseAuth.getInstance()
.generateVerifyAndChangeEmailLinkAsync("test@example.com", "new@example.com").get();
assertEquals("https://mock-oob-link.for.auth.tests", link);
checkRequestHeaders(interceptor);

GenericJson parsed = parseRequestContent(interceptor);
assertEquals(4, parsed.size());
assertEquals("test@example.com", parsed.get("email"));
assertEquals("new@example.com", parsed.get("newEmail"));
assertEquals("VERIFY_AND_CHANGE_EMAIL", parsed.get("requestType"));
assertTrue((Boolean) parsed.get("returnOobLink"));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This is a good test for the success case. To make the tests more comprehensive, please consider adding another test case that calls generateVerifyAndChangeEmailLinkAsync with an ActionCodeSettings object. This will ensure that the settings are correctly passed in the request payload, similar to how it's tested for other email action links (e.g., testGeneratePasswordResetLinkWithSettings).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gemini-code-assist Thank you for the review! I have addressed all the feedback in the latest commit:

Refactored Logic: Updated AbstractFirebaseAuth.java to use the internal getEmailActionLink and getEmailActionLinkAsync helper methods.

Validation & Javadoc: Added checkArgument to ensure emails are not null/empty, and included the requested Javadoc.

Imports: Removed the unused CallableOperation import in the test file.

Test Coverage: Added testGenerateVerifyAndChangeEmailLinkWithSettings to verify that ActionCodeSettings are correctly parsed and passed in the payload.

Public API: Exposed the new methods in FirebaseAuth.java so they are accessible.

Linting: Fixed all Checkstyle line-length and formatting requirements.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's great to see that you've added testGenerateVerifyAndChangeEmailLinkWithSettings to ensure ActionCodeSettings are correctly handled. This significantly improves the test coverage for the new API. Well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

X Tutup