-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Firebase Phone Number Auth integration #119
Comments
Yeah it will be. Will look at seeing what's possible to do |
@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 |
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. |
I believe its already available as they have open sourced their SDKs |
@jeveloper it's only available in ios at the moment |
ohh thats odd, i thought they were just reusing fabrics digits which works on anything thanks man! |
Maybe now is too early June :)
|
Yesterday it said late May! :) |
+1 I am also really interested in this feature! |
+1 We're anxiously awaiting this feature after Google finishes implementation for Android |
https://firebase.google.com/support/release-notes/android#20170607 Now available on Android SDK! |
Is anyone assigned or already working a PR to this issue? If not, I could try to do it myself. |
@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 |
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.
@chrisbianca @Salakar How do you suggest dealing with the following workflow: After calling firebase.auth().verifyPhoneNumber('+441234567')
.then((credential) => {}) // onVerificationCompleted
.catch(() => {}); That may potentially never get called, as it's hit the |
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.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 Hope that helps, for me it's working perfectly. |
@eneskaya will it work on android with same syntax and all ? |
@yash2code The JS part of the code yes, but obviously not the objective-c part. I will, later today implement the android side. |
@eneskaya Any thought on my comment above regarding onCodeSent would be handy on Android. |
@eneskaya Ty sir, I m surely waiting for that !! :) |
@Ehesp not yet, but will look into that. I'll keep you posted |
I was just about to post a version of myself.
|
So, it's a tad more difficult on Android than I thought 😅 Does anyone else have an Idea? |
Need this for a project, so I can pick up the work if someone's not already on it - @eneskaya, is your work all on this comment thread or is there a branch I should work from? |
For what it's worth, we recently got Firebase phone authentication working in a FirebaseUI provides drop-in phone authentication interfaces for iOS and Android and is pretty easy to wrap to spawn it with a native-module call. It handles sending and confirming the SMS token. We then retrieve the authentication token to generate a custom token and eventually sign in to Obviously FirebaseUI is not an option if your app needs a custom phone auth UI, but it's a good stop-gap until there's more robust support for the underlying APIs in |
@javamonn that sounds awesome. Can you provide some more info or maybe sample code on your integration? |
Hello! Could someone help me please i have an error: "Remote notification and background fetching need to be set up for the app. If app delegate swizzling is disabled, the APNs device token received by UIApplicationDelegate needs to be forwarded to FIRAuth's APNSToken property.". Do i need to set up firebase cloud messaging for auth with phone for firebase (do i need to make swizzling disabled or not?)? It is pointed that cloud messaging is optional in installation docs. Thanks |
Yes, you need to activate Firebase Cloud Messaging!
… Am 14.09.2017 um 10:24 schrieb Kirill ***@***.***>:
Hello! Could someone help me please i have an error: "Remote notification and background fetching need to be set up for the app. If app delegate swizzling is disabled, the APNs device token received by UIApplicationDelegate needs to be forwarded to FIRAuth's APNSToken property.". Do i need to set up firebase cloud messaging for auth with phone for firebase? It is pointed that cloud messaging is optional in installation docs. Thanks
also could you tell me please what is "auto-verify" in this context?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@eneskaya Thanks for fast answer, also could you tell me please is it possible to test it and debug this auth without generating APNs Auth Key (i do not have payed dev account now. So the question is can i test this method of auth without payed account? (how?) |
You need to have FCM enabled and correctly set up according to Firebase documentation (https://firebase.google.com/docs/cloud-messaging/ios/client). It's a security measure, to prevent scam. So, unfortunately you need a paid developer account for iOS developers program to even be able to test it.
… Am 14.09.2017 um 10:51 schrieb Kirill ***@***.***>:
@eneskaya Thanks for fast answer, also could you tell me please is it possible to test it and debug this auth without generating APNs Auth Key (i do not have payed dev account now. So the question is can i test this method of auth without payed account? (how?)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@eneskaya Thank you very mush! You helped me a lot. I kindly suggest firebase developers to add some information about phone auth in the doc. I know that it is only alpha, but anyway, just some words will be good. |
Need to be patient with that ;) but glad I could help.
… Am 14.09.2017 um 11:54 schrieb Kirill ***@***.***>:
@eneskaya Thank you very mush! You helped me a lot. I kindly suggest firebase developers to add some information about phone auth in the doc. I know that it is only alpha, but anyway, just some words will be good.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@javamonn could you share the code you guys use to bridge and share firebaseUI? Offloading auth and all its checks sounds like viable idea. |
Ok I'm trying to setup react-native-firebase@next to try Phone authentication and followed the instructions here https://invertase.io/react-native-firebase/#/installation-android for v3.x.x Everything compiles, but when I start the app, before seeing anything I'm getting this error
Any hints what can be happening? Thanks, cheers |
I'm back in the country now, so should be able to pick back up on this over the next few days. I'll try and pick up on the questions above that haven't already been answered: @subugwriter Thanks for raising the linking issue. Auto-verify on Android has been a right pain to deal with in a cross platform manner. What I'm going to try and do is the following:
@javamonn Thanks for mentioning FirebaseUI. This is certainly an option, however by using FirebaseUI there are some limitations - namely, that it will import all the underlying Firebase modules regardless of whether they're being used in the rest of the app or not. Also, React Native makes it so easy to build UIs, that it shouldn't really be necessary. Hopefully when the above changes I've proposed are made, this will render FirebaseUI unnecessary. I'm also not sure how well the new multi app support would translate across between @maraujop Sounds like you've not got the required Android libraries being imported correctly. I'd need the rest of the error message to be able to help any further than that. @faahmad I'd suggest you debug your @bintoll Documentation will be firmed up once we've agreed on the final implementation. We wanted to find out how everybody was using the library and highlight the edge cases, e.g. linking the credential, before we spent too much time there. |
Thanks @chrisbianca for your answer. It took me a while, but I finally found a cross dependency problem, another library was loading an older version of Firebase. It is now working good, as I've excluded dependencies from libraries and manually forced dependencies. This article was of great help. Beware that I've had to do a little adjustment to your javascript example, to handle auto verification, when there is no need to request to the user for an SMS code. Now, I'm trying to compile it correctly in iOS, but I'm facing this compile error:
Maybe react-native-firebase is not compatible with react-native-fcm? I have been using that project for FCM, and I wouldn't like to change it all now, as I'm in a hurry to have this finished. My guess is that this also has to do with inter dependecy problems, or maybe that react-native-fcm is not set up using Pods. Any hint would be greatly appreciated. Thanks. By the way your project hierarchy and code quality is amazing, great work! Cheers, |
@maraujop Not sure how compatible we are with In terms of your error, I'd suggest clearing down your project, DerivedData, etc and restarting XCode. See if that helps... |
All, I've just pushed up the This implementation gives you full control of the phone number verification flow on either platform as well as a flexible api to suite any UI flow and should be familiar to use if you've used things like storage Cross Platform ExampleThis code snippet covers all scenarios for both android and iOS, how you implement this is entirely up to you. A simpler iOS only usage example can be found below this example. firebase.auth()
.verifyPhoneNumber('+441234567890')
.on('state_changed', (phoneAuthSnapshot) => {
// How you handle these state events is entirely up to your ui flow and whether
// you need to support both ios and android. In short: not all of them need to
// be handled - it's entirely up to you, your ui and supported platforms.
// E.g you could handle android specific events only here, and let the rest fall back
// to the optionalErrorCb or optionalCompleteCb functions
switch (phoneAuthSnapshot.state) {
// ------------------------
// IOS AND ANDROID EVENTS
// ------------------------
case firebase.auth.PhoneAuthState.CODE_SENT: // or 'sent'
console.log('code sent');
// on ios this is the final phone auth state event you'd receive
// so you'd then ask for user input of the code and build a credential from it
break;
case firebase.auth.PhoneAuthState.ERROR: // or 'error'
console.log('verification error');
console.log(phoneAuthSnapshot.error);
break;
// ---------------------
// ANDROID ONLY EVENTS
// ---------------------
case firebase.auth.PhoneAuthState.AUTO_VERIFY_TIMEOUT: // or 'timeout'
console.log('auto verify on android timed out');
// proceed with your manual code input flow, same as you would do in
// CODE_SENT if you were on IOS
break;
case firebase.auth.PhoneAuthState.AUTO_VERIFIED: // or 'verified'
// auto verified means the code has also been automatically confirmed as correct/received
// phoneAuthSnapshot.code will contain the auto verified sms code - no need to ask the user for input.
console.log('auto verified on android');
console.log(phoneAuthSnapshot);
// Example usage if handling here and not in optionalCompleteCb:
// const { verificationId, code } = phoneAuthSnapshot;
// const credential = firebase.auth.PhoneAuthProvider.credential(verificationId, code);
// Do something with your new credential, e.g.:
// firebase.auth().signInWithCredential(credential);
// firebase.auth().linkWithCredential(credential);
// etc ...
break;
}
}, (error) => {
// optionalErrorCb would be same logic as the ERROR case above, if you've already handed
// the ERROR case in the above observer then there's no need to handle it here
console.log(error);
// verificationId is attached to error if required
console.log(error.verificationId);
}, (phoneAuthSnapshot) => {
// optionalCompleteCb would be same logic as the AUTO_VERIFIED/CODE_SENT switch cases above
// depending on the platform. If you've already handled those cases in the observer then
// there's absolutely no need to handle it here.
// Platform specific logic:
// - if this is on IOS then phoneAuthSnapshot.code will always be null
// - if ANDROID auto verified the sms code then phoneAuthSnapshot.code will contain the verified sms code
// and there'd be no need to ask for user input of the code - proceed to credential creating logic
// - if ANDROID auto verify timed out then phoneAuthSnapshot.code would be null, just like ios, you'd
// continue with user input logic.
console.log(phoneAuthSnapshot);
});
// optionally also supports .then & .catch instead of optionalErrorCb &
// optionalCompleteCb (with the same resulting args) Basic ExampleThe api is flexible enough to not force those of you that only need to support one platform (in this case iOS) into a wall of code. Whilst this example makes use of the optional resulting promise, you can still however use the observer api instead and handle firebase.auth()
.verifyPhoneNumber('+441234567890')
.then((phoneAuthSnapshot) => {
console.log(phoneAuthSnapshot);
// wait / get users code input then:
// const { verificationId } = phoneAuthSnapshot;
// const { codeInput } = this.state; // tied to your input box, for example
// const credential = firebase.auth.PhoneAuthProvider.credential(verificationId, codeInput);
// Do something with your new credential, e.g.:
// firebase.auth().signInWithCredential(credential);
// firebase.auth().linkWithCredential(credential);
})
.catch((error) => {
console.log(error);
console.log(error.verificationId);
}); As As for Thanks again all for helping us work through the implementation. |
Thanks @chrisbianca I finally managed to get it working. It was a bad iOS + cocoapods setup. Although it's true that search paths in react-native-fcm are too open and can cause trouble. Anyway, I finally got it working in both platforms. In the future, I will probably move into using one single app. @Salakar i was trying to change my code to use this new Currently
So, it ends up in this error: |
@maraujop I'm using fcm as well. Do you have any tips to share for getting it working? |
@maraujop and others, I probably should of said this above but please wait until we give the go ahead before trying to use As you correctly said the native parts are not there yet as they require a few more tweaks / testing. My post was a heads up of what the new API is :) |
Thanks @Salakar , do you have an ETA? |
@Salakar Oh, I totally misunderstood your post, sorry, as you said you had merged the implementation I understood it was fully there. @jcharbo Make sure your
Also make sure that you do |
@maraujop this has been pushed up for android, you should be good to go now for android - will sort ios shortly. My UI skills aren't amazing, but here's a gif of android working with auto verify included, it's pretty quick: |
Hey all, this is now part of the full v3 release (with Cloud Firestore 🎉 ) that we pushed out to npm yesterday. I'll be closing this issue now. I'm still working on the docs but for now please use this thread (#119 (comment) specifically) for implementation guides. Please raise any new issues in separate github issues going forward. If you have queries / or want to continue discussing this further you can join us on discord - this thread takes far too long to load now 🙈 Thanks |
Hi everyone, I appreciate your work on this but does anyone have an estimate on when this will all be ready for production? |
awesome @Salakar , thanks! |
Is the new Firebase Phone number Auth on the roadmap?
The text was updated successfully, but these errors were encountered: