Skip to content

fix(auth, android): handle native exception in signInWithEmailLink #8502

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
Apr 30, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -459,18 +459,23 @@ private void signInWithEmailLink(
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);

firebaseAuth
.signInWithEmailLink(email, emailLink)
.addOnSuccessListener(
authResult -> {
Log.d(TAG, "signInWithEmailLink:onComplete:success");
promiseWithAuthResult(authResult, promise);
})
.addOnFailureListener(
exception -> {
Log.e(TAG, "signInWithEmailLink:onComplete:failure", exception);
promiseRejectAuthException(promise, exception);
});
try {
firebaseAuth
.signInWithEmailLink(email, emailLink)
.addOnSuccessListener(
authResult -> {
Log.d(TAG, "signInWithEmailLink:onComplete:success");
promiseWithAuthResult(authResult, promise);
})
.addOnFailureListener(
exception -> {
Log.e(TAG, "signInWithEmailLink:onComplete:failure", exception);
promiseRejectAuthException(promise, exception);
});
} catch (Exception exception) {
Log.e(TAG, "signInWithEmailLink:onComplete:totalfailure", exception);
promiseRejectAuthException(promise, exception);
}
}

@ReactMethod
Expand Down
52 changes: 49 additions & 3 deletions packages/auth/e2e/emailLink.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,55 @@ describe('auth() -> emailLink Provider', function () {
});
});

// FOR MANUAL TESTING ONLY
xdescribe('signInWithEmailLink', function () {
it('should signIn', async function () {
describe('signInWithEmailLink', function () {
it('sign in via email does not crash with missing apiKey', async function () {
const { getAuth, sendSignInLinkToEmail, signInWithEmailLink } = authModular;

const auth = getAuth();
const random = Utils.randString(12, '#aa');
const email = `${random}@${random}.com`;
const continueUrl = `http://${Platform.android ? '10.0.2.2' : '127.0.0.1'}:8081/authLinkFoo?bar=${random}`;
const actionCodeSettings = {
url: continueUrl,
handleCodeInApp: true,
iOS: {
bundleId: 'com.testing',
},
android: {
packageName: 'com.testing',
installApp: true,
minimumVersion: '12',
},
};
await sendSignInLinkToEmail(auth, email, actionCodeSettings);
const oobInfo = await getLastOob(email);
oobInfo.oobLink.should.containEql(encodeURIComponent(continueUrl));

// Specifically remove the apiKey param. Android requires it and needs
// specific error handling or it crashes, See #8360
let linkNoApiKey = oobInfo.oobLink.replace('&apiKey=fake-api-key', '');
try {
const signInResponse = await signInWithEmailLink(auth, email, linkNoApiKey);
if (Platform.OS !== 'ios') {
throw new Error('Should have rejected on Android and Other');
} else {
signInResponse.user.email.should.equal(email);
auth.currentUser.email.should.equal(email);
}
} catch (e) {
if (Platform.OS === 'android') {
e.message.should.containEql('Given link is not a valid email link');
} else if (Platform.OS === 'macos') {
e.message.should.containEql('auth/argument-error');
} else {
// ios should have been fine without apiKey
throw e;
}
}
});

// FOR MANUAL TESTING ONLY
xit('should signIn', async function () {
const auth = getAuth();
const email = 'MANUAL TEST EMAIL HERE';
const emailLink = 'MANUAL TEST CODE HERE';
Expand Down
7 changes: 4 additions & 3 deletions packages/auth/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2008,15 +2008,16 @@ export namespace FirebaseAuthTypes {
sendSignInLinkToEmail(email: string, actionCodeSettings?: ActionCodeSettings): Promise<void>;

/**
* Returns whether the user signed in with a given email link.
* Checks if an incoming link is a sign-in with email link suitable for signInWithEmailLink.
* Note that android and other platforms require `apiKey` link parameter for signInWithEmailLink
*
* #### Example
*
* ```js
* const signedInWithLink = await firebase.auth().isSignInWithEmailLink(link);
* const valid = await firebase.auth().isSignInWithEmailLink(link);
* ```
*
* @param emailLink The email link to check whether the user signed in with it.
* @param emailLink The email link to verify prior to using signInWithEmailLink
*/
isSignInWithEmailLink(emailLink: string): Promise<boolean>;

Expand Down
3 changes: 2 additions & 1 deletion packages/auth/lib/modular/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ export interface PopupRedirectResolver {}
export function initializeRecaptchaConfig(auth: Auth): Promise<void>;

/**
* Checks if an incoming link is a sign-in with email link suitable for signInWithEmailLink().
* Checks if an incoming link is a sign-in with email link suitable for signInWithEmailLink.
* Note that android and other platforms require `apiKey` link parameter for signInWithEmailLink
*
* @param auth - The Auth instance.
* @param emailLink - The email link to check.
Expand Down
Loading