Skip to content

Commit

Permalink
feat(firebase_auth): expose smsCode (android only) (firebase#3308)
Browse files Browse the repository at this point in the history
Co-authored-by: Mike Diarmid <[email protected]>
  • Loading branch information
Ehesp and Salakar authored Aug 26, 2020
1 parent 14cb755 commit 63d67be
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,10 @@ public void onVerificationCompleted(
authCredentials.put(phoneAuthCredentialHashCode, phoneAuthCredential);
event.put(Constants.TOKEN, phoneAuthCredentialHashCode);

if (phoneAuthCredential.getSmsCode() != null) {
event.put(Constants.SMS_CODE, phoneAuthCredential.getSmsCode());
}

channel.invokeMethod(
"Auth#phoneVerificationCompleted",
event,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,16 +699,19 @@ void runInstanceTests() {
unawaited(auth.verifyPhoneNumber(
phoneNumber: 'foo',
verificationCompleted: (PhoneAuthCredential credential) {
fail("Should not have been called");
return completer
.completeError(Exception("Should not have been called"));
},
verificationFailed: (FirebaseAuthException e) {
completer.complete(e);
},
codeSent: (String verificationId, int resetToken) {
fail("Should not have been called");
return completer
.completeError(Exception("Should not have been called"));
},
codeAutoRetrievalTimeout: (String foo) {
fail("Should not have been called");
return completer
.completeError(Exception("Should not have been called"));
}));

return completer.future;
Expand All @@ -722,6 +725,7 @@ void runInstanceTests() {

test('should auto verify phone number', () async {
String testPhoneNumber = '+447444555666';
String testSmsCode = '123456';
await auth.signInAnonymously();

Future<PhoneAuthCredential> getCredential() async {
Expand All @@ -730,18 +734,26 @@ void runInstanceTests() {
unawaited(auth.verifyPhoneNumber(
phoneNumber: testPhoneNumber,
// ignore: invalid_use_of_visible_for_testing_member
autoRetrievedSmsCodeForTesting: '123456',
autoRetrievedSmsCodeForTesting: testSmsCode,
verificationCompleted: (PhoneAuthCredential credential) {
if (credential.smsCode != testSmsCode) {
return completer
.completeError(Exception("SMS code did not match"));
}

completer.complete(credential);
},
verificationFailed: (FirebaseException e) {
fail("Should not have been called");
return completer
.completeError(Exception("Should not have been called"));
},
codeSent: (String verificationId, int resetToken) {
fail("Should not have been called");
return completer
.completeError(Exception("Should not have been called"));
},
codeAutoRetrievalTimeout: (String foo) {
fail("Should not have been called");
return completer
.completeError(Exception("Should not have been called"));
}));

return completer.future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ class MethodChannelFirebaseAuth extends FirebaseAuthPlatform {
Map<dynamic, dynamic> arguments) async {
final int handle = arguments['handle'];
final int token = arguments['token'];
final String smsCode = arguments['smsCode'];

PhoneAuthCredential phoneAuthCredential =
PhoneAuthProvider.credentialFromToken(token);
PhoneAuthProvider.credentialFromToken(token, smsCode: smsCode);
PhoneAuthCallbacks callbacks = _phoneAuthCallbacks[handle];
callbacks.verificationCompleted(phoneAuthCredential);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class PhoneAuthProvider extends AuthProvider {

/// Create a [PhoneAuthCredential] from an internal token, where the ID
/// relates to a natively stored credential.
static AuthCredential credentialFromToken(int token) {
static AuthCredential credentialFromToken(int token, {String smsCode}) {
assert(token != null);
return PhoneAuthCredential._credentialFromToken(token);
return PhoneAuthCredential._credentialFromToken(token, smsCode: smsCode);
}

@Deprecated('Deprecated in favor of `PhoneAuthProvider.credential()`')
Expand Down Expand Up @@ -69,8 +69,9 @@ class PhoneAuthCredential extends AuthCredential {
verificationId: verificationId, smsCode: smsCode);
}

factory PhoneAuthCredential._credentialFromToken(int token) {
return PhoneAuthCredential._(token: token);
factory PhoneAuthCredential._credentialFromToken(int token,
{String smsCode}) {
return PhoneAuthCredential._(token: token, smsCode: smsCode);
}

/// The phone auth verification ID.
Expand Down

0 comments on commit 63d67be

Please sign in to comment.