5

I am super impressed with the new Firebase and auth options in that. But what if I want to create my own userID-password system to create a user? Like for example I authenticate the user with his phone number (using something like Fabric's Digits) and use that to create an user account. Now, how can I do this in the new Google's firebase? Or is it even doable?

Codevalley
  • 4,593
  • 7
  • 42
  • 56
  • FYI, Firebase phone number authentication is now supported in Firebase (iOS and Web, with Android coming soon): https://firebase.google.com/docs/auth/web/phone-auth – bojeil May 19 '17 at 06:35

4 Answers4

3

Right now it cannot be done directly, but you could validate the user with Digits, send the security headers to a backend Web Service you developed in which you can create a email/password user using as email phone_number@yourdomain.com and as password a string you randomly created, and using firebase custom authentication to give your end users tokens to reauthenticate, all this would seem as phone authentication to the end user, and he would'n even know he is using a email/password auth to sign in

Ymmanuel
  • 2,523
  • 13
  • 12
  • Good answer, Care to share a sample strategy, say maybe with web and node.js on the backend? – praneybehl Aug 24 '16 at 23:21
  • Since the firebase upgrade you cannot use the email method described above, mainly because they changed the way createUser works, but you can actually create a nodejs backend (example express js) set up the server as it is described in firebase documentation and use the digits authentication mixed with the firebase custom token to associate an UID to a phone number – Ymmanuel Aug 25 '16 at 19:39
2

I've used a simpler method of phone number authentication, by writing my login function to accept the mobile no as input.

and then appending a common domain name to the end of the mobileno in your login.ts function - (everytime you call the authentication methods)

9xxx9@mobileappdomain.com

You don't need to use a 3rd Party Web Service for this, nor even the custom authentication Methods in Firebase.

Simply use the standard email/password authentication in Firebase with very little code change, by appending the email domain to the mobile no and handle it in your code.

login() {
    this.login.mobileno = this.login.mobileno + '@appdomain.com';

       this.auth.signin(this.login)
           .then((data) => {

                ...............
                 ............................
             }  


}
Magnus Melwin
  • 1,509
  • 1
  • 21
  • 32
0

Firebase is now supporting phone number authentication.

https://firebase.google.com/docs/auth/ios/phone-auth

Gohar Ali
  • 9
  • 1
0

now phone auth is available in firebase.Here is Code for Phone Auth using Firebase:

EditText phoneNum,Code;   // two edit text one for enter phone number other for enter OTP code
Button sent_,Verify;    // sent_ button to request for verification and verify is for to verify code
private PhoneAuthProvider.ForceResendingToken mResendToken;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private FirebaseAuth mAuth;
private String mVerificationId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_phone_number_auth);

    phoneNum =(EditText) findViewById(R.id.fn_num);
    Code =(EditText) findViewById(R.id.code);

    sent_ =(Button)findViewById(R.id.sent_nu);
    Verify =(Button)findViewById(R.id.verify);

    callback_verificvation();

    mAuth = FirebaseAuth.getInstance();
    sent_.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String num=phoneNum.getText().toString();
            startPhoneNumberVerification(num);    // call function for receive OTP 6 digit code
        }
    });
    Verify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String code=Code.getText().toString();
            verifyPhoneNumberWithCode(mVerificationId,code);            //call function for verify code 

        }
    });
}

private void startPhoneNumberVerification(String phoneNumber) {
    // [START start_phone_auth]
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);        // OnVerificationStateChangedCallbacks
    // [END start_phone_auth]


}

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information

                        FirebaseUser user = task.getResult().getUser();
                        Toast.makeText(getApplicationContext(), "sign in successfull", Toast.LENGTH_SHORT).show();
                        // [START_EXCLUDE]

                        // [END_EXCLUDE]
                    } else {
                        // Sign in failed, display a message and update the UI

                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid
                            // [START_EXCLUDE silent]

                            // [END_EXCLUDE]
                        }
                        // [START_EXCLUDE silent]
                        // Update UI

                        // [END_EXCLUDE]
                    }
                }
            });
}
private void verifyPhoneNumberWithCode(String verificationId, String code) {
    // [START verify_with_code]
    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
    // [END verify_with_code]
    signInWithPhoneAuthCredential(credential);
}


private void callback_verificvation() {

    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential credential) {
            // This callback will be invoked in two situations:
            // 1 - Instant verification. In some cases the phone number can be instantly
            //     verified without needing to send or enter a verification code.
            // 2 - Auto-retrieval. On some devices Google Play services can automatically
            //     detect the incoming verification SMS and perform verificaiton without
            //     user action.
            // [START_EXCLUDE silent]
            // [END_EXCLUDE]

            // [START_EXCLUDE silent]
            // Update the UI and attempt sign in with the phone credential
            // [END_EXCLUDE]
            signInWithPhoneAuthCredential(credential);
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            // This callback is invoked in an invalid request for verification is made,
            // for instance if the the phone number format is not valid.
            // [START_EXCLUDE silent]
            // [END_EXCLUDE]

            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Invalid request
                // [START_EXCLUDE]

                // [END_EXCLUDE]
            } else if (e instanceof FirebaseTooManyRequestsException) {
                // The SMS quota for the project has been exceeded
                // [START_EXCLUDE]

                // [END_EXCLUDE]
            }

            // Show a message and update the UI
            // [START_EXCLUDE]

            // [END_EXCLUDE]
        }

        @Override
        public void onCodeSent(String verificationId,
                               PhoneAuthProvider.ForceResendingToken token) {
            // The SMS verification code has been sent to the provided phone number, we
            // now need to ask the user to enter the code and then construct a credential
            // by combining the code with a verification ID.


            // Save verification ID and resending token so we can use them later
            mVerificationId = verificationId;
            mResendToken = token;

            // [START_EXCLUDE]
            // Update UI

            // [END_EXCLUDE]
        }
    };