2016-05-26 18 views
5

Jestem pod wrażeniem nowej opcji Firebase i auth. Ale co jeśli chcę stworzyć własny system identyfikatora użytkownika, aby utworzyć użytkownika? Na przykład uwierzytelniam użytkownika swoim numerem telefonu (używając czegoś takiego jak cyfry tkaniny) i używam go do utworzenia konta użytkownika. Teraz, jak mogę to zrobić w nowej bazie Firebase? Czy jest to nawet wykonalne?Autoryzacja numeru telefonu w Firebase (z cyframi)

+0

FYI, uwierzytelnianie Firebase numer telefonu jest obecnie obsługiwana w Firebase (iOS i sieci, z Androidem wkrótce): https://firebase.google.com/docs/auth/web/phone-auth – bojeil

Odpowiedz

3

W tej chwili nie można tego zrobić bezpośrednio, ale można zweryfikować użytkownika za pomocą Cyfry, wysłać nagłówki zabezpieczeń do usługi sieciowej zaplecza, którą stworzyłeś, w której możesz utworzyć użytkownika poczty e-mail/hasła, używając jako adresu e-mail [email protected]_domena.com i jako hasło, które tworzysz losowo, i używasz niestandardowego uwierzytelniania firebase, aby tokeny użytkowników końcowych były ponownie uwierzytelniane, wszystko to wydawałoby się uwierzytelnieniem telefonu dla użytkownika końcowego, a on nawet nie wiedziałby, że używa uwierzytelnienia e-mail/hasło zaloguj się

+0

Dobra odpowiedź, zadbaj o udostępnienie przykładowej strategii, powiedzmy, może z web i node.js na zapleczu? – praneybehl

+0

Od aktualizacji firebase nie można korzystać z opisanej powyżej metody poczty e-mail, głównie dlatego, że zmieniły one sposób działania createUser, ale można w rzeczywistości utworzyć serwer nodejs (np. Express js), konfigurując serwer zgodnie z opisem w dokumentacji Firebase i korzystając z uwierzytelnianie cyfr zmieszane z tokenem niestandardowym firebase w celu powiązania UID z numerem telefonu – Ymmanuel

+0

FYI https://github.com/deltaepsilon/firebase-digits – SDude

2

Użyłem prostszej metody uwierzytelniania numeru telefonu, pisząc moją funkcję logowania, aby zaakceptować telefon komórkowy jako wejście.

a następnie dodanie wspólną nazwę domeny do końca mobileno w Twoim funkcji login.ts - (za każdym wywołaniu metody uwierzytelniania)

[email protected] 

Nie trzeba używać w tym celu usługa sieci Web innej firmy, a nawet niestandardowe metody uwierzytelniania w Firebase.

Wystarczy użyć standardowego uwierzytelniania poczty e-mail/hasła w Firebase przy niewielkiej zmianie kodu, dołączając domenę e-mail do telefonu komórkowego i obsługiwać go w kodzie.

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

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

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


} 
0

teraz telefon auth jest dostępny w firebase.Here jest kod do telefonu Auth użyciu 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] 
     } 
    }; 
Powiązane problemy