react-native-firebase icon indicating copy to clipboard operation
react-native-firebase copied to clipboard

Firebase Phone Number Auth integration

Open jasan-s opened this issue 9 years ago • 137 comments

Is the new Firebase Phone number Auth on the roadmap?

jasan-s avatar May 18 '17 00:05 jasan-s

Yeah it will be. Will look at seeing what's possible to do

Ehesp avatar May 18 '17 05:05 Ehesp

@jasan-s @Ehesp - might make sense to wait until android support is out for this at the end of may: https://firebase.google.com/docs/auth/android/phone-auth

Salakar avatar May 18 '17 12:05 Salakar

They will support this? Awesome 😄

Since I currently use Facebook Account Kit to verify phone numbers, I actually consider waiting with my first release until I can use it with firebase.

DavidKuennen avatar May 18 '17 17:05 DavidKuennen

I believe its already available as they have open sourced their SDKs

jeveloper avatar May 18 '17 19:05 jeveloper

@jeveloper it's only available in ios at the moment

Salakar avatar May 19 '17 09:05 Salakar

ohh thats odd, i thought they were just reusing fabrics digits which works on anything

thanks man!

jeveloper avatar May 19 '17 13:05 jeveloper

Maybe now is too early June :)

Phone number sign-in is coming soon to Firebase on Android. Check this page in early June 2017 for details.

Froelund avatar Jun 01 '17 12:06 Froelund

Yesterday it said late May! :)

chrisbianca avatar Jun 01 '17 12:06 chrisbianca

+1 I am also really interested in this feature!

perrosnk avatar Jun 01 '17 19:06 perrosnk

+1 We're anxiously awaiting this feature after Google finishes implementation for Android

willbattel avatar Jun 06 '17 22:06 willbattel

https://firebase.google.com/support/release-notes/android#20170607

Now available on Android SDK!

willbattel avatar Jun 08 '17 01:06 willbattel

Is anyone assigned or already working a PR to this issue? If not, I could try to do it myself.

Sparragus avatar Jun 08 '17 15:06 Sparragus

@Sparragus no one specifically. I'll take a look at Android on Monday if theres no PR. IOS looks a bit too complicated for my object c skills

Ehesp avatar Jun 08 '17 17:06 Ehesp

I've been looking into this today. I'll have to speak internally about how to actually implement this (more best practice). The aim of this lib is to try and replicate the web SDK, however I don't think it'll be possible here.

  1. On the Web docs there's no need for steps 1-3.
  2. signInWithPhoneNumber takes a phoneNumber & appVerifier (where appVerifier is redundant on the device).
  3. The native SDKs (at least on Android) takes a verifyPhoneNumber. It can also do things like auto detection. The response of which, can be directly passed into signInWithCredential.

@chrisbianca @Salakar How do you suggest dealing with the following workflow:

After calling verifyPhoneNumber, we return a promise which would resolve on onVerificationCompleted (Instantly verified or auto detected). The issue is though, there's an optional onCodeSent which would prompt the user to enter the code manually (as it's not been detected automagically). How would that fit into the API?

firebase.auth().verifyPhoneNumber('+441234567')
  .then((credential) => {}) // onVerificationCompleted
  .catch(() => {});

That may potentially never get called, as it's hit the onCodeSent block. We could add a method like verifyPhoneCode or something, but then how would the user know whether to use this or not.

Ehesp avatar Jun 15 '17 12:06 Ehesp

Hello, I successfully got it working for iOS. Unfortunately I don't have the time for a PR but I will describe how I went on. First, the NativeModule (my module is called WHOFirebaseAuth here)

//
//  WHOFirebaseAuth.m
//  WhoCaresAppNew
//
//  Created by Enes Kaya on 29.05.17.
//  Copyright © 2017 Facebook. All rights reserved.
//

#import "WHOFirebaseAuth.h"
#import <FirebaseAuth/FirebaseAuth.h>

@implementation WHOFirebaseAuth

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(verifyPhoneNumber:(NSString*) userInput
                  callback:(RCTResponseSenderBlock) callback)
{
    [[FIRPhoneAuthProvider provider]
     verifyPhoneNumber: userInput
     completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {
       
       NSLog(@"WHOFirebaseAuth: %@", userInput);
       
      if (error) {
        callback(@[error.localizedDescription, @"ERROR"]);
        NSLog(@"WHOFirebaseAuth: %@", error.localizedDescription);
      } else {
        // Sets the verficiationID in UserDefaults for later usage
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:verificationID forKey:@"authVerificationID"];
        
        callback(@[[NSNull null], verificationID]);
      }
      
    }];
}

RCT_EXPORT_METHOD(getVerificationId:(RCTResponseSenderBlock)callback)
{
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  NSString *verificationID = [defaults stringForKey:@"authVerificationID"];
  callback(@[[NSNull null], verificationID]);
}

@end

You call it like this in your RN project:

const WHOFirebaseAuth = NativeModules.WHOFirebaseAuth
...
static sendVerificationCodeTo(phoneNumber) {
   return new Promise((resolve, reject) => {
	WHOFirebaseAuth.verifyPhoneNumber(
		phoneNumber,
		(error, verificationID) => {
			if (!error) {
				resolve({
					error: null,
					verificationID: verificationID
				})
			} else {
				reject({
					error,
					verificationID: null
				})
			}
		}
	)
   })
}
...

After that, if it was successful, the user get's the code sent via SMS. Then you can log in like so:

	/**
   * Signs the user in with PhoneAuth credential.
   *
   * @param  {[type]} verificationID The verificationID returned by this.sendVerificationCodeTo
   * @param  {[type]} code           The code which was sent via SMS
   * @return {[type]}                firebase.Promise containing non-null firebase.User
   */
	static signInWithPhoneAuth(verificationID, code) {
		var credential = firebase.auth.PhoneAuthProvider.credential(
			verificationID,
			code
		)

		return firebase.auth().signInWithCredential(credential)
	}

So, basically it's a mix between the native SDK and the JS SDK. Remember to install Firebase/Auth via CocoaPods in version >4.0.0.

Hope that helps, for me it's working perfectly.

eneskaya avatar Jun 20 '17 09:06 eneskaya

@eneskaya will it work on android with same syntax and all ?

yash2code avatar Jun 20 '17 09:06 yash2code

@yash2code The JS part of the code yes, but obviously not the objective-c part. I will, later today implement the android side.

eneskaya avatar Jun 20 '17 09:06 eneskaya

@eneskaya Any thought on my comment above regarding onCodeSent would be handy on Android.

Ehesp avatar Jun 20 '17 09:06 Ehesp

@eneskaya Ty sir, I m surely waiting for that !! :)

yash2code avatar Jun 20 '17 09:06 yash2code

@Ehesp not yet, but will look into that. I'll keep you posted

eneskaya avatar Jun 20 '17 09:06 eneskaya